mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
refactor(mention): move from bot.py to event cog
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,6 +2,7 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
*$py.class
|
*$py.class
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
@@ -148,7 +149,5 @@ cython_debug/
|
|||||||
|
|
||||||
# Log file
|
# Log file
|
||||||
discord.log
|
discord.log
|
||||||
|
|
||||||
cogs/media/files/
|
cogs/media/files/
|
||||||
|
|
||||||
logs.txt
|
logs.txt
|
||||||
31
bot.py
31
bot.py
@@ -1,11 +1,6 @@
|
|||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import random
|
import random
|
||||||
import signal
|
|
||||||
import sys
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
@@ -198,30 +193,6 @@ class DiscordBot(commands.Bot):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Error during bot shutdown: {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:
|
async def on_command_completion(self, context: Context) -> None:
|
||||||
full_command_name = context.command.qualified_name
|
full_command_name = context.command.qualified_name
|
||||||
split = full_command_name.split(" ")
|
split = full_command_name.split(" ")
|
||||||
@@ -310,5 +281,5 @@ if __name__ == "__main__":
|
|||||||
bot.run(os.getenv("TOKEN"))
|
bot.run(os.getenv("TOKEN"))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
from .baitbot import baitbot_command, BaitBotListener, has_protected_role
|
from .baitbot import baitbot_command, BaitBotListener, has_protected_role
|
||||||
|
from .mention import MentionListener
|
||||||
|
|
||||||
|
|
||||||
def _require_group_prefix(context: Context) -> bool:
|
def _require_group_prefix(context: Context) -> bool:
|
||||||
@@ -65,4 +66,8 @@ async def setup(bot) -> None:
|
|||||||
listener = BaitBotListener(bot)
|
listener = BaitBotListener(bot)
|
||||||
await bot.add_cog(listener)
|
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.baitbot'")
|
||||||
|
bot.logger.info("Loaded extension 'events.mention'")
|
||||||
|
|||||||
41
cogs/events/mention.py
Normal file
41
cogs/events/mention.py
Normal 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)
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user