From e71558959c8930e3af90b109776c96b2c2415400 Mon Sep 17 00:00:00 2001 From: "Max R. Carrara" Date: Fri, 14 Mar 2025 00:14:08 +0100 Subject: [PATCH] bot: cogs: add Debug cog and load extensions when bot is ready As the name implies, the Debug cog is solely there to debug stuff. In the future the commands in that cog should only respond to whoever has the provided `BOT_OWNER_ID` (me). Additionally, extensions for discord.py are now loaded dynamically in `main.py`. Signed-off-by: Max R. Carrara --- src/bot/cogs/debug.py | 43 +++++++++++++++++++++++++++++++++++++++++++ src/bot/main.py | 14 ++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/bot/cogs/debug.py diff --git a/src/bot/cogs/debug.py b/src/bot/cogs/debug.py new file mode 100644 index 0000000..e4d4c92 --- /dev/null +++ b/src/bot/cogs/debug.py @@ -0,0 +1,43 @@ +import asyncio +import logging + +import discord +from discord.ext import commands + +from bot.types import RichEmbed + +_log = logging.getLogger(__name__) + + +class Debug(commands.Cog): + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + + @commands.command() + async def ping(self, ctx: commands.Context): + _log.debug("pong!") + + embed: RichEmbed = { + "type": "rich", + "description": "Pong." + } + + await ctx.send(embed=discord.Embed.from_dict(embed)) + + +async def setup(bot: commands.Bot): + _log.debug("Adding Debug cog") + await bot.add_cog(Debug(bot)) + + cog = bot.get_cog(Debug.__name__) + + if cog is None: + return + + _log.debug("Loaded Debug cog with the following commands:") + _log.debug([c.name for c in cog.get_commands()]) + + +async def teardown(bot: commands.Bot): + _log.debug("Removing Debug cog") + await bot.remove_cog(Debug.__name__) diff --git a/src/bot/main.py b/src/bot/main.py index 0709a4b..4f9c8ad 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -12,6 +12,9 @@ from bot.env import Environment _log = logging.getLogger(__name__) +_default_extensions: dict[str, str] = { + "Debug": "bot.cogs.debug", +} def setup_logging(): @@ -127,6 +130,17 @@ def register_event_handlers(bot: commands.Bot): async def on_ready(): _log.info("Ready: Connected to Discord") + _log.info("Loading extensions") + for ext_name, ext_path in _default_extensions.items(): + try: + _log.info(f'Loading extension "{ext_name}" ({ext_path})') + await bot.load_extension(ext_path) + _log.info(f'Loaded extension "{ext_name}" ({ext_path})') + except Exception as e: + _log.error( + f'Failed to load extension "{ext_name}" ({ext_path})', exc_info=e + ) + @bot.event async def on_resumed(): _log.info("Resumed: Session with Discord resumed")