@@ -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