refactor(mention): move from bot.py to event cog

This commit is contained in:
neoarz
2025-11-03 15:15:52 -05:00
parent 1eff6c9f53
commit 480b5ce83a
4 changed files with 48 additions and 32 deletions

3
.gitignore vendored
View File

@@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
.ruff_cache/
# C extensions
*.so
@@ -148,7 +149,5 @@ cython_debug/
# Log file
discord.log
cogs/media/files/
logs.txt

31
bot.py
View File

@@ -1,11 +1,6 @@
import asyncio
import json
import logging
import os
import platform
import random
import signal
import sys
import time
import aiosqlite
@@ -198,30 +193,6 @@ class DiscordBot(commands.Bot):
except Exception as e:
self.logger.error(f"Error during bot shutdown: {e}")
async def on_message(self, message: discord.Message) -> None:
if message.author == self.user or message.author.bot:
return
if self.user in message.mentions:
try:
emoji_string = "<a:PandaPing:1417550314260926575>"
self.logger.debug(
f"Attempting to react with PandaPing emoji: {emoji_string}"
)
await message.add_reaction(emoji_string)
self.logger.debug("Successfully reacted with PandaPing emoji")
except Exception as e:
self.logger.debug(f"Failed to react with PandaPing emoji: {e}")
try:
self.logger.debug("Falling back to wave emoji")
await message.add_reaction("👋")
self.logger.debug("Successfully reacted with wave emoji")
except Exception as fallback_error:
self.logger.debug(
f"Failed to react with fallback emoji: {fallback_error}"
)
await self.process_commands(message)
async def on_command_completion(self, context: Context) -> None:
full_command_name = context.command.qualified_name
split = full_command_name.split(" ")
@@ -310,5 +281,5 @@ if __name__ == "__main__":
bot.run(os.getenv("TOKEN"))
except KeyboardInterrupt:
pass
except:
except Exception:
pass

View File

@@ -4,6 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
from .baitbot import baitbot_command, BaitBotListener, has_protected_role
from .mention import MentionListener
def _require_group_prefix(context: Context) -> bool:
@@ -65,4 +66,8 @@ async def setup(bot) -> None:
listener = BaitBotListener(bot)
await bot.add_cog(listener)
mention_listener = MentionListener(bot)
await bot.add_cog(mention_listener)
bot.logger.info("Loaded extension 'events.baitbot'")
bot.logger.info("Loaded extension 'events.mention'")

41
cogs/events/mention.py Normal file
View File

@@ -0,0 +1,41 @@
import discord
import random
from discord.ext import commands
class MentionListener(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
if message.author == self.bot.user or message.author.bot:
return
if self.bot.user in message.mentions:
try:
emoji_options = [
"<a:PandaPing:1417550314260926575>",
"<:PandaPing2:1434998389451395224>",
"<:PandaPing3:1434998524696723466>",
]
selected_emoji = random.choice(emoji_options)
self.bot.logger.debug(
f"Attempting to react with emoji: {selected_emoji}"
)
await message.add_reaction(selected_emoji)
self.bot.logger.debug("Successfully reacted with mention emoji")
except Exception as e:
self.bot.logger.debug(f"Failed to react with mention emoji: {e}")
try:
self.bot.logger.debug("Falling back to wave emoji")
await message.add_reaction("👋")
self.bot.logger.debug("Successfully reacted with wave emoji")
except Exception as fallback_error:
self.bot.logger.debug(
f"Failed to react with fallback emoji: {fallback_error}"
)
await self.bot.process_commands(message)