Started creating config file system
This commit is contained in:
parent
ae2e63da56
commit
60d6bd2c40
45
bots/discordbot.py
Normal file
45
bots/discordbot.py
Normal file
@ -0,0 +1,45 @@
|
||||
import sys
|
||||
from discord.ext import commands
|
||||
import os
|
||||
import yaml
|
||||
|
||||
class DiscordBot:
|
||||
def __init__(self, config):
|
||||
print("jajajah")
|
||||
self.constants = ["token"]
|
||||
self.modules = {"quiz": "quiz"}
|
||||
prefixes = ["/"]
|
||||
|
||||
self.client = commands.Bot(command_prefix=prefixes, case_insensitive=True)
|
||||
self.client.prefixes = prefixes
|
||||
self.load_config(config)
|
||||
# Load this cog first so the other cogs can use properties initialized here
|
||||
self.client.load_extension("cogs.events")
|
||||
|
||||
# for file in os.listdir("./cogs"):
|
||||
# if file.endswith(".py") and not (file.startswith(("events",))):
|
||||
# self.client.load_extension(f"cogs.{file[:-3]}")
|
||||
|
||||
def run(self):
|
||||
if "token" in self.client.config.keys():
|
||||
print(self.client.config["token"])
|
||||
self.client.run(self.client.config["token"])
|
||||
|
||||
def get_client(self):
|
||||
return self.client
|
||||
|
||||
def load_config(self, config):
|
||||
with open(config, 'r') as file:
|
||||
try:
|
||||
self.client.config = yaml.safe_load(file)
|
||||
for cog in self.client.config:
|
||||
if cog not in self.constants and self.loaded_cog(cog):
|
||||
print(f"Loaded cog {cog}")
|
||||
except yaml.YAMLError as exc:
|
||||
print(exc)
|
||||
|
||||
def loaded_cog(self, module):
|
||||
if os.path.isfile(f"/cogs/{self.modules[module]}.py"):
|
||||
self.client.load_extension(f"cogs.{self.modules[module]}")
|
||||
return True
|
||||
return False
|
||||
17
tests/quiz_test.py
Normal file
17
tests/quiz_test.py
Normal file
@ -0,0 +1,17 @@
|
||||
from bots.discordbot import DiscordBot
|
||||
import discord.ext.test as dpytest
|
||||
import pytest
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bot():
|
||||
bot = DiscordBot("config.yaml")
|
||||
|
||||
# Load any extensions/cogs you want to in here
|
||||
dpytest.configure(bot.get_client())
|
||||
config = dpytest.get_config()
|
||||
channel = config.channels[0]
|
||||
user = dpytest.backend.make_user("test", 1, 1)
|
||||
member = dpytest.backend.make_member(user, config.guilds[0])
|
||||
guild = config.guilds[0]
|
||||
await dpytest.message("hallo", channel, member)
|
||||
dpytest.verify_message("[Expected help output]")
|
||||
Loading…
x
Reference in New Issue
Block a user