Skip to content

Commit fd0b535

Browse files
EepyElvyrai0bs
andauthored
fix!: Correct gateway command and subcommand dispatching (#559)
* fix!: correct kwarg dictionary when dispatching a command * undo changes in dispatch * fix!: argument conversion for non-sub-commands * fix!: sub-command keyerror * fix!: _client for attributes and sub_command(_group) convertion * Update headers * Update gateway.py * fix!: add _client to slots * style: rename dunder Co-authored-by: fl0w <41456914+goverfl0w@users.noreply.github.com>
1 parent 862280c commit fd0b535

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

interactions/api/dispatch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def dispatch(self, __name: str, *args, **kwargs) -> None:
3333
:type \**kwargs: dict
3434
"""
3535
for event in self.events.get(__name, []):
36+
3637
self.loop.create_task(event(*args, **kwargs))
3738
log.debug(f"DISPATCH: {event}")
3839

interactions/api/gateway.py

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,23 @@ def _dispatch_event(self, event: str, data: dict) -> None:
310310

311311
if _context.data._json.get("options"):
312312
for option in _context.data.options:
313-
__kwargs.update(self.__sub_command_context(option))
314-
__kwargs.update(
315-
self.__option_type_context(
316-
_context,
317-
(
318-
option["type"]
319-
if isinstance(option, dict)
320-
else option.type.value
321-
),
322-
)
313+
_type = self.__option_type_context(
314+
_context,
315+
(
316+
option["type"]
317+
if isinstance(option, dict)
318+
else option.type.value
319+
),
323320
)
321+
if _type:
322+
if isinstance(option, dict):
323+
_type[option["value"]]._client = self._http
324+
option.update({"value": _type[option["value"]]})
325+
else:
326+
_type[option.value]._client = self._http
327+
option._json.update({"value": _type[option.value]})
328+
_option = self.__sub_command_context(option, _context)
329+
__kwargs.update(_option)
324330

325331
self._dispatch.dispatch("on_command", _context)
326332
elif data["type"] == InteractionType.MESSAGE_COMPONENT:
@@ -390,7 +396,9 @@ def __contextualize(self, data: dict) -> object:
390396
context: object = getattr(__import__("interactions.context"), _context)
391397
return context(**data)
392398

393-
def __sub_command_context(self, data: Union[dict, Option]) -> Union[Tuple[str], dict]:
399+
def __sub_command_context(
400+
self, data: Union[dict, Option], _context: Optional[object] = MISSING
401+
) -> Union[Tuple[str], dict]:
394402
"""
395403
Checks if an application command schema has sub commands
396404
needed for argument collection.
@@ -404,23 +412,62 @@ def __sub_command_context(self, data: Union[dict, Option]) -> Union[Tuple[str],
404412
_data: dict = data._json if isinstance(data, Option) else data
405413

406414
def _check_auto(option: dict) -> Optional[Tuple[str]]:
407-
if option.get("focused"):
408-
return (option["name"], option["value"])
415+
try:
416+
if option.get("focused"):
417+
return (option["name"], option["value"])
418+
except AttributeError:
419+
return False
409420

410421
x = _check_auto(_data)
411422
if x:
412423
return x
413424
if _data.get("options"):
414-
for option in _data["options"]:
415-
if option["type"] == OptionType.SUB_COMMAND:
416-
for sub_option in _data["options"]:
425+
if _data["type"] == OptionType.SUB_COMMAND:
426+
__kwargs["sub_command"] = _data["name"]
427+
for sub_option in _data["options"]:
428+
_check_auto(sub_option)
429+
_option_context = self.__option_type_context(
430+
_context,
431+
(
432+
sub_option["type"]
433+
if isinstance(sub_option, dict)
434+
else sub_option.type.value
435+
),
436+
)
437+
if _option_context:
438+
if isinstance(sub_option, dict):
439+
_option_context[sub_option["value"]]._client = self._http
440+
sub_option.update({"value": _option_context[sub_option["value"]]})
441+
else:
442+
_option_context[sub_option.value]._client = self._http
443+
sub_option._json.update({"value": _option_context[sub_option.value]})
444+
__kwargs[sub_option["name"]] = sub_option["value"]
445+
elif _data["type"] == OptionType.SUB_COMMAND_GROUP:
446+
__kwargs["sub_command_group"] = _data["name"]
447+
for _group_option in _data["options"]:
448+
_check_auto(_group_option)
449+
__kwargs["sub_command"] = _group_option["name"]
450+
for sub_option in _group_option["options"]:
417451
_check_auto(sub_option)
452+
_option_context = self.__option_type_context(
453+
_context,
454+
(
455+
sub_option["type"]
456+
if isinstance(sub_option, dict)
457+
else sub_option.type.value
458+
),
459+
)
460+
if _option_context:
461+
if isinstance(sub_option, dict):
462+
_option_context[sub_option["value"]]._client = self._http
463+
sub_option.update({"value": _option_context[sub_option["value"]]})
464+
else:
465+
_option_context[sub_option.value]._client = self._http
466+
sub_option._json.update(
467+
{"value": _option_context[sub_option.value]}
468+
)
418469
__kwargs[sub_option["name"]] = sub_option["value"]
419-
else:
420-
for group in _data["options"]:
421-
for _group_option in group:
422-
_check_auto(_group_option)
423-
__kwargs[_group_option["name"]] = _group_option["value"]
470+
424471
elif _data.get("value") and _data.get("name"):
425472
__kwargs[_data["name"]] = _data["value"]
426473

interactions/api/gateway.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class WebSocketClient:
6464
async def wait_until_ready(self) -> None: ...
6565
def _dispatch_event(self, event: str, data: dict) -> None: ...
6666
def __contextualize(self, data: dict) -> object: ...
67-
def __sub_command_context(self, data: Union[dict, Option]) -> Union[Tuple[str], dict]: ...
67+
def __sub_command_context(self, data: Union[dict, Option], _context: Optional[object] = MISSING) -> Union[Tuple[str], dict]: ...
6868
def __option_type_context(self, context: object, type: int) -> dict: ...
6969
@property
7070
async def __receive_packet_stream(self) -> Optional[Dict[str, Any]]: ...

interactions/api/models/gw.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,16 @@ class MessageReaction(DictSerializerMixin):
389389
:ivar Optional[Emoji] emoji?: The emoji of the event.
390390
"""
391391

392-
__slots__ = ("_json", "user_id", "channel_id", "message_id", "guild_id", "member", "emoji")
392+
__slots__ = (
393+
"_json",
394+
"_client",
395+
"user_id",
396+
"channel_id",
397+
"message_id",
398+
"guild_id",
399+
"member",
400+
"emoji",
401+
)
393402

394403
def __init__(self, **kwargs):
395404
super().__init__(**kwargs)
@@ -416,7 +425,7 @@ class ReactionRemove(MessageReaction):
416425
:ivar Optional[Emoji] emoji?: The emoji of the event.
417426
"""
418427

419-
__slots__ = ("_json", "user_id", "channel_id", "message_id", "guild_id", "emoji")
428+
__slots__ = ("_json", "_client", "user_id", "channel_id", "message_id", "guild_id", "emoji")
420429

421430
def __init__(self, **kwargs):
422431
super().__init__(**kwargs)

0 commit comments

Comments
 (0)