@@ -167,9 +167,6 @@ async def __restart(self):
167167 self .__task .cancel ()
168168 await self ._client .close ()
169169 self .__heartbeater .event .clear ()
170- self ._closed = False
171- self ._client = None
172- self .__heartbeater .delay = 0.0
173170 await self ._establish_connection ()
174171
175172 async def _establish_connection (
@@ -185,21 +182,25 @@ async def _establish_connection(
185182 :param presence: The presence to carry with. Defaults to ``None``.
186183 :type presence: Optional[ClientPresence]
187184 """
185+ self ._client = None
186+ self .__heartbeater .delay = 0.0
187+ self ._closed = False
188188 self ._options ["headers" ] = {"User-Agent" : self ._http ._req ._headers ["User-Agent" ]}
189189 url = await self ._http .get_gateway ()
190190
191191 async with self ._http ._req ._session .ws_connect (url , ** self ._options ) as self ._client :
192192 self ._closed = self ._client .closed
193193
194+ if self ._closed :
195+ await self ._establish_connection ()
196+
194197 while not self ._closed :
195198 stream = await self .__receive_packet_stream
196199
197200 if stream is None :
198201 continue
199202 if self ._client .close_code in range (4010 , 4014 ) or self ._client .close_code == 4004 :
200203 raise GatewayException (self ._client .close_code )
201- elif self ._closed : # Redundant conditional.
202- await self ._establish_connection ()
203204
204205 await self ._handle_connection (stream , shard , presence )
205206
@@ -394,80 +395,87 @@ def __contextualize(self, data: dict) -> object:
394395
395396 data ["client" ] = self ._http
396397 context : object = getattr (__import__ ("interactions.context" ), _context )
398+
397399 return context (** data )
398400
399401 def __sub_command_context (
400- self , data : Union [dict , Option ], _context : Optional [ object ] = MISSING
402+ self , data : Union [dict , Option ], context : object
401403 ) -> Union [Tuple [str ], dict ]:
402404 """
403405 Checks if an application command schema has sub commands
404406 needed for argument collection.
405407
406408 :param data: The data structure of the option.
407409 :type data: Union[dict, Option]
410+ :param context: The context to refer subcommands from.
411+ :type context: object
408412 :return: A dictionary of the collected options, if any.
409413 :rtype: Union[Tuple[str], dict]
410414 """
411415 __kwargs : dict = {}
412416 _data : dict = data ._json if isinstance (data , Option ) else data
413417
414418 def _check_auto (option : dict ) -> Optional [Tuple [str ]]:
415- try :
416- if option .get ("focused" ):
417- return (option ["name" ], option ["value" ])
418- except AttributeError :
419- return False
420-
421- x = _check_auto (_data )
422- if x :
423- return x
419+ return (option ["name" ], option ["value" ]) if option .get ("focused" ) else None
420+
421+ check = _check_auto (_data )
422+
423+ if check :
424+ return check
424425 if _data .get ("options" ):
425426 if _data ["type" ] == OptionType .SUB_COMMAND :
426427 __kwargs ["sub_command" ] = _data ["name" ]
428+
427429 for sub_option in _data ["options" ]:
428- _check_auto (sub_option )
429- _option_context = self .__option_type_context (
430- _context ,
430+ _check = _check_auto (sub_option )
431+ _type = self .__option_type_context (
432+ context ,
431433 (
432434 sub_option ["type" ]
433435 if isinstance (sub_option , dict )
434436 else sub_option .type .value
435437 ),
436438 )
437- if _option_context :
439+
440+ if _type :
438441 if isinstance (sub_option , dict ):
439- _option_context [sub_option ["value" ]]._client = self ._http
440- sub_option .update ({"value" : _option_context [sub_option ["value" ]]})
442+ _type [sub_option ["value" ]]._client = self ._http
443+ sub_option .update ({"value" : _type [sub_option ["value" ]]})
441444 else :
442- _option_context [sub_option .value ]._client = self ._http
443- sub_option ._json .update ({"value" : _option_context [sub_option .value ]})
445+ _type [sub_option .value ]._client = self ._http
446+ sub_option ._json .update ({"value" : _type [sub_option .value ]})
447+ if _check :
448+ return _check
449+
444450 __kwargs [sub_option ["name" ]] = sub_option ["value" ]
445451 elif _data ["type" ] == OptionType .SUB_COMMAND_GROUP :
446452 __kwargs ["sub_command_group" ] = _data ["name" ]
447453 for _group_option in _data ["options" ]:
448454 _check_auto (_group_option )
449455 __kwargs ["sub_command" ] = _group_option ["name" ]
456+
450457 for sub_option in _group_option ["options" ]:
451- _check_auto (sub_option )
452- _option_context = self .__option_type_context (
453- _context ,
458+ _check = _check_auto (sub_option )
459+ _type = self .__option_type_context (
460+ context ,
454461 (
455462 sub_option ["type" ]
456463 if isinstance (sub_option , dict )
457464 else sub_option .type .value
458465 ),
459466 )
460- if _option_context :
467+
468+ if _type :
461469 if isinstance (sub_option , dict ):
462- _option_context [sub_option ["value" ]]._client = self ._http
463- sub_option .update ({"value" : _option_context [sub_option ["value" ]]})
470+ _type [sub_option ["value" ]]._client = self ._http
471+ sub_option .update ({"value" : _type [sub_option ["value" ]]})
464472 else :
465- _option_context [sub_option .value ]._client = self ._http
466- sub_option ._json .update (
467- {"value" : _option_context [sub_option .value ]}
468- )
469- __kwargs [sub_option ["name" ]] = sub_option ["value" ]
473+ _type [sub_option .value ]._client = self ._http
474+ sub_option ._json .update ({"value" : _type [sub_option .value ]})
475+ if _check :
476+ return _check
470477
478+ __kwargs [sub_option ["name" ]] = sub_option ["value" ]
471479 elif _data .get ("value" ) and _data .get ("name" ):
472480 __kwargs [_data ["name" ]] = _data ["value" ]
473481
0 commit comments