diff --git a/bots/discordbot.py b/bots/discordbot.py new file mode 100644 index 0000000..6a0b695 --- /dev/null +++ b/bots/discordbot.py @@ -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 diff --git a/test.py b/test.py new file mode 100644 index 0000000..ef7bc57 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +from tests.quiz_test import * diff --git a/tests/quiz_test.py b/tests/quiz_test.py new file mode 100644 index 0000000..a1602d9 --- /dev/null +++ b/tests/quiz_test.py @@ -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]")