@@ -1222,33 +1222,44 @@ async def handle_reaction_events(self, payload):
1222
1222
return
1223
1223
1224
1224
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 :
1228
1244
return
1229
- channel = await _thread .recipient .create_dm ()
1230
1245
1246
+ # thread must exist before doing this API call
1231
1247
try :
1232
1248
message = await channel .fetch_message (payload .message_id )
1233
1249
except (discord .NotFound , discord .Forbidden ):
1234
1250
return
1235
1251
1236
1252
reaction = payload .emoji
1237
-
1238
1253
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 :
1244
1255
if (
1245
1256
payload .event_type == "REACTION_ADD"
1246
1257
and message .embeds
1247
1258
and str (reaction ) == str (close_emoji )
1248
1259
and self .config .get ("recipient_thread_close" )
1249
1260
):
1250
1261
ts = message .embeds [0 ].timestamp
1251
- if thread and ts == thread .channel .created_at :
1262
+ if ts == thread .channel .created_at :
1252
1263
# the reacted message is the corresponding thread creation embed
1253
1264
# closing thread
1254
1265
return await thread .close (closer = user )
@@ -1268,11 +1279,10 @@ async def handle_reaction_events(self, payload):
1268
1279
logger .warning ("Failed to find linked message for reactions: %s" , e )
1269
1280
return
1270
1281
else :
1271
- thread = await self .threads .find (channel = channel )
1272
- if not thread :
1273
- return
1274
1282
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
+ )
1276
1286
except ValueError as e :
1277
1287
logger .warning ("Failed to find linked message for reactions: %s" , e )
1278
1288
return
0 commit comments