Skip to content

Commit

Permalink
Fix rate limit issue on raw reaction add/remove events. (modmail-dev#…
Browse files Browse the repository at this point in the history
…3306)

* Fix rate limit issue on raw reaction add/remove events.

* Pasd message object to `find_linked_messages` since it is already fetched.

---------

Co-authored-by: Taku <[email protected]>
(cherry picked from commit ae99060)
Signed-off-by: Khakers <[email protected]>
  • Loading branch information
2 people authored and khakers committed Jun 18, 2024
1 parent e67f04c commit d904384
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s
- Fixed uncached member issue in large guild for react_to_contact and ticket creation.
- Fixed blocked roles improperly saving in `blocked_users` config.
- Fixed `?block` command improperly parsing reason as timestamp.
- Rate limit issue when fetch the messages due to reaction linking. ([PR #3306](https://github.com/modmail-dev/Modmail/pull/3306))

### Internal
- `ConfigManager.get` no longer accepts two positional arguments: the `convert` argument is now keyword-only.
Expand Down
40 changes: 25 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,33 +1247,44 @@ async def handle_reaction_events(self, payload):
return

channel = self.get_channel(payload.channel_id)
if not channel: # dm channel not in internal cache
_thread = await self.threads.find(recipient=user)
if not _thread:
thread = None
# dm channel not in internal cache
if not channel:
thread = await self.threads.find(recipient=user)
if not thread:
return
channel = await thread.recipient.create_dm()
if channel.id != payload.channel_id:
return

from_dm = isinstance(channel, discord.DMChannel)
from_txt = isinstance(channel, discord.TextChannel)
if not from_dm and not from_txt:
return

if not thread:
params = {"recipient": user} if from_dm else {"channel": channel}
thread = await self.threads.find(**params)
if not thread:
return
channel = await _thread.recipient.create_dm()

# thread must exist before doing this API call
try:
message = await channel.fetch_message(payload.message_id)
except (discord.NotFound, discord.Forbidden):
return

reaction = payload.emoji

close_emoji = await self.convert_emoji(self.config["close_emoji"])

if isinstance(channel, discord.DMChannel):
thread = await self.threads.find(recipient=user)
if not thread:
return
if from_dm:
if (
payload.event_type == "REACTION_ADD"
and message.embeds
and str(reaction) == str(close_emoji)
and self.config.get("recipient_thread_close")
):
ts = message.embeds[0].timestamp
if thread and ts == thread.channel.created_at:
if ts == thread.channel.created_at:
# the reacted message is the corresponding thread creation embed
# closing thread
return await thread.close(closer=user)
Expand All @@ -1293,11 +1304,10 @@ async def handle_reaction_events(self, payload):
logger.warning("Failed to find linked message for reactions: %s", e)
return
else:
thread = await self.threads.find(channel=channel)
if not thread:
return
try:
_, *linked_messages = await thread.find_linked_messages(message.id, either_direction=True)
_, *linked_messages = await thread.find_linked_messages(
message1=message, either_direction=True
)
except ValueError as e:
logger.warning("Failed to find linked message for reactions: %s", e)
return
Expand Down

0 comments on commit d904384

Please sign in to comment.