@@ -1247,33 +1247,44 @@ async def handle_reaction_events(self, payload):
1247
1247
return
1248
1248
1249
1249
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 :
1253
1269
return
1254
- channel = await _thread .recipient .create_dm ()
1255
1270
1271
+ # thread must exist before doing this API call
1256
1272
try :
1257
1273
message = await channel .fetch_message (payload .message_id )
1258
1274
except (discord .NotFound , discord .Forbidden ):
1259
1275
return
1260
1276
1261
1277
reaction = payload .emoji
1262
-
1263
1278
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 :
1269
1280
if (
1270
1281
payload .event_type == "REACTION_ADD"
1271
1282
and message .embeds
1272
1283
and str (reaction ) == str (close_emoji )
1273
1284
and self .config .get ("recipient_thread_close" )
1274
1285
):
1275
1286
ts = message .embeds [0 ].timestamp
1276
- if thread and ts == thread .channel .created_at :
1287
+ if ts == thread .channel .created_at :
1277
1288
# the reacted message is the corresponding thread creation embed
1278
1289
# closing thread
1279
1290
return await thread .close (closer = user )
@@ -1293,11 +1304,10 @@ async def handle_reaction_events(self, payload):
1293
1304
logger .warning ("Failed to find linked message for reactions: %s" , e )
1294
1305
return
1295
1306
else :
1296
- thread = await self .threads .find (channel = channel )
1297
- if not thread :
1298
- return
1299
1307
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
+ )
1301
1311
except ValueError as e :
1302
1312
logger .warning ("Failed to find linked message for reactions: %s" , e )
1303
1313
return
0 commit comments