Merge pull request #45 from Payphone-Bot/14-readd-support-for-reactions

feat(bot): ✨ Add support for reactions.
This commit is contained in:
Rafael Bradley
2024-10-23 15:27:30 -03:00
committed by GitHub
2 changed files with 126 additions and 2 deletions
+2 -2
View File
@@ -85,14 +85,14 @@ class Payphone(commands.AutoShardedBot):
async def get_or_fetch_channel( async def get_or_fetch_channel(
self, channel_id: int self, channel_id: int
) -> abc.GuildChannel | abc.PrivateChannel | None: ) -> abc.GuildChannel | abc.PrivateChannel | discord.Thread | None:
"""Get's a channel from the bot's cache, or fetches it from Discord if it's not available. """Get's a channel from the bot's cache, or fetches it from Discord if it's not available.
Args: Args:
channel_id (int): The channel's ID. channel_id (int): The channel's ID.
Returns: Returns:
(abc.GuildChannel | abc.PrivateChannel | None): The channel, or None if not found. (abc.GuildChannel | abc.PrivateChannel | discord.Thread | None): The channel, or None if not found.
""" """
channel = self.get_channel(channel_id) channel = self.get_channel(channel_id)
+124
View File
@@ -1101,6 +1101,130 @@ class Calls(base.Cog):
# TODO: Edit replies. # TODO: Edit replies.
@commands.Cog.listener()
async def on_raw_reaction_add(
self, payload: discord.RawReactionActionEvent
) -> None:
if payload.user_id == self.bot.user.id:
return
channel = await models.Channel.get_or_none(id=payload.channel_id)
if channel is None:
return
if channel.call_id is None:
return
call_id = channel.call_id
other_channel_id = None
other_channel_message_id = await redis.hget(
constants.CALL_MESSAGE_FROM_PROPAGATION_TEMPLATE % call_id,
payload.message_id,
)
if other_channel_message_id is None:
other_channel_message_json = await redis.hget(
constants.CALL_MESSAGES_TEMPLATE % call_id, payload.message_id
)
if other_channel_message_json is None:
return
other_channel_message_dict = orjson.loads(other_channel_message_json)
if other_channel_message_dict["propagation_id"] is None:
# The message wasn't propagated
return
other_channel_id = other_channel_message_dict["propagation_channel_id"]
other_channel_message_id = other_channel_message_dict["propagation_id"]
else:
other_channel = await models.Channel.get_or_none(
call_id=call_id, id__not=payload.channel_id
)
if other_channel is not None:
other_channel_id = other_channel.id
else:
return
other_channel_message_id = int(other_channel_message_id)
channel = await self.bot.get_or_fetch_channel(other_channel_id)
if channel is None:
return
await channel.get_partial_message(other_channel_message_id).add_reaction(
payload.emoji
)
@commands.Cog.listener()
async def on_raw_reaction_remove(
self, payload: discord.RawReactionActionEvent
) -> None:
if payload.user_id == self.bot.user.id:
return
channel = await models.Channel.get_or_none(id=payload.channel_id)
if channel is None:
return
if channel.call_id is None:
return
call_id = channel.call_id
other_channel_id = None
other_channel_message_id = await redis.hget(
constants.CALL_MESSAGE_FROM_PROPAGATION_TEMPLATE % call_id,
payload.message_id,
)
if other_channel_message_id is None:
other_channel_message_json = await redis.hget(
constants.CALL_MESSAGES_TEMPLATE % call_id, payload.message_id
)
if other_channel_message_json is None:
return
other_channel_message_dict = orjson.loads(other_channel_message_json)
if other_channel_message_dict["propagation_id"] is None:
# The message wasn't propagated
return
other_channel_id = other_channel_message_dict["propagation_channel_id"]
other_channel_message_id = other_channel_message_dict["propagation_id"]
else:
other_channel = await models.Channel.get_or_none(
call_id=call_id, id__not=payload.channel_id
)
if other_channel is not None:
other_channel_id = other_channel.id
else:
return
other_channel_message_id = int(other_channel_message_id)
channel = await self.bot.get_or_fetch_channel(other_channel_id)
if channel is None:
return
await channel.get_partial_message(other_channel_message_id).remove_reaction(
payload.emoji, self.bot.user
)
def setup(b: bot.Payphone) -> None: def setup(b: bot.Payphone) -> None:
"""Load the cog into the bot. """Load the cog into the bot.