@@ -390,6 +390,40 @@ async def restore_from_snooze(self):
390390 except Exception :
391391 logger .error ("Failed to recreate thread channel during unsnooze." , exc_info = True )
392392 return False
393+ # Helper to safely send to thread channel, recreating once if deleted
394+ async def _safe_send_to_channel (* , content = None , embeds = None , allowed_mentions = None ):
395+ nonlocal channel
396+ try :
397+ return await channel .send (
398+ content = content , embeds = embeds , allowed_mentions = allowed_mentions
399+ )
400+ except discord .NotFound :
401+ # Channel was deleted between restore and send; try to recreate once
402+ try :
403+ ow_map : dict = {}
404+ for role_id , perm_values in (self .snooze_data .get ("overwrites" , []) or []):
405+ target = guild .get_role (role_id ) or guild .get_member (role_id )
406+ if target is None :
407+ continue
408+ ow_map [target ] = discord .PermissionOverwrite (** perm_values )
409+ channel = await guild .create_text_channel (
410+ name = (self .snooze_data .get ("name" ) or f"thread-{ self .id } " ),
411+ category = orig_category ,
412+ overwrites = ow_map or None ,
413+ position = self .snooze_data .get ("position" ),
414+ topic = self .snooze_data .get ("topic" ),
415+ slowmode_delay = self .snooze_data .get ("slowmode_delay" ) or 0 ,
416+ nsfw = bool (self .snooze_data .get ("nsfw" )),
417+ reason = "Thread unsnoozed/restored (recreated after NotFound)" ,
418+ )
419+ self ._channel = channel
420+ return await channel .send (
421+ content = content , embeds = embeds , allowed_mentions = allowed_mentions
422+ )
423+ except Exception :
424+ logger .error ("Failed to recreate channel during unsnooze send." , exc_info = True )
425+ return None
426+
393427 # Strictly restore the log_key from snooze_data (never create a new one)
394428 self .log_key = self .snooze_data .get ("log_key" )
395429
@@ -439,7 +473,9 @@ async def restore_from_snooze(self):
439473 )
440474 except Exception :
441475 pass
442- await channel .send (embeds = embeds , allowed_mentions = discord .AllowedMentions .none ())
476+ await _safe_send_to_channel (
477+ embeds = embeds , allowed_mentions = discord .AllowedMentions .none ()
478+ )
443479 else :
444480 # Build a non-empty message; include attachment URLs if no content
445481 header = f"**{ username } ({ user_id } )**"
@@ -449,13 +485,15 @@ async def restore_from_snooze(self):
449485 formatted = header + "\n " + "\n " .join (attachments )
450486 else :
451487 formatted = header
452- await channel .send (formatted , allowed_mentions = discord .AllowedMentions .none ())
488+ await _safe_send_to_channel (
489+ content = formatted , allowed_mentions = discord .AllowedMentions .none ()
490+ )
453491 else :
454492 # Recipient message: include attachment URLs if content is empty
455493 content_to_send = (
456494 content if content else ("\n " .join (attachments ) if attachments else None )
457495 )
458- await channel . send (
496+ await _safe_send_to_channel (
459497 content = content_to_send ,
460498 embeds = embeds or None ,
461499 allowed_mentions = discord .AllowedMentions .none (),
@@ -490,7 +528,9 @@ async def restore_from_snooze(self):
490528 notify_channel = self .bot .config .get ("unsnooze_notify_channel" ) or "thread"
491529 notify_text = self .bot .config .get ("unsnooze_text" ) or "This thread has been unsnoozed and restored."
492530 if notify_channel == "thread" :
493- await channel .send (notify_text , allowed_mentions = discord .AllowedMentions .none ())
531+ await _safe_send_to_channel (
532+ content = notify_text , allowed_mentions = discord .AllowedMentions .none ()
533+ )
494534 else :
495535 ch = self .bot .get_channel (int (notify_channel ))
496536 if ch :
@@ -882,11 +922,13 @@ async def _close(self, closer, silent=False, delete_channel=True, message=None,
882922 tasks = [self .bot .config .update ()]
883923
884924 if self .bot .log_channel is not None and self .channel is not None :
885- if self .bot .config ["show_log_url_button" ]:
925+ # Only create a URL button if we actually have a valid log_url
926+ view = None
927+ if self .bot .config .get ("show_log_url_button" ) and log_url :
886928 view = discord .ui .View ()
887- view .add_item (discord . ui . Button ( label = "Log link" , url = log_url , style = discord . ButtonStyle . url ))
888- else :
889- view = None
929+ view .add_item (
930+ discord . ui . Button ( label = "Log link" , url = log_url , style = discord . ButtonStyle . url )
931+ )
890932 tasks .append (self .bot .log_channel .send (embed = embed , view = view ))
891933
892934 # Thread closed message
0 commit comments