From e09ec5c946538eb6874a08c46e0032eee501aa53 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Wed, 15 Jun 2022 01:56:18 +0200 Subject: [PATCH] Loading cogs --- didier/cogs/__init__.py | 0 didier/cogs/test_cog.py | 14 ++++++++++++++ didier/didier.py | 28 ++++++++++++++++++++++++++++ main.py | 2 +- tests/__init__.py | 0 tests/test_dummy.py | 2 ++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 didier/cogs/__init__.py create mode 100644 didier/cogs/test_cog.py create mode 100644 tests/__init__.py create mode 100644 tests/test_dummy.py diff --git a/didier/cogs/__init__.py b/didier/cogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/didier/cogs/test_cog.py b/didier/cogs/test_cog.py new file mode 100644 index 0000000..093aaf7 --- /dev/null +++ b/didier/cogs/test_cog.py @@ -0,0 +1,14 @@ +from discord.ext import commands + +from didier import Didier + + +class TestCog(commands.Cog): + client: Didier + + def __init__(self, client: Didier): + self.client = client + + +async def setup(client: Didier): + await client.add_cog(TestCog(client)) diff --git a/didier/didier.py b/didier/didier.py index 1329163..f4b36c5 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -1,3 +1,5 @@ +import os + import discord from discord.ext import commands from sqlalchemy.ext.asyncio import AsyncSession @@ -10,6 +12,8 @@ from didier.utils.prefix import get_prefix class Didier(commands.Bot): """DIDIER <3""" + initial_extensions: tuple[str] = () + def __init__(self): activity = discord.Activity(type=discord.ActivityType.playing, name=settings.DISCORD_STATUS_MESSAGE) status = discord.Status.online @@ -27,7 +31,31 @@ class Didier(commands.Bot): command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status ) + async def setup_hook(self) -> None: + """Hook called once the bot is initialised""" + await self._load_initial_cogs() + await self._load_directory_cogs("didier/cogs") + @property def db_session(self) -> AsyncSession: """Obtain a database session""" return DBSession() + + async def on_ready(self): + """Event triggered when the bot is ready""" + print(settings.DISCORD_READY_MESSAGE) + + async def _load_initial_cogs(self): + """Load all cogs""" + for extension in self.initial_extensions: + await self.load_extension(f"didier.cogs.{extension}") + + async def _load_directory_cogs(self, path: str): + """Load all cogs in a given directory""" + load_path = path.removeprefix("./").replace("/", ".") + + for file in os.listdir(path): + if file.endswith(".py") and not file.startswith("_") and not file.startswith(self.initial_extensions): + await self.load_extension(f"{load_path}.{file[:-3]}") + elif os.path.isdir(new_path := f"{path}/{file}"): + await self._load_directory_cogs(new_path) diff --git a/main.py b/main.py index cb4aa8e..8c8168b 100644 --- a/main.py +++ b/main.py @@ -22,9 +22,9 @@ def setup_logging(): handler = RotatingFileHandler(settings.LOGFILE, mode="a", maxBytes=max_log_size, backupCount=5) handler.setFormatter(logging.Formatter("[%(asctime)s] [%(levelname)s]: %(message)s")) - handler.setLevel(logging.INFO) didier_log.addHandler(handler) + didier_log.setLevel(logging.INFO) logging.getLogger("discord").setLevel(logging.ERROR) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_dummy.py b/tests/test_dummy.py new file mode 100644 index 0000000..f4f5361 --- /dev/null +++ b/tests/test_dummy.py @@ -0,0 +1,2 @@ +def test_dummy(): + assert True