@@ -167,9 +167,6 @@ async def __restart(self):
167
167
self .__task .cancel ()
168
168
await self ._client .close ()
169
169
self .__heartbeater .event .clear ()
170
- self ._closed = False
171
- self ._client = None
172
- self .__heartbeater .delay = 0.0
173
170
await self ._establish_connection ()
174
171
175
172
async def _establish_connection (
@@ -185,21 +182,25 @@ async def _establish_connection(
185
182
:param presence: The presence to carry with. Defaults to ``None``.
186
183
:type presence: Optional[ClientPresence]
187
184
"""
185
+ self ._client = None
186
+ self .__heartbeater .delay = 0.0
187
+ self ._closed = False
188
188
self ._options ["headers" ] = {"User-Agent" : self ._http ._req ._headers ["User-Agent" ]}
189
189
url = await self ._http .get_gateway ()
190
190
191
191
async with self ._http ._req ._session .ws_connect (url , ** self ._options ) as self ._client :
192
192
self ._closed = self ._client .closed
193
193
194
+ if self ._closed :
195
+ await self ._establish_connection ()
196
+
194
197
while not self ._closed :
195
198
stream = await self .__receive_packet_stream
196
199
197
200
if stream is None :
198
201
continue
199
202
if self ._client .close_code in range (4010 , 4014 ) or self ._client .close_code == 4004 :
200
203
raise GatewayException (self ._client .close_code )
201
- elif self ._closed : # Redundant conditional.
202
- await self ._establish_connection ()
203
204
204
205
await self ._handle_connection (stream , shard , presence )
205
206
@@ -394,80 +395,87 @@ def __contextualize(self, data: dict) -> object:
394
395
395
396
data ["client" ] = self ._http
396
397
context : object = getattr (__import__ ("interactions.context" ), _context )
398
+
397
399
return context (** data )
398
400
399
401
def __sub_command_context (
400
- self , data : Union [dict , Option ], _context : Optional [ object ] = MISSING
402
+ self , data : Union [dict , Option ], context : object
401
403
) -> Union [Tuple [str ], dict ]:
402
404
"""
403
405
Checks if an application command schema has sub commands
404
406
needed for argument collection.
405
407
406
408
:param data: The data structure of the option.
407
409
:type data: Union[dict, Option]
410
+ :param context: The context to refer subcommands from.
411
+ :type context: object
408
412
:return: A dictionary of the collected options, if any.
409
413
:rtype: Union[Tuple[str], dict]
410
414
"""
411
415
__kwargs : dict = {}
412
416
_data : dict = data ._json if isinstance (data , Option ) else data
413
417
414
418
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
424
425
if _data .get ("options" ):
425
426
if _data ["type" ] == OptionType .SUB_COMMAND :
426
427
__kwargs ["sub_command" ] = _data ["name" ]
428
+
427
429
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 ,
431
433
(
432
434
sub_option ["type" ]
433
435
if isinstance (sub_option , dict )
434
436
else sub_option .type .value
435
437
),
436
438
)
437
- if _option_context :
439
+
440
+ if _type :
438
441
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" ]]})
441
444
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
+
444
450
__kwargs [sub_option ["name" ]] = sub_option ["value" ]
445
451
elif _data ["type" ] == OptionType .SUB_COMMAND_GROUP :
446
452
__kwargs ["sub_command_group" ] = _data ["name" ]
447
453
for _group_option in _data ["options" ]:
448
454
_check_auto (_group_option )
449
455
__kwargs ["sub_command" ] = _group_option ["name" ]
456
+
450
457
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 ,
454
461
(
455
462
sub_option ["type" ]
456
463
if isinstance (sub_option , dict )
457
464
else sub_option .type .value
458
465
),
459
466
)
460
- if _option_context :
467
+
468
+ if _type :
461
469
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" ]]})
464
472
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
470
477
478
+ __kwargs [sub_option ["name" ]] = sub_option ["value" ]
471
479
elif _data .get ("value" ) and _data .get ("name" ):
472
480
__kwargs [_data ["name" ]] = _data ["value" ]
473
481
0 commit comments