Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

feat: add .reaction and .reaction_count to MessageReaction events #561

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions naff/api/events/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def on_guild_join(event):
from naff.models.discord.stage_instance import StageInstance
from naff.models.naff.context import ModalContext
from naff.models.discord.auto_mod import AutoModerationAction, AutoModRule
from naff.models.discord.reaction import Reaction


@define(kw_only=False)
Expand Down Expand Up @@ -452,6 +453,17 @@ class MessageReactionAdd(BaseEvent):
message: "Message" = field(metadata=docs("The message that was reacted to"))
emoji: "PartialEmoji" = field(metadata=docs("The emoji that was added to the message"))
author: Union["Member", "User"] = field(metadata=docs("The user who added the reaction"))
# reaction can be None when the message is not in the cache, and it was the last reaction, and it was deleted in the event
reaction: Optional["Reaction"] = field(
default=None, metadata=docs("The reaction object corresponding to the emoji")
)

@property
def reaction_count(self) -> int:
"""Times the emoji in the event has been used to react"""
if self.reaction is None:
return 0
return self.reaction.count


@define(kw_only=False)
Expand Down
32 changes: 18 additions & 14 deletions naff/api/events/processors/reaction_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async def _handle_message_reaction_change(self, event: "RawGatewayEvent", add: b

emoji = PartialEmoji.from_dict(event.data.get("emoji")) # type: ignore
message = self.cache.get_message(event.data.get("channel_id"), event.data.get("message_id"))
reaction = None

if message:
for i in range(len(message.reactions)):
Expand All @@ -34,28 +35,31 @@ async def _handle_message_reaction_change(self, event: "RawGatewayEvent", add: b
message.reactions.pop(i)
else:
message.reactions[i] = r
reaction = r
break
else:
message.reactions.append(
Reaction.from_dict(
{
"count": 1,
"me": author.id == self.user.id, # type: ignore
"emoji": emoji.to_dict(),
"message_id": message.id,
"channel_id": message._channel_id,
},
self, # type: ignore
)
reaction = Reaction.from_dict(
{
"count": 1,
"me": author.id == self.user.id, # type: ignore
"emoji": emoji.to_dict(),
"message_id": message.id,
"channel_id": message._channel_id,
},
self, # type: ignore
)
message.reactions.append(reaction)

else:
message = await self.cache.fetch_message(event.data.get("channel_id"), event.data.get("message_id"))

for r in message.reactions:
if r.emoji == emoji:
reaction = r
break
if add:
self.dispatch(events.MessageReactionAdd(message=message, emoji=emoji, author=author))
self.dispatch(events.MessageReactionAdd(message=message, emoji=emoji, author=author, reaction=reaction))
else:
self.dispatch(events.MessageReactionRemove(message=message, emoji=emoji, author=author))
self.dispatch(events.MessageReactionRemove(message=message, emoji=emoji, author=author, reaction=reaction))

@Processor.define()
async def _on_raw_message_reaction_add(self, event: "RawGatewayEvent") -> None:
Expand Down