From f98df2a4276ab0c1fbc2e4df888c007d551d1947 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Fri, 26 Aug 2022 12:36:15 -0400 Subject: [PATCH 1/3] refactor: make Command.dispatch a function --- interactions/client/bot.py | 10 ++-- interactions/client/models/command.py | 74 ++++++++++++--------------- 2 files changed, 38 insertions(+), 46 deletions(-) diff --git a/interactions/client/bot.py b/interactions/client/bot.py index 838904c12..f1b2f2820 100644 --- a/interactions/client/bot.py +++ b/interactions/client/bot.py @@ -497,17 +497,18 @@ def __resolve_commands(self) -> None: ) data: Union[dict, List[dict]] = cmd.full_data - coro = cmd.dispatcher + dispatcher = cmd.dispatcher self.__check_command( command=ApplicationCommand(**(data[0] if isinstance(data, list) else data)), - coro=coro, + coro=dispatcher, ) if cmd.autocompletions: self.__id_autocomplete.update(cmd.autocompletions) - coro = coro.__func__ if hasattr(coro, "__func__") else coro + # weird interaction with methods, where they're a read-only version of their function + coro = dispatcher.__func__ if hasattr(dispatcher, "__func__") else dispatcher coro._command_data = data coro._name = cmd.name @@ -529,7 +530,7 @@ def __resolve_commands(self) -> None: else: self._scopes.add(cmd.scope if isinstance(cmd.scope, int) else cmd.scope.id) - self.event(coro, name=f"command_{cmd.name}") + self.event(dispatcher, name=f"command_{cmd.name}") cmd.resolved = True async def __sync(self) -> None: # sourcery no-metrics @@ -1584,7 +1585,6 @@ def __new__(cls, client: Client, *args, **kwargs) -> "Extension": commands = self._commands.get(cmd.name, []) coro = cmd.dispatcher - coro = coro.__func__ if hasattr(coro, "__func__") else coro commands.append(coro) self._commands[f"command_{cmd.name}"] = commands diff --git a/interactions/client/models/command.py b/interactions/client/models/command.py index 5ad7d3006..79c1f4fbf 100644 --- a/interactions/client/models/command.py +++ b/interactions/client/models/command.py @@ -702,8 +702,14 @@ def decorator(coro: Callable[..., Awaitable]) -> "Command": return decorator - @property - def dispatcher(self) -> Callable[..., Awaitable]: + async def dispatcher( + self, + ctx: "CommandContext", + *args, + sub_command_group: Optional[str] = None, + sub_command: Optional[str] = None, + **kwargs, + ) -> Optional[BaseResult]: """ Returns a coroutine that calls the command along with the subcommands, if any. @@ -713,47 +719,33 @@ def dispatcher(self) -> Callable[..., Awaitable]: :return: A coroutine that calls the command along with the subcommands, if any. :rtype: Callable[..., Awaitable] """ - if not self.has_subcommands: - return self.__wrap_coro(self.coro) - - @wraps(self.coro) - async def dispatch( - ctx: "CommandContext", - *args, - sub_command_group: Optional[str] = None, - sub_command: Optional[str] = None, - **kwargs, - ) -> Optional[Any]: - """Dispatches all of the subcommands of the command.""" - base_coro = self.coro - base_res = BaseResult( - result=await self.__call(base_coro, ctx, *args, _name=self.name, **kwargs) + base_coro = self.coro + base_res = BaseResult( + result=await self.__call(base_coro, ctx, *args, _name=self.name, **kwargs) + ) + if base_res() is StopCommand or isinstance(base_res(), StopCommand): + return + if sub_command_group: + group_coro = self.coroutines[sub_command_group] + name = f"{sub_command_group} {sub_command}" + subcommand_coro = self.coroutines[name] + group_res = GroupResult( + result=await self.__call( + group_coro, ctx, *args, _res=base_res, _name=sub_command_group, **kwargs + ), + parent=base_res, ) - if base_res() is StopCommand or isinstance(base_res(), StopCommand): + if group_res() is StopCommand or isinstance(group_res(), StopCommand): return - if sub_command_group: - group_coro = self.coroutines[sub_command_group] - name = f"{sub_command_group} {sub_command}" - subcommand_coro = self.coroutines[name] - group_res = GroupResult( - result=await self.__call( - group_coro, ctx, *args, _res=base_res, _name=sub_command_group, **kwargs - ), - parent=base_res, - ) - if group_res() is StopCommand or isinstance(group_res(), StopCommand): - return - return await self.__call( - subcommand_coro, ctx, *args, _res=group_res, _name=name, **kwargs - ) - elif sub_command: - subcommand_coro = self.coroutines[sub_command] - return await self.__call( - subcommand_coro, ctx, *args, _res=base_res, _name=sub_command, **kwargs - ) - return base_res - - return dispatch + return await self.__call( + subcommand_coro, ctx, *args, _res=group_res, _name=name, **kwargs + ) + elif sub_command: + subcommand_coro = self.coroutines[sub_command] + return await self.__call( + subcommand_coro, ctx, *args, _res=base_res, _name=sub_command, **kwargs + ) + return base_res def autocomplete( self, name: Optional[str] = MISSING From 7ad4dbabc64165a3404f71ef2ab4e39272f7a136 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Sat, 27 Aug 2022 11:30:40 -0400 Subject: [PATCH 2/3] docs(docstrings): update the docstring for Command.dispatcher --- interactions/client/models/command.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/interactions/client/models/command.py b/interactions/client/models/command.py index 79c1f4fbf..9b3690c9a 100644 --- a/interactions/client/models/command.py +++ b/interactions/client/models/command.py @@ -711,13 +711,17 @@ async def dispatcher( **kwargs, ) -> Optional[BaseResult]: """ - Returns a coroutine that calls the command along with the subcommands, if any. - - .. note:: - The coroutine returned is never the same object. - - :return: A coroutine that calls the command along with the subcommands, if any. - :rtype: Callable[..., Awaitable] + Call the command along with any subcommands + + :param ctx: The context for the interaction + :type ctx: CommandContext + :param args: The args to be passed to the command + :param sub_command_group: The subcommand group being invoked, if any + :type sub_command_group: Optional[str] + :param sub_command: The subcommand being invoked, if any + :type sub_command: Optional[str] + :param kwargs: The kwargs to pass to the command + :return: """ base_coro = self.coro base_res = BaseResult( From 592dac6e8a1a892e9402f90670e8ffa75ed68b49 Mon Sep 17 00:00:00 2001 From: Catalyst4 <84055084+Catalyst4222@users.noreply.github.com> Date: Sat, 3 Sep 2022 23:35:56 -0400 Subject: [PATCH 3/3] docs(docstrings): fix the docstring for `command.dispatcher` --- interactions/client/models/command.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/interactions/client/models/command.py b/interactions/client/models/command.py index 9b3690c9a..c326dc641 100644 --- a/interactions/client/models/command.py +++ b/interactions/client/models/command.py @@ -710,18 +710,21 @@ async def dispatcher( sub_command: Optional[str] = None, **kwargs, ) -> Optional[BaseResult]: - """ + r""" Call the command along with any subcommands :param ctx: The context for the interaction :type ctx: CommandContext :param args: The args to be passed to the command + :type args: tuple :param sub_command_group: The subcommand group being invoked, if any :type sub_command_group: Optional[str] :param sub_command: The subcommand being invoked, if any :type sub_command: Optional[str] :param kwargs: The kwargs to pass to the command - :return: + :type kwargs: Dict + :return: The result of the base command if no StopCommand is returned anywhere, else None + :rtype: Optional[BaseResult] """ base_coro = self.coro base_res = BaseResult(