Skip to content

Commit ae99060

Browse files
Jerrie-AriesTaaku18
andauthoredNov 19, 2023
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>
1 parent 5c71059 commit ae99060

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed
 

‎bot.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -1222,33 +1222,44 @@ async def handle_reaction_events(self, payload):
12221222
return
12231223

12241224
channel = self.get_channel(payload.channel_id)
1225-
if not channel: # dm channel not in internal cache
1226-
_thread = await self.threads.find(recipient=user)
1227-
if not _thread:
1225+
thread = None
1226+
# dm channel not in internal cache
1227+
if not channel:
1228+
thread = await self.threads.find(recipient=user)
1229+
if not thread:
1230+
return
1231+
channel = await thread.recipient.create_dm()
1232+
if channel.id != payload.channel_id:
1233+
return
1234+
1235+
from_dm = isinstance(channel, discord.DMChannel)
1236+
from_txt = isinstance(channel, discord.TextChannel)
1237+
if not from_dm and not from_txt:
1238+
return
1239+
1240+
if not thread:
1241+
params = {"recipient": user} if from_dm else {"channel": channel}
1242+
thread = await self.threads.find(**params)
1243+
if not thread:
12281244
return
1229-
channel = await _thread.recipient.create_dm()
12301245

1246+
# thread must exist before doing this API call
12311247
try:
12321248
message = await channel.fetch_message(payload.message_id)
12331249
except (discord.NotFound, discord.Forbidden):
12341250
return
12351251

12361252
reaction = payload.emoji
1237-
12381253
close_emoji = await self.convert_emoji(self.config["close_emoji"])
1239-
1240-
if isinstance(channel, discord.DMChannel):
1241-
thread = await self.threads.find(recipient=user)
1242-
if not thread:
1243-
return
1254+
if from_dm:
12441255
if (
12451256
payload.event_type == "REACTION_ADD"
12461257
and message.embeds
12471258
and str(reaction) == str(close_emoji)
12481259
and self.config.get("recipient_thread_close")
12491260
):
12501261
ts = message.embeds[0].timestamp
1251-
if thread and ts == thread.channel.created_at:
1262+
if ts == thread.channel.created_at:
12521263
# the reacted message is the corresponding thread creation embed
12531264
# closing thread
12541265
return await thread.close(closer=user)
@@ -1268,11 +1279,10 @@ async def handle_reaction_events(self, payload):
12681279
logger.warning("Failed to find linked message for reactions: %s", e)
12691280
return
12701281
else:
1271-
thread = await self.threads.find(channel=channel)
1272-
if not thread:
1273-
return
12741282
try:
1275-
_, *linked_messages = await thread.find_linked_messages(message.id, either_direction=True)
1283+
_, *linked_messages = await thread.find_linked_messages(
1284+
message1=message, either_direction=True
1285+
)
12761286
except ValueError as e:
12771287
logger.warning("Failed to find linked message for reactions: %s", e)
12781288
return

0 commit comments

Comments
 (0)