Skip to content

Commit

Permalink
[commands] Fix localization support for hybrid group fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Aug 24, 2023
1 parent 16f6466 commit c7f6e95
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions discord/ext/commands/hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,18 @@ def replace_parameters(


class HybridAppCommand(discord.app_commands.Command[CogT, P, T]):
def __init__(self, wrapped: Union[HybridCommand[CogT, ..., T], HybridGroup[CogT, ..., T]]) -> None:
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 @@ -594,6 +598,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 +609,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 +637,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 @@ -654,8 +665,7 @@ def __init__(
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

0 comments on commit c7f6e95

Please sign in to comment.