# bot_discord.py import discord from discord.ext import commands import importlib import cmd_discord from modules import db class DiscordBot(commands.Bot): def __init__(self, config, log_func): super().__init__(command_prefix="!", intents=discord.Intents.all()) self.config = config self.log = log_func # Use the logging function from bots.py self.db_conn = None # We'll set this later self.load_commands() def set_db_connection(self, db_conn): """ Store the DB connection in the bot so commands can use it. """ self.db_conn = db_conn try: db.ensure_quotes_table(self.db_conn, self.log) except Exception as e: self.log(f"Critical: unable to ensure quotes table: {e}", "FATAL") def load_commands(self): """ Load all commands dynamically from cmd_discord.py. """ try: importlib.reload(cmd_discord) cmd_discord.setup(self) self.log("Discord commands loaded successfully.", "INFO") except Exception as e: self.log(f"Error loading Discord commands: {e}", "ERROR") async def on_command(self, ctx): """Logs every command execution at DEBUG level.""" self.log(f"Discord Command executed: {ctx.command} by {ctx.author} in #{ctx.channel}: {ctx.message.content}", "DEBUG") async def on_ready(self): self.log(f"Discord bot is online as {self.user}", "INFO") async def run(self, token): try: await super().start(token) except Exception as e: self.log(f"Discord bot error: {e}", "CRITICAL")