Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions interactions/client/bot.py
Original file line number Diff line number Diff line change
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
74 changes: 33 additions & 41 deletions interactions/client/models/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down