@@ -131,7 +131,7 @@ def ready(self, flag: bool) -> None:
131131 self ._ready_event .set ()
132132 try :
133133 self .bot .dispatch ("thread_create" , self )
134- except Exception as e :
134+ except ( RuntimeError , TypeError ) as e :
135135 logger .warning ("Error dispatching thread_create: %s" , e )
136136 else :
137137 self ._ready_event .clear ()
@@ -167,7 +167,7 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
167167 if snoozed_cat_id :
168168 try :
169169 target_category = self .bot .modmail_guild .get_channel (int (snoozed_cat_id ))
170- except Exception :
170+ except ( ValueError , AttributeError ) :
171171 target_category = None
172172 if isinstance (target_category , discord .CategoryChannel ):
173173 try :
@@ -178,7 +178,7 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
178178 self .id ,
179179 )
180180 return False
181- except Exception :
181+ except ( AttributeError , TypeError ) :
182182 # If we cannot determine channel count, proceed; downstream will handle errors
183183 pass
184184 # Ensure self.log_key is set before snoozing
@@ -264,7 +264,7 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
264264 if snoozed_cat_id :
265265 try :
266266 target_category = guild .get_channel (int (snoozed_cat_id ))
267- except Exception :
267+ except ( ValueError , AttributeError ) :
268268 target_category = None
269269 # If no valid snooze category is configured, create one automatically
270270 if not isinstance (target_category , discord .CategoryChannel ):
@@ -293,9 +293,9 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
293293 try :
294294 await self .bot .config .set ("snoozed_category_id" , target_category .id )
295295 await self .bot .config .update ()
296- except Exception :
296+ except ( AttributeError , RuntimeError ) :
297297 logger .warning ("Failed to persist snoozed_category_id after auto-creation." )
298- except Exception as e :
298+ except ( discord . Forbidden , discord . HTTPException ) as e :
299299 logger .warning (
300300 "Failed to auto-create snoozed category (%s). Falling back to current category." ,
301301 e ,
@@ -312,7 +312,7 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
312312 self ._channel = channel
313313 # mark in snooze data that this was a move-based snooze
314314 self .snooze_data ["moved" ] = True
315- except Exception as e :
315+ except ( discord . Forbidden , discord . HTTPException ) as e :
316316 logger .warning (
317317 "Failed to move channel to snoozed category: %s. Falling back to delete." ,
318318 e ,
@@ -387,11 +387,11 @@ async def restore_from_snooze(self):
387387 ow_map [target ] = discord .PermissionOverwrite (** perm_values )
388388 if ow_map :
389389 await self .channel .edit (overwrites = ow_map , reason = "Restore original overwrites" )
390- except Exception as e :
390+ except ( discord . Forbidden , discord . HTTPException , ValueError ) as e :
391391 logger .warning ("Failed to restore original overwrites on unsnooze: %s" , e )
392392
393393 channel = self .channel
394- except Exception as e :
394+ except ( discord . Forbidden , discord . HTTPException ) as e :
395395 logger .warning ("Failed to move snoozed channel back, recreating: %s" , e )
396396 channel = None
397397
@@ -417,7 +417,7 @@ async def restore_from_snooze(self):
417417 reason = "Thread unsnoozed/restored (recreated)" ,
418418 )
419419 self ._channel = channel
420- except Exception :
420+ except ( discord . Forbidden , discord . HTTPException ) :
421421 logger .error ("Failed to recreate thread channel during unsnooze." , exc_info = True )
422422 return False
423423
@@ -452,7 +452,7 @@ async def _safe_send_to_channel(*, content=None, embeds=None, allowed_mentions=N
452452 embeds = embeds ,
453453 allowed_mentions = allowed_mentions ,
454454 )
455- except Exception :
455+ except ( discord . Forbidden , discord . HTTPException ) :
456456 logger .error (
457457 "Failed to recreate channel during unsnooze send." ,
458458 exc_info = True ,
@@ -466,7 +466,7 @@ async def _ensure_genesis(force: bool = False):
466466 nonlocal genesis_already_sent
467467 try :
468468 existing = await self .get_genesis_message ()
469- except Exception :
469+ except ( discord . NotFound , discord . Forbidden ) :
470470 existing = None
471471 if existing is None or force :
472472 # Build log_url and log_count best-effort
@@ -483,14 +483,14 @@ async def _ensure_genesis(force: bool = False):
483483 try :
484484 logs = await self .bot .api .get_user_logs (self .id )
485485 log_count = sum (1 for log in logs if not log .get ("open" ))
486- except Exception :
486+ except ( RuntimeError , AttributeError ) :
487487 log_count = None
488488 # Resolve recipient object
489489 user = self .recipient
490490 if user is None :
491491 try :
492492 user = await self .bot .get_or_fetch_user (self .id )
493- except Exception :
493+ except discord . NotFound :
494494 user = SimpleNamespace (
495495 id = self .id ,
496496 mention = f"<@{ self .id } >" ,
@@ -501,11 +501,11 @@ async def _ensure_genesis(force: bool = False):
501501 msg = await channel .send (embed = info_embed )
502502 try :
503503 await msg .pin ()
504- except Exception as e :
504+ except ( discord . Forbidden , discord . HTTPException ) as e :
505505 logger .warning ("Failed to pin genesis message during unsnooze: %s" , e )
506506 self ._genesis_message = msg
507507 genesis_already_sent = True
508- except Exception :
508+ except ( discord . Forbidden , discord . HTTPException ) :
509509 logger .warning ("Failed to send genesis message during unsnooze." , exc_info = True )
510510
511511 # If we recreated the channel, force-send genesis; if moved back, ensure it's present
@@ -514,7 +514,7 @@ async def _ensure_genesis(force: bool = False):
514514 await _ensure_genesis (force = False )
515515 else :
516516 await _ensure_genesis (force = True )
517- except Exception :
517+ except ( discord . Forbidden , discord . HTTPException ) :
518518 logger .debug ("Genesis ensure step encountered an error." )
519519
520520 # Strictly restore the log_key from snooze_data (never create a new one)
@@ -823,7 +823,7 @@ async def setup(self, *, creator=None, category=None, initial_message=None):
823823 )
824824
825825 log_count = sum (1 for log in log_data if not log ["open" ])
826- except Exception :
826+ except ( RuntimeError , AttributeError ) :
827827 logger .error ("An error occurred while posting logs to the database." , exc_info = True )
828828 log_url = log_count = None
829829 # ensure core functionality still works
@@ -851,12 +851,12 @@ async def send_genesis_message():
851851 if opt .get ("type" ) == "command" :
852852 log_txt += f" -> { opt .get ('callback' )} "
853853 await channel .send (embed = discord .Embed (description = log_txt , color = self .bot .mod_color ))
854- except Exception :
854+ except ( discord . Forbidden , discord . HTTPException ) :
855855 logger .warning (
856856 "Failed logging thread-creation menu selection" ,
857857 exc_info = True ,
858858 )
859- except Exception :
859+ except ( discord . Forbidden , discord . HTTPException ) :
860860 logger .error ("Failed unexpectedly:" , exc_info = True )
861861
862862 async def send_recipient_genesis_message ():
@@ -871,7 +871,7 @@ async def send_recipient_genesis_message():
871871 close_emoji = self .bot .config ["close_emoji" ]
872872 close_emoji = await self .bot .convert_emoji (close_emoji )
873873 await self .bot .add_reaction (initial_message , close_emoji )
874- except Exception as e :
874+ except ( discord . Forbidden , discord . HTTPException ) as e :
875875 logger .info ("Failed to add self-close reaction to initial message: %s" , e )
876876 return
877877
@@ -1078,7 +1078,7 @@ async def _close(self, closer, silent=False, delete_channel=True, message=None,
10781078 # with the menu after the thread is closed.
10791079 try :
10801080 await self ._disable_dm_creation_menu ()
1081- except Exception :
1081+ except ( discord . Forbidden , discord . HTTPException ) :
10821082 # Non-fatal; continue closing even if we can't edit the DM menu
10831083 pass
10841084 if self .channel :
@@ -1249,18 +1249,16 @@ async def _disable_dm_creation_menu(self) -> None:
12491249 msg = await dm .fetch_message (self ._dm_menu_msg_id )
12501250 except (discord .NotFound , discord .Forbidden ):
12511251 return
1252- except Exception :
1253- return
12541252 try :
12551253 closed_text = "This thread has been closed. Send a new message to start a new thread." # Grammar-friendly guidance
12561254 closed_embed = discord .Embed (description = closed_text )
12571255 await msg .edit (content = None , embed = closed_embed , view = None )
1258- except Exception as e :
1256+ except ( discord . Forbidden , discord . HTTPException ) as e :
12591257 # Fallback: at least remove interaction so menu cannot be used
12601258 logger .warning ("Failed editing DM menu message on close: %s" , e )
12611259 try :
12621260 await msg .edit (view = None )
1263- except Exception as inner_e :
1261+ except ( discord . Forbidden , discord . HTTPException ) as inner_e :
12641262 logger .debug ("Failed removing view from DM menu message: %s" , inner_e )
12651263
12661264 async def cancel_closure (self , auto_close : bool = False , all : bool = False ) -> None :
@@ -2120,7 +2118,7 @@ def lottie_to_png(data):
21202118 ):
21212119 try :
21222120 await message .delete ()
2123- except Exception as e :
2121+ except ( discord . Forbidden , discord . HTTPException ) as e :
21242122 logger .warning ("Cannot delete message: %s." , e )
21252123
21262124 if (
@@ -2149,7 +2147,7 @@ def lottie_to_png(data):
21492147 else :
21502148 logger .warning ("Channel not found." )
21512149 raise
2152- except (discord .Forbidden , discord .HTTPException , Exception ) as e :
2150+ except (discord .Forbidden , discord .HTTPException ) as e :
21532151 logger .warning (
21542152 "Unable to send typing to %s: %s. Continuing without typing." ,
21552153 destination ,
@@ -2176,7 +2174,7 @@ def lottie_to_png(data):
21762174 for att in message .attachments :
21772175 try :
21782176 files .append (await att .to_file ())
2179- except Exception :
2177+ except ( discord . Forbidden , discord . HTTPException ) :
21802178 logger .warning ("Failed to attach file in plain DM." , exc_info = True )
21812179
21822180 msg = await destination .send (plain_message , files = files or None )
@@ -2431,7 +2429,7 @@ async def find(
24312429 )
24322430 self .cache .pop (thread .id , None )
24332431 thread = None
2434- except Exception :
2432+ except ( AttributeError , KeyError ) :
24352433 # If any attribute access fails, be safe and drop it.
24362434 self .cache .pop (getattr (thread , "id" , None ), None )
24372435 thread = None
0 commit comments