64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
# bot_discord.py
|
|
import discord
|
|
from discord.ext import commands
|
|
import importlib
|
|
import cmd_discord
|
|
|
|
import modules
|
|
import modules.utility
|
|
|
|
class DiscordBot(commands.Bot):
|
|
def __init__(self, config, log_func):
|
|
super().__init__(command_prefix="!", intents=discord.Intents.all())
|
|
self.remove_command("help") # Remove built-in help function
|
|
self.config = config
|
|
self.log = log_func # Use the logging function from bots.py
|
|
self.db_conn = None # We'll set this later
|
|
self.help_data = 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:
|
|
modules.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 from cmd_discord.py
|
|
"""
|
|
try:
|
|
importlib.reload(cmd_discord)
|
|
cmd_discord.setup(self)
|
|
self.log("Discord commands loaded successfully.", "INFO")
|
|
|
|
# Now load the help info from dictionary/help_discord.json
|
|
help_json_path = "dictionary/help_discord.json"
|
|
|
|
modules.utility.initialize_help_data(
|
|
bot=self,
|
|
help_json_path=help_json_path,
|
|
is_discord=True,
|
|
log_func=self.log
|
|
)
|
|
|
|
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")
|