@@ -24,6 +24,7 @@ class SlashCommand:
24
24
:ivar req: :class:`.http.SlashCommandRequest` of this client.
25
25
:ivar logger: Logger of this client.
26
26
:ivar auto_register: Whether to register commands automatically.
27
+ :ivar has_listener: Whether discord client has listener add function.
27
28
"""
28
29
def __init__ (self ,
29
30
client : typing .Union [discord .Client , commands .Bot ],
@@ -37,11 +38,13 @@ def __init__(self,
37
38
self .auto_register = auto_register
38
39
if self .auto_register :
39
40
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 :
41
42
self .logger .info ("Detected discord.Client! Overriding on_socket_response." )
42
43
self ._discord .on_socket_response = self .on_socket_response
44
+ self .has_listener = False
43
45
else :
44
46
self ._discord .add_listener (self .on_socket_response )
47
+ self .has_listener = True
45
48
46
49
def remove (self ):
47
50
self ._discord .remove_listener (self .on_socket_response )
@@ -374,7 +377,10 @@ async def on_socket_response(self, msg):
374
377
args = await self .process_options (ctx .guild , to_use ["data" ]["options" ], selected_cmd ["auto_convert" ]) \
375
378
if "options" in to_use ["data" ] else []
376
379
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 )
378
384
379
385
async def handle_subcommand (self , ctx : model .SlashContext , data : dict ):
380
386
"""
@@ -403,10 +409,27 @@ async def handle_subcommand(self, ctx: model.SlashContext, data: dict):
403
409
args = await self .process_options (ctx .guild , x ["options" ], selected ["auto_convert" ]) \
404
410
if "options" in x .keys () else []
405
411
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 )
407
416
return
408
417
selected = base [sub_name ]
409
418
args = await self .process_options (ctx .guild , sub_opts , selected ["auto_convert" ]) \
410
419
if "options" in sub .keys () else []
411
420
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