@@ -280,7 +280,7 @@ async def wait_until_ready(self):
280280 """Waits for the client to become ready according to the Gateway."""
281281 await self .ready .wait ()
282282
283- def _dispatch_event (self , event : str , data : dict ) -> None :
283+ def _dispatch_event (self , event : str , data : dict ) -> None : # sourcery no-metrics
284284 """
285285 Dispatches an event from the Gateway.
286286
@@ -292,96 +292,87 @@ def _dispatch_event(self, event: str, data: dict) -> None:
292292 path : str = "interactions"
293293 path += ".models" if event == "INTERACTION_CREATE" else ".api.models"
294294
295- if event != "TYPING_START" :
296- if event != "INTERACTION_CREATE" :
297- name : str = event .lower ()
298- try :
299- _event_path : list = [section .capitalize () for section in name .split ("_" )]
300- _name : str = (
301- _event_path [0 ] if len (_event_path ) < 3 else "" .join (_event_path [:- 1 ])
302- )
303- __obj : object = getattr (__import__ (path ), _name )
304-
305- if name in {"_create" , "_add" }:
306- data ["_client" ] = self ._http
307-
308- self ._dispatch .dispatch (f"on_{ name } " , __obj (** data )) # noqa
309- except AttributeError as error :
310- log .fatal (f"An error occured dispatching { name } : { error } " )
295+ if event == "INTERACTION_CREATE" :
296+ if not data .get ("type" ):
297+ log .warning (
298+ "Context is being created for the interaction, but no type is specified. Skipping..."
299+ )
311300 else :
312- if not data .get ("type" ):
313- log .warning (
314- "Context is being created for the interaction, but no type is specified. Skipping..."
315- )
316- else :
317- _context = self .__contextualize (data )
318- _name : str = ""
319- __args : list = [_context ]
320- __kwargs : dict = {}
321-
322- if data ["type" ] == InteractionType .APPLICATION_COMMAND :
323- _name = f"command_{ _context .data .name } "
324-
325- if _context .data ._json .get ("options" ):
326- for option in _context .data .options :
327- _type = self .__option_type_context (
328- _context ,
329- (
330- option ["type" ]
331- if isinstance (option , dict )
332- else option .type .value
333- ),
334- )
335- if _type :
336- if isinstance (option , dict ):
337- _type [option ["value" ]]._client = self ._http
338- option .update ({"value" : _type [option ["value" ]]})
339- else :
340- _type [option .value ]._client = self ._http
341- option ._json .update ({"value" : _type [option .value ]})
342- _option = self .__sub_command_context (option , _context )
343- __kwargs .update (_option )
344-
345- self ._dispatch .dispatch ("on_command" , _context )
346- elif data ["type" ] == InteractionType .MESSAGE_COMPONENT :
347- _name = f"component_{ _context .data .custom_id } "
348-
349- if _context .data ._json .get ("values" ):
350- __args .append (_context .data .values )
351-
352- self ._dispatch .dispatch ("on_component" , _context )
353- elif data ["type" ] == InteractionType .APPLICATION_COMMAND_AUTOCOMPLETE :
354- _name = f"autocomplete_{ _context .data .id } "
355-
356- if _context .data ._json .get ("options" ):
357- for option in _context .data .options :
358- __name , _value = self .__sub_command_context (option , _context )
359- _name += f"_{ __name } " if __name else ""
360-
361- if _value :
362- __args .append (_value )
363-
364- self ._dispatch .dispatch ("on_autocomplete" , _context )
365- elif data ["type" ] == InteractionType .MODAL_SUBMIT :
366- _name = f"modal_{ _context .data .custom_id } "
367-
368- if _context .data ._json .get ("components" ):
369- for component in _context .data .components :
370- if component .get ("components" ):
371- __args .append (
372- [_value ["value" ] for _value in component ["components" ]][0 ]
373- )
301+ # sourcery skip: extract-method
302+ _context = self .__contextualize (data )
303+ _name : str = ""
304+ __args : list = [_context ]
305+ __kwargs : dict = {}
306+
307+ if data ["type" ] == InteractionType .APPLICATION_COMMAND :
308+ _name = f"command_{ _context .data .name } "
309+
310+ if _context .data ._json .get ("options" ):
311+ for option in _context .data .options :
312+ _type = self .__option_type_context (
313+ _context ,
314+ (option ["type" ] if isinstance (option , dict ) else option .type .value ),
315+ )
316+ if _type :
317+ if isinstance (option , dict ):
318+ _type [option ["value" ]]._client = self ._http
319+ option .update ({"value" : _type [option ["value" ]]})
374320 else :
375- __args .append (
376- [_value .value for _value in component .components ][0 ]
377- )
378-
379- self ._dispatch .dispatch ("on_modal" , _context )
380-
381- self ._dispatch .dispatch (_name , * __args , ** __kwargs )
382- self ._dispatch .dispatch ("on_interaction" , _context )
383- self ._dispatch .dispatch ("on_interaction_create" , _context )
384-
321+ _type [option .value ]._client = self ._http
322+ option ._json .update ({"value" : _type [option .value ]})
323+ _option = self .__sub_command_context (option , _context )
324+ __kwargs .update (_option )
325+
326+ self ._dispatch .dispatch ("on_command" , _context )
327+ elif data ["type" ] == InteractionType .MESSAGE_COMPONENT :
328+ _name = f"component_{ _context .data .custom_id } "
329+
330+ if _context .data ._json .get ("values" ):
331+ __args .append (_context .data .values )
332+
333+ self ._dispatch .dispatch ("on_component" , _context )
334+ elif data ["type" ] == InteractionType .APPLICATION_COMMAND_AUTOCOMPLETE :
335+ _name = f"autocomplete_{ _context .data .id } "
336+
337+ if _context .data ._json .get ("options" ):
338+ for option in _context .data .options :
339+ __name , _value = self .__sub_command_context (option , _context )
340+ _name += f"_{ __name } " if __name else ""
341+
342+ if _value :
343+ __args .append (_value )
344+
345+ self ._dispatch .dispatch ("on_autocomplete" , _context )
346+ elif data ["type" ] == InteractionType .MODAL_SUBMIT :
347+ _name = f"modal_{ _context .data .custom_id } "
348+
349+ if _context .data ._json .get ("components" ):
350+ for component in _context .data .components :
351+ if component .get ("components" ):
352+ __args .append (
353+ [_value ["value" ] for _value in component ["components" ]][0 ]
354+ )
355+ else :
356+ __args .append ([_value .value for _value in component .components ][0 ])
357+
358+ self ._dispatch .dispatch ("on_modal" , _context )
359+
360+ self ._dispatch .dispatch (_name , * __args , ** __kwargs )
361+ self ._dispatch .dispatch ("on_interaction" , _context )
362+ self ._dispatch .dispatch ("on_interaction_create" , _context )
363+ elif event != "TYPING_START" :
364+ name : str = event .lower ()
365+ try :
366+ _event_path : list = [section .capitalize () for section in name .split ("_" )]
367+ _name : str = _event_path [0 ] if len (_event_path ) < 3 else "" .join (_event_path [:- 1 ])
368+ __obj : object = getattr (__import__ (path ), _name )
369+
370+ if name in {"_create" , "_add" }:
371+ data ["_client" ] = self ._http
372+
373+ self ._dispatch .dispatch (f"on_{ name } " , __obj (** data )) # noqa
374+ except AttributeError as error :
375+ log .fatal (f"An error occured dispatching { name } : { error } " )
385376 self ._dispatch .dispatch ("raw_socket_create" , data )
386377
387378 def __contextualize (self , data : dict ) -> object :
0 commit comments