Skip to content

Commit 2124227

Browse files
authored
Revert "refactor: make Command.dispatch a function (#1040)"
This reverts commit b1d2787.
1 parent 47c4e83 commit 2124227

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

interactions/client/bot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,17 @@ def __resolve_commands(self) -> None:
503503
)
504504

505505
data: Union[dict, List[dict]] = cmd.full_data
506-
dispatcher = cmd.dispatcher
506+
coro = cmd.dispatcher
507507

508508
self.__check_command(
509509
command=ApplicationCommand(**(data[0] if isinstance(data, list) else data)),
510-
coro=dispatcher,
510+
coro=coro,
511511
)
512512

513513
if cmd.autocompletions:
514514
self.__id_autocomplete.update(cmd.autocompletions)
515515

516-
# weird interaction with methods, where they're a read-only version of their function
517-
coro = dispatcher.__func__ if hasattr(dispatcher, "__func__") else dispatcher
516+
coro = coro.__func__ if hasattr(coro, "__func__") else coro
518517

519518
coro._command_data = data
520519
coro._name = cmd.name
@@ -536,7 +535,7 @@ def __resolve_commands(self) -> None:
536535
else:
537536
self._scopes.add(cmd.scope if isinstance(cmd.scope, int) else cmd.scope.id)
538537

539-
self.event(dispatcher, name=f"command_{cmd.name}")
538+
self.event(coro, name=f"command_{cmd.name}")
540539
cmd.resolved = True
541540

542541
async def __sync(self) -> None: # sourcery no-metrics
@@ -1595,6 +1594,7 @@ def __new__(cls, client: Client, *args, **kwargs) -> "Extension":
15951594

15961595
commands = self._commands.get(cmd.name, [])
15971596
coro = cmd.dispatcher
1597+
coro = coro.__func__ if hasattr(coro, "__func__") else coro
15981598
commands.append(coro)
15991599
self._commands[f"command_{cmd.name}"] = commands
16001600

interactions/client/models/command.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -702,57 +702,58 @@ def decorator(coro: Callable[..., Awaitable]) -> "Command":
702702

703703
return decorator
704704

705-
async def dispatcher(
706-
self,
707-
ctx: "CommandContext",
708-
*args,
709-
sub_command_group: Optional[str] = None,
710-
sub_command: Optional[str] = None,
711-
**kwargs,
712-
) -> Optional[BaseResult]:
713-
r"""
714-
Call the command along with any subcommands
715-
716-
:param ctx: The context for the interaction
717-
:type ctx: CommandContext
718-
:param args: The args to be passed to the command
719-
:type args: tuple
720-
:param sub_command_group: The subcommand group being invoked, if any
721-
:type sub_command_group: Optional[str]
722-
:param sub_command: The subcommand being invoked, if any
723-
:type sub_command: Optional[str]
724-
:param kwargs: The kwargs to pass to the command
725-
:type kwargs: Dict
726-
:return: The result of the base command if no StopCommand is returned anywhere, else None
727-
:rtype: Optional[BaseResult]
705+
@property
706+
def dispatcher(self) -> Callable[..., Awaitable]:
728707
"""
729-
base_coro = self.coro
730-
base_res = BaseResult(
731-
result=await self.__call(base_coro, ctx, *args, _name=self.name, **kwargs)
732-
)
733-
if base_res() is StopCommand or isinstance(base_res(), StopCommand):
734-
return
735-
if sub_command_group:
736-
group_coro = self.coroutines[sub_command_group]
737-
name = f"{sub_command_group} {sub_command}"
738-
subcommand_coro = self.coroutines[name]
739-
group_res = GroupResult(
740-
result=await self.__call(
741-
group_coro, ctx, *args, _res=base_res, _name=sub_command_group, **kwargs
742-
),
743-
parent=base_res,
708+
Returns a coroutine that calls the command along with the subcommands, if any.
709+
710+
.. note::
711+
The coroutine returned is never the same object.
712+
713+
:return: A coroutine that calls the command along with the subcommands, if any.
714+
:rtype: Callable[..., Awaitable]
715+
"""
716+
if not self.has_subcommands:
717+
return self.__wrap_coro(self.coro)
718+
719+
@wraps(self.coro)
720+
async def dispatch(
721+
ctx: "CommandContext",
722+
*args,
723+
sub_command_group: Optional[str] = None,
724+
sub_command: Optional[str] = None,
725+
**kwargs,
726+
) -> Optional[Any]:
727+
"""Dispatches all of the subcommands of the command."""
728+
base_coro = self.coro
729+
base_res = BaseResult(
730+
result=await self.__call(base_coro, ctx, *args, _name=self.name, **kwargs)
744731
)
745-
if group_res() is StopCommand or isinstance(group_res(), StopCommand):
732+
if base_res() is StopCommand or isinstance(base_res(), StopCommand):
746733
return
747-
return await self.__call(
748-
subcommand_coro, ctx, *args, _res=group_res, _name=name, **kwargs
749-
)
750-
elif sub_command:
751-
subcommand_coro = self.coroutines[sub_command]
752-
return await self.__call(
753-
subcommand_coro, ctx, *args, _res=base_res, _name=sub_command, **kwargs
754-
)
755-
return base_res
734+
if sub_command_group:
735+
group_coro = self.coroutines[sub_command_group]
736+
name = f"{sub_command_group} {sub_command}"
737+
subcommand_coro = self.coroutines[name]
738+
group_res = GroupResult(
739+
result=await self.__call(
740+
group_coro, ctx, *args, _res=base_res, _name=sub_command_group, **kwargs
741+
),
742+
parent=base_res,
743+
)
744+
if group_res() is StopCommand or isinstance(group_res(), StopCommand):
745+
return
746+
return await self.__call(
747+
subcommand_coro, ctx, *args, _res=group_res, _name=name, **kwargs
748+
)
749+
elif sub_command:
750+
subcommand_coro = self.coroutines[sub_command]
751+
return await self.__call(
752+
subcommand_coro, ctx, *args, _res=base_res, _name=sub_command, **kwargs
753+
)
754+
return base_res
755+
756+
return dispatch
756757

757758
def autocomplete(
758759
self, name: Optional[str] = MISSING

0 commit comments

Comments
 (0)