Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
cop-discord committed Jun 20, 2024
1 parent 2d76513 commit bafadde
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ async def runner():
await self.start(token, reconnect=reconnect)

if log_handler is not None:
utils.setup_loguru(
utils.setup_logging(
handler=log_handler,
formatter=log_formatter,
level=log_level,
Expand Down
34 changes: 25 additions & 9 deletions discord/ext/commands/hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,20 @@ def replace_parameters(


class HybridAppCommand(discord.app_commands.Command[CogT, P, T]):
def __init__(self, wrapped: Union[HybridCommand[CogT, ..., T], HybridGroup[CogT, ..., T]]) -> None:
__commands_is_hybrid_app_command__: ClassVar[bool] = True

def __init__(
self,
wrapped: Union[HybridCommand[CogT, ..., T], HybridGroup[CogT, ..., T]],
name: Optional[Union[str, app_commands.locale_str]] = None,
) -> None:
signature = inspect.signature(wrapped.callback)
params = replace_parameters(wrapped.params, wrapped.callback, signature)
wrapped.callback.__signature__ = signature.replace(parameters=params)
nsfw = getattr(wrapped.callback, '__discord_app_commands_is_nsfw__', False)
try:
super().__init__(
name=wrapped._locale_name or wrapped.name,
name=name or wrapped._locale_name or wrapped.name,
callback=wrapped.callback, # type: ignore # Signature doesn't match but we're overriding the invoke
description=wrapped._locale_description or wrapped.description or wrapped.short_doc or '…',
nsfw=nsfw,
Expand Down Expand Up @@ -398,7 +404,7 @@ async def _check_can_run(self, interaction: discord.Interaction) -> bool:
if self.binding is not None:
try:
# Type checker does not like runtime attribute retrieval
check: AppCommandCheck = self.binding.interaction_check # type: ignore
check: AppCommandCheck = self.binding.interaction_check
except AttributeError:
pass
else:
Expand Down Expand Up @@ -594,6 +600,8 @@ class HybridGroup(Group[CogT, P, T]):
application command groups cannot be invoked, this creates a subcommand within
the group that can be invoked with the given group callback. If ``None``
then no fallback command is given. Defaults to ``None``.
fallback_locale: Optional[:class:`~discord.app_commands.locale_str`]
The fallback command name's locale string, if available.
"""

__commands_is_hybrid__: ClassVar[bool] = True
Expand All @@ -603,7 +611,7 @@ def __init__(
*args: Any,
name: Union[str, app_commands.locale_str] = MISSING,
description: Union[str, app_commands.locale_str] = MISSING,
fallback: Optional[str] = None,
fallback: Optional[Union[str, app_commands.locale_str]] = None,
**attrs: Any,
) -> None:
name, name_locale = (name.message, name) if isinstance(name, app_commands.locale_str) else (name, None)
Expand Down Expand Up @@ -631,7 +639,12 @@ def __init__(
# However, Python does not have conditional typing so it's very hard to
# make this type depend on the with_app_command bool without a lot of needless repetition
self.app_command: app_commands.Group = MISSING

fallback, fallback_locale = (
(fallback.message, fallback) if isinstance(fallback, app_commands.locale_str) else (fallback, None)
)
self.fallback: Optional[str] = fallback
self.fallback_locale: Optional[app_commands.locale_str] = fallback_locale

if self.with_app_command:
guild_ids = attrs.pop('guild_ids', None) or getattr(
Expand All @@ -640,22 +653,25 @@ def __init__(
guild_only = getattr(self.callback, '__discord_app_commands_guild_only__', False)
default_permissions = getattr(self.callback, '__discord_app_commands_default_permissions__', None)
nsfw = getattr(self.callback, '__discord_app_commands_is_nsfw__', False)
contexts = getattr(self.callback, '__discord_app_commands_contexts__', MISSING)
installs = getattr(self.callback, '__discord_app_commands_installation_types__', MISSING)
self.app_command = app_commands.Group(
name=self._locale_name or self.name,
description=self._locale_description or self.description or self.short_doc or '…',
guild_ids=guild_ids,
guild_only=guild_only,
default_permissions=default_permissions,
nsfw=nsfw,
allowed_installs=installs,
allowed_contexts=contexts,
)

# This prevents the group from re-adding the command at __init__
self.app_command.parent = parent
self.app_command.module = self.module

if fallback is not None:
command = HybridAppCommand(self)
command.name = fallback
command = HybridAppCommand(self, name=fallback_locale or fallback)
self.app_command.add_command(command)

@property
Expand Down Expand Up @@ -890,7 +906,7 @@ def hybrid_command(
def decorator(func: CommandCallback[CogT, ContextT, P, T]) -> HybridCommand[CogT, P, T]:
if isinstance(func, Command):
raise TypeError('Callback is already a command.')
return HybridCommand(func, name=name, with_app_command=with_app_command, **attrs) # type: ignore # ???
return HybridCommand(func, name=name, with_app_command=with_app_command, **attrs)

return decorator

Expand Down Expand Up @@ -920,9 +936,9 @@ def hybrid_group(
If the function is not a coroutine or is already a command.
"""

def decorator(func: CommandCallback[CogT, ContextT, P, T]):
def decorator(func: CommandCallback[CogT, ContextT, P, T]) -> HybridGroup[CogT, P, T]:
if isinstance(func, Command):
raise TypeError('Callback is already a command.')
return HybridGroup(func, name=name, with_app_command=with_app_command, **attrs)

return decorator # type: ignore
return decorator
2 changes: 1 addition & 1 deletion discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
'as_chunks',
'format_dt',
'MISSING',
'setup_loguru',
'setup_logging',
)

DISCORD_EPOCH = 1420070400000
Expand Down

0 comments on commit bafadde

Please sign in to comment.