OokamiPupV2/bot_discord.py

67 lines
2.3 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()
self.log("Discord bot initiated")
cmd_class = str(type(self.commands)).split("'", 2)[1]
log_func(f"DiscordBot.commands type: {cmd_class}", "DEBUG")
def set_db_connection(self, db_conn):
"""
Store the DB connection in the bot so commands can use it.
"""
self.db_conn = db_conn
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.")
# 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."""
_cmd_args = str(ctx.message.content).split(" ")[1:]
self.log(f"Command '{ctx.command}' (Discord) initiated by {ctx.author} in #{ctx.channel}", "DEBUG")
if len(_cmd_args) > 1: self.log(f"!{ctx.command} arguments: {_cmd_args}", "DEBUG")
async def on_ready(self):
self.log(f"Discord bot is online as {self.user}")
async def run(self, token):
try:
await super().start(token)
except Exception as e:
self.log(f"Discord bot error: {e}", "CRITICAL")