@@ -24,6 +24,7 @@ class SlashCommand:
2424 :ivar req: :class:`.http.SlashCommandRequest` of this client.
2525 :ivar logger: Logger of this client.
2626 :ivar auto_register: Whether to register commands automatically.
27+ :ivar has_listener: Whether discord client has listener add function.
2728 """
2829 def __init__ (self ,
2930 client : typing .Union [discord .Client , commands .Bot ],
@@ -37,11 +38,13 @@ def __init__(self,
3738 self .auto_register = auto_register
3839 if self .auto_register :
3940 self ._discord .loop .create_task (self .register_all_commands ())
40- if not isinstance (client , commands .Bot ) or not isinstance (client , commands .AutoShardedBot ) and not override_type :
41+ if not isinstance (client , commands .Bot ) and not isinstance (client , commands .AutoShardedBot ) and not override_type :
4142 self .logger .info ("Detected discord.Client! Overriding on_socket_response." )
4243 self ._discord .on_socket_response = self .on_socket_response
44+ self .has_listener = False
4345 else :
4446 self ._discord .add_listener (self .on_socket_response )
47+ self .has_listener = True
4548
4649 def remove (self ):
4750 self ._discord .remove_listener (self .on_socket_response )
@@ -374,7 +377,10 @@ async def on_socket_response(self, msg):
374377 args = await self .process_options (ctx .guild , to_use ["data" ]["options" ], selected_cmd ["auto_convert" ]) \
375378 if "options" in to_use ["data" ] else []
376379 self ._discord .dispatch ("slash_command" , to_use ["data" ]["name" ], selected_cmd , ctx )
377- await selected_cmd ["func" ](ctx , * args )
380+ try :
381+ await selected_cmd ["func" ](ctx , * args )
382+ except Exception as ex :
383+ await self .on_slash_command_error (ctx , ex )
378384
379385 async def handle_subcommand (self , ctx : model .SlashContext , data : dict ):
380386 """
@@ -403,10 +409,27 @@ async def handle_subcommand(self, ctx: model.SlashContext, data: dict):
403409 args = await self .process_options (ctx .guild , x ["options" ], selected ["auto_convert" ]) \
404410 if "options" in x .keys () else []
405411 self ._discord .dispatch ("slash_command" , f"{ data ['data' ]['name' ]} { sub_name } { sub_group } " , selected , ctx )
406- await selected ["func" ](ctx , * args )
412+ try :
413+ await selected ["func" ](ctx , * args )
414+ except Exception as ex :
415+ await self .on_slash_command_error (ctx , ex )
407416 return
408417 selected = base [sub_name ]
409418 args = await self .process_options (ctx .guild , sub_opts , selected ["auto_convert" ]) \
410419 if "options" in sub .keys () else []
411420 self ._discord .dispatch ("slash_command" , f"{ data ['data' ]['name' ]} { sub_name } " , selected , ctx )
412- await selected ["func" ](ctx , * args )
421+ try :
422+ await selected ["func" ](ctx , * args )
423+ except Exception as ex :
424+ await self .on_slash_command_error (ctx , ex )
425+
426+ async def on_slash_command_error (self , ctx , ex ):
427+ if self .has_listener :
428+ if self ._discord .extra_events .get ('on_slash_command_error' ):
429+ self ._discord .dispatch ("slash_command_error" , ctx , ex )
430+ return
431+ if hasattr (self ._discord , "on_slash_command_error" ):
432+ self ._discord .dispatch ("slash_command_error" , ctx , ex )
433+ return
434+ # Prints exception if not overrided or has no listener for error.
435+ self .logger .exception (f"An exception has occurred while executing command `{ ctx .name } `:" )
0 commit comments