Skip to content

Commit

Permalink
refactor: make Command.dispatch a function (#1040)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Catalyst4222 and Catalyst4 committed Sep 4, 2022
1 parent ab243d2 commit b1d2787
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
10 changes: 5 additions & 5 deletions interactions/client/bot.py
Expand Up @@ -503,17 +503,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
Expand All @@ -535,7 +536,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
Expand Down Expand Up @@ -1594,7 +1595,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

Expand Down
97 changes: 48 additions & 49 deletions interactions/client/models/command.py
Expand Up @@ -702,58 +702,57 @@ def decorator(coro: Callable[..., Awaitable]) -> "Command":

return decorator

@property
def dispatcher(self) -> Callable[..., Awaitable]:
"""
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]
async def dispatcher(
self,
ctx: "CommandContext",
*args,
sub_command_group: Optional[str] = None,
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
:type kwargs: Dict
:return: The result of the base command if no StopCommand is returned anywhere, else None
:rtype: Optional[BaseResult]
"""
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
Expand Down

0 comments on commit b1d2787

Please sign in to comment.