Skip to content

Commit b1d2787

Browse files
Catalyst4222Catalyst4
andauthored
refactor: make Command.dispatch a function (#1040)
* refactor: make Command.dispatch a function * docs(docstrings): update the docstring for Command.dispatcher * docs(docstrings): fix the docstring for `command.dispatcher` Co-authored-by: Catalyst4 <catalyst4222@gmail.com>
1 parent ab243d2 commit b1d2787

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

interactions/client/bot.py

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

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

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

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

516-
coro = coro.__func__ if hasattr(coro, "__func__") else coro
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
517518

518519
coro._command_data = data
519520
coro._name = cmd.name
@@ -535,7 +536,7 @@ def __resolve_commands(self) -> None:
535536
else:
536537
self._scopes.add(cmd.scope if isinstance(cmd.scope, int) else cmd.scope.id)
537538

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

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

15951596
commands = self._commands.get(cmd.name, [])
15961597
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: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -702,58 +702,57 @@ def decorator(coro: Callable[..., Awaitable]) -> "Command":
702702

703703
return decorator
704704

705-
@property
706-
def dispatcher(self) -> Callable[..., Awaitable]:
707-
"""
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]
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]
715728
"""
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)
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,
731744
)
732-
if base_res() is StopCommand or isinstance(base_res(), StopCommand):
745+
if group_res() is StopCommand or isinstance(group_res(), StopCommand):
733746
return
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
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
757756

758757
def autocomplete(
759758
self, name: Optional[str] = MISSING

0 commit comments

Comments
 (0)