2024-08-12 07:35:37 +00:00
|
|
|
import contextlib
|
2024-08-18 09:16:18 +00:00
|
|
|
|
2024-07-10 18:11:20 +00:00
|
|
|
from discord import Message
|
2024-07-10 20:11:12 +00:00
|
|
|
from discord.ext.commands import Cog
|
|
|
|
from loguru import logger
|
2024-04-05 12:05:36 +00:00
|
|
|
|
2024-07-08 19:50:07 +00:00
|
|
|
from services.blacklist_service import BlacklistUserService
|
2024-07-17 12:01:12 +00:00
|
|
|
from services.reactions_service import CustomReactionsService
|
2024-04-05 12:06:44 +00:00
|
|
|
|
2024-04-05 12:05:36 +00:00
|
|
|
|
|
|
|
class ReactionHandler:
|
2024-07-10 18:11:20 +00:00
|
|
|
"""
|
|
|
|
Handles reactions to messages based on predefined triggers and responses.
|
|
|
|
"""
|
2024-07-10 20:11:12 +00:00
|
|
|
|
2024-07-10 18:11:20 +00:00
|
|
|
def __init__(self, client, message: Message) -> None:
|
2024-07-10 20:11:12 +00:00
|
|
|
self.client = client
|
2024-07-10 18:11:20 +00:00
|
|
|
self.message: Message = message
|
|
|
|
self.content: str = self.message.content.lower()
|
2024-07-10 20:11:12 +00:00
|
|
|
self.reaction_service = CustomReactionsService()
|
|
|
|
|
2024-07-11 13:30:28 +00:00
|
|
|
async def run_checks(self) -> None:
|
2024-07-10 18:11:20 +00:00
|
|
|
"""
|
2024-07-11 13:30:28 +00:00
|
|
|
Runs checks for reactions and responses.
|
|
|
|
Guild triggers are prioritized over global triggers if they are identical.
|
2024-07-10 18:11:20 +00:00
|
|
|
"""
|
2024-07-10 20:11:12 +00:00
|
|
|
guild_id = self.message.guild.id if self.message.guild else None
|
2024-04-05 12:05:36 +00:00
|
|
|
|
2024-07-10 20:11:12 +00:00
|
|
|
if guild_id:
|
2024-07-11 13:30:28 +00:00
|
|
|
data = await self.reaction_service.find_trigger(guild_id, self.content)
|
|
|
|
if data:
|
2024-07-10 20:11:12 +00:00
|
|
|
processed = False
|
|
|
|
try:
|
2024-07-11 13:30:28 +00:00
|
|
|
if data["type"] == "text":
|
|
|
|
processed = await self.try_respond(data)
|
|
|
|
elif data["type"] == "emoji":
|
|
|
|
processed = await self.try_react(data)
|
2024-07-10 20:11:12 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.warning(f"Failed to process reaction: {e}")
|
|
|
|
|
|
|
|
if processed:
|
|
|
|
await self.reaction_service.increment_reaction_usage(
|
2024-07-17 11:47:26 +00:00
|
|
|
int(data["id"]),
|
2024-07-10 20:11:12 +00:00
|
|
|
)
|
2024-07-17 11:47:26 +00:00
|
|
|
|
2024-07-11 13:30:28 +00:00
|
|
|
async def try_respond(self, data) -> bool:
|
|
|
|
"""
|
|
|
|
Tries to respond to the message.
|
|
|
|
"""
|
2024-08-12 07:35:37 +00:00
|
|
|
if response := data.get("response"):
|
|
|
|
with contextlib.suppress(Exception):
|
2024-07-11 13:30:28 +00:00
|
|
|
await self.message.reply(response)
|
|
|
|
return True
|
|
|
|
return False
|
2024-07-17 11:47:26 +00:00
|
|
|
|
2024-07-11 13:30:28 +00:00
|
|
|
async def try_react(self, data) -> bool:
|
|
|
|
"""
|
|
|
|
Tries to react to the message.
|
|
|
|
"""
|
2024-08-12 07:35:37 +00:00
|
|
|
if emoji_id := data.get("emoji_id"):
|
|
|
|
with contextlib.suppress(Exception):
|
|
|
|
if emoji := self.client.get_emoji(emoji_id):
|
2024-07-11 13:30:28 +00:00
|
|
|
await self.message.add_reaction(emoji)
|
|
|
|
return True
|
|
|
|
return False
|
2024-04-05 12:05:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReactionListener(Cog):
|
2024-07-10 18:11:20 +00:00
|
|
|
def __init__(self, client) -> None:
|
2024-04-05 12:05:36 +00:00
|
|
|
self.client = client
|
|
|
|
|
2024-07-10 20:11:12 +00:00
|
|
|
@Cog.listener("on_message")
|
2024-07-10 18:11:20 +00:00
|
|
|
async def reaction_listener(self, message: Message) -> None:
|
|
|
|
"""
|
|
|
|
Listens for new messages and processes them if the author is not a bot and not blacklisted.
|
2024-07-10 20:11:12 +00:00
|
|
|
|
2024-07-10 18:11:20 +00:00
|
|
|
:param message: The message to process.
|
|
|
|
"""
|
2024-07-10 20:11:12 +00:00
|
|
|
if not message.author.bot and not BlacklistUserService.is_user_blacklisted(
|
2024-08-22 12:42:11 +00:00
|
|
|
message.author.id,
|
2024-07-10 20:11:12 +00:00
|
|
|
):
|
2024-07-11 13:30:28 +00:00
|
|
|
await ReactionHandler(self.client, message).run_checks()
|
2024-04-05 12:05:36 +00:00
|
|
|
|
|
|
|
|
2024-07-10 18:11:20 +00:00
|
|
|
def setup(client) -> None:
|
2024-04-05 12:05:36 +00:00
|
|
|
client.add_cog(ReactionListener(client))
|