Skip to content

Commit f86b674

Browse files
Jerrie-AriesTaaku18
authored andcommitted
Fix rate limit issue on raw reaction add/remove events. (modmail-dev#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 <45324516+Taaku18@users.noreply.github.com> (cherry picked from commit ae99060) Signed-off-by: Khakers <22665282+khakers@users.noreply.github.com>
1 parent 3b95fb2 commit f86b674

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s
8080
- Fixed uncached member issue in large guild for react_to_contact and ticket creation.
8181
- Fixed blocked roles improperly saving in `blocked_users` config.
8282
- Fixed `?block` command improperly parsing reason as timestamp.
83+
- Rate limit issue when fetch the messages due to reaction linking. ([PR #3306](https://github.com/modmail-dev/Modmail/pull/3306))
8384

8485
### Internal
8586
- `ConfigManager.get` no longer accepts two positional arguments: the `convert` argument is now keyword-only.

bot.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -1247,33 +1247,44 @@ async def handle_reaction_events(self, payload):
12471247
return
12481248

12491249
channel = self.get_channel(payload.channel_id)
1250-
if not channel: # dm channel not in internal cache
1251-
_thread = await self.threads.find(recipient=user)
1252-
if not _thread:
1250+
thread = None
1251+
# dm channel not in internal cache
1252+
if not channel:
1253+
thread = await self.threads.find(recipient=user)
1254+
if not thread:
1255+
return
1256+
channel = await thread.recipient.create_dm()
1257+
if channel.id != payload.channel_id:
1258+
return
1259+
1260+
from_dm = isinstance(channel, discord.DMChannel)
1261+
from_txt = isinstance(channel, discord.TextChannel)
1262+
if not from_dm and not from_txt:
1263+
return
1264+
1265+
if not thread:
1266+
params = {"recipient": user} if from_dm else {"channel": channel}
1267+
thread = await self.threads.find(**params)
1268+
if not thread:
12531269
return
1254-
channel = await _thread.recipient.create_dm()
12551270

1271+
# thread must exist before doing this API call
12561272
try:
12571273
message = await channel.fetch_message(payload.message_id)
12581274
except (discord.NotFound, discord.Forbidden):
12591275
return
12601276

12611277
reaction = payload.emoji
1262-
12631278
close_emoji = await self.convert_emoji(self.config["close_emoji"])
1264-
1265-
if isinstance(channel, discord.DMChannel):
1266-
thread = await self.threads.find(recipient=user)
1267-
if not thread:
1268-
return
1279+
if from_dm:
12691280
if (
12701281
payload.event_type == "REACTION_ADD"
12711282
and message.embeds
12721283
and str(reaction) == str(close_emoji)
12731284
and self.config.get("recipient_thread_close")
12741285
):
12751286
ts = message.embeds[0].timestamp
1276-
if thread and ts == thread.channel.created_at:
1287+
if ts == thread.channel.created_at:
12771288
# the reacted message is the corresponding thread creation embed
12781289
# closing thread
12791290
return await thread.close(closer=user)
@@ -1293,11 +1304,10 @@ async def handle_reaction_events(self, payload):
12931304
logger.warning("Failed to find linked message for reactions: %s", e)
12941305
return
12951306
else:
1296-
thread = await self.threads.find(channel=channel)
1297-
if not thread:
1298-
return
12991307
try:
1300-
_, *linked_messages = await thread.find_linked_messages(message.id, either_direction=True)
1308+
_, *linked_messages = await thread.find_linked_messages(
1309+
message1=message, either_direction=True
1310+
)
13011311
except ValueError as e:
13021312
logger.warning("Failed to find linked message for reactions: %s", e)
13031313
return

0 commit comments

Comments
 (0)