Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make Command.dispatch a function #1040

Merged
merged 3 commits into from Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions interactions/client/bot.py
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

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(
Catalyst4222 marked this conversation as resolved.
Show resolved Hide resolved
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