[commands] Fix decorator order mattering for hybrid commands

This commit is contained in:
Rapptz
2026-02-05 08:56:58 -05:00
parent a7d42b9906
commit 103fe90d8b
2 changed files with 66 additions and 44 deletions

View File

@@ -2180,8 +2180,9 @@ def describe(**parameters: Union[str, locale_str]) -> Callable[[T], T]:
''' '''
def decorator(inner: T) -> T: def decorator(inner: T) -> T:
if isinstance(inner, Command): unwrapped = getattr(inner, '__discord_app_commands_unwrap__', inner) or inner
_populate_descriptions(inner._params, parameters) if isinstance(unwrapped, Command):
_populate_descriptions(unwrapped._params, parameters)
else: else:
try: try:
inner.__discord_app_commands_param_description__.update(parameters) # type: ignore # Runtime attribute access inner.__discord_app_commands_param_description__.update(parameters) # type: ignore # Runtime attribute access
@@ -2223,8 +2224,9 @@ def rename(**parameters: Union[str, locale_str]) -> Callable[[T], T]:
""" """
def decorator(inner: T) -> T: def decorator(inner: T) -> T:
if isinstance(inner, Command): unwrapped = getattr(inner, '__discord_app_commands_unwrap__', inner) or inner
_populate_renames(inner._params, parameters) if isinstance(unwrapped, Command):
_populate_renames(unwrapped._params, parameters)
else: else:
try: try:
inner.__discord_app_commands_param_rename__.update(parameters) # type: ignore # Runtime attribute access inner.__discord_app_commands_param_rename__.update(parameters) # type: ignore # Runtime attribute access
@@ -2292,8 +2294,9 @@ def choices(**parameters: List[Choice[ChoiceT]]) -> Callable[[T], T]:
""" """
def decorator(inner: T) -> T: def decorator(inner: T) -> T:
if isinstance(inner, Command): unwrapped = getattr(inner, '__discord_app_commands_unwrap__', inner) or inner
_populate_choices(inner._params, parameters) if isinstance(unwrapped, Command):
_populate_choices(unwrapped._params, parameters)
else: else:
try: try:
inner.__discord_app_commands_param_choices__.update(parameters) # type: ignore # Runtime attribute access inner.__discord_app_commands_param_choices__.update(parameters) # type: ignore # Runtime attribute access
@@ -2351,8 +2354,9 @@ def autocomplete(**parameters: AutocompleteCallback[GroupT, ChoiceT]) -> Callabl
""" """
def decorator(inner: T) -> T: def decorator(inner: T) -> T:
if isinstance(inner, Command): unwrapped = getattr(inner, '__discord_app_commands_unwrap__', inner) or inner
_populate_autocomplete(inner._params, parameters) if isinstance(unwrapped, Command):
_populate_autocomplete(unwrapped._params, parameters)
else: else:
try: try:
inner.__discord_app_commands_param_autocomplete__.update(parameters) # type: ignore # Runtime attribute access inner.__discord_app_commands_param_autocomplete__.update(parameters) # type: ignore # Runtime attribute access
@@ -2408,13 +2412,14 @@ def guilds(*guild_ids: Union[Snowflake, int]) -> Callable[[T], T]:
defaults: List[int] = [g if isinstance(g, int) else g.id for g in guild_ids] defaults: List[int] = [g if isinstance(g, int) else g.id for g in guild_ids]
def decorator(inner: T) -> T: def decorator(inner: T) -> T:
if isinstance(inner, (Group, ContextMenu)): unwrapped = getattr(inner, '__discord_app_commands_unwrap__', inner) or inner
inner._guild_ids = defaults if isinstance(unwrapped, (Group, ContextMenu)):
elif isinstance(inner, Command): unwrapped._guild_ids = defaults
if inner.parent is not None: elif isinstance(unwrapped, Command):
if unwrapped.parent is not None:
raise ValueError('child commands of a group cannot have default guilds set') raise ValueError('child commands of a group cannot have default guilds set')
inner._guild_ids = defaults unwrapped._guild_ids = defaults
else: else:
# Runtime attribute assignment # Runtime attribute assignment
inner.__discord_app_commands_default_guilds__ = defaults # type: ignore inner.__discord_app_commands_default_guilds__ = defaults # type: ignore
@@ -2470,13 +2475,14 @@ def check(predicate: Check) -> Callable[[T], T]:
""" """
def decorator(func: CheckInputParameter) -> CheckInputParameter: def decorator(func: CheckInputParameter) -> CheckInputParameter:
if isinstance(func, (Command, ContextMenu)): unwrapped = getattr(func, '__discord_app_commands_unwrap__', func) or func
func.checks.append(predicate) if isinstance(unwrapped, (Command, ContextMenu)):
unwrapped.checks.append(predicate)
else: else:
if not hasattr(func, '__discord_app_commands_checks__'): if not hasattr(func, '__discord_app_commands_checks__'):
func.__discord_app_commands_checks__ = [] func.__discord_app_commands_checks__ = [] # type: ignore # Runtime attribute assignment
func.__discord_app_commands_checks__.append(predicate) func.__discord_app_commands_checks__.append(predicate) # type: ignore # Runtime attribute access
return func return func
@@ -2513,10 +2519,11 @@ def guild_only(func: Optional[T] = None) -> Union[T, Callable[[T], T]]:
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
f.guild_only = True if isinstance(unwrapped, (Command, Group, ContextMenu)):
allowed_contexts = f.allowed_contexts or AppCommandContext() unwrapped.guild_only = True
f.allowed_contexts = allowed_contexts allowed_contexts = unwrapped.allowed_contexts or AppCommandContext()
unwrapped.allowed_contexts = allowed_contexts
else: else:
f.__discord_app_commands_guild_only__ = True # type: ignore # Runtime attribute assignment f.__discord_app_commands_guild_only__ = True # type: ignore # Runtime attribute assignment
@@ -2567,10 +2574,11 @@ def private_channel_only(func: Optional[T] = None) -> Union[T, Callable[[T], T]]
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
f.guild_only = False if isinstance(unwrapped, (Command, Group, ContextMenu)):
allowed_contexts = f.allowed_contexts or AppCommandContext() unwrapped.guild_only = False
f.allowed_contexts = allowed_contexts allowed_contexts = unwrapped.allowed_contexts or AppCommandContext()
unwrapped.allowed_contexts = allowed_contexts
else: else:
allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext() allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext()
f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment
@@ -2617,10 +2625,11 @@ def dm_only(func: Optional[T] = None) -> Union[T, Callable[[T], T]]:
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
f.guild_only = False if isinstance(unwrapped, (Command, Group, ContextMenu)):
allowed_contexts = f.allowed_contexts or AppCommandContext() unwrapped.guild_only = False
f.allowed_contexts = allowed_contexts allowed_contexts = unwrapped.allowed_contexts or AppCommandContext()
unwrapped.allowed_contexts = allowed_contexts
else: else:
allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext() allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext()
f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment
@@ -2658,10 +2667,11 @@ def allowed_contexts(guilds: bool = MISSING, dms: bool = MISSING, private_channe
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
f.guild_only = False if isinstance(unwrapped, (Command, Group, ContextMenu)):
allowed_contexts = f.allowed_contexts or AppCommandContext() unwrapped.guild_only = False
f.allowed_contexts = allowed_contexts allowed_contexts = unwrapped.allowed_contexts or AppCommandContext()
unwrapped.allowed_contexts = allowed_contexts
else: else:
allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext() allowed_contexts = getattr(f, '__discord_app_commands_contexts__', None) or AppCommandContext()
f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment f.__discord_app_commands_contexts__ = allowed_contexts # type: ignore # Runtime attribute assignment
@@ -2709,9 +2719,10 @@ def guild_install(func: Optional[T] = None) -> Union[T, Callable[[T], T]]:
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
allowed_installs = f.allowed_installs or AppInstallationType() if isinstance(unwrapped, (Command, Group, ContextMenu)):
f.allowed_installs = allowed_installs allowed_installs = unwrapped.allowed_installs or AppInstallationType()
unwrapped.allowed_installs = allowed_installs
else: else:
allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType() allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType()
f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment
@@ -2757,9 +2768,10 @@ def user_install(func: Optional[T] = None) -> Union[T, Callable[[T], T]]:
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
allowed_installs = f.allowed_installs or AppInstallationType() if isinstance(unwrapped, (Command, Group, ContextMenu)):
f.allowed_installs = allowed_installs allowed_installs = unwrapped.allowed_installs or AppInstallationType()
unwrapped.allowed_installs = allowed_installs
else: else:
allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType() allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType()
f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment
@@ -2801,9 +2813,10 @@ def allowed_installs(
""" """
def inner(f: T) -> T: def inner(f: T) -> T:
if isinstance(f, (Command, Group, ContextMenu)): unwrapped = getattr(f, '__discord_app_commands_unwrap__', f) or f
allowed_installs = f.allowed_installs or AppInstallationType() if isinstance(unwrapped, (Command, Group, ContextMenu)):
f.allowed_installs = allowed_installs allowed_installs = unwrapped.allowed_installs or AppInstallationType()
unwrapped.allowed_installs = allowed_installs
else: else:
allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType() allowed_installs = getattr(f, '__discord_app_commands_installation_types__', None) or AppInstallationType()
f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment f.__discord_app_commands_installation_types__ = allowed_installs # type: ignore # Runtime attribute assignment
@@ -2874,8 +2887,9 @@ def default_permissions(perms_obj: Optional[Permissions] = None, /, **perms: Unp
permissions = Permissions(**perms) permissions = Permissions(**perms)
def decorator(func: T) -> T: def decorator(func: T) -> T:
if isinstance(func, (Command, Group, ContextMenu)): unwrapped = getattr(func, '__discord_app_commands_unwrap__', func) or func
func.default_permissions = permissions if isinstance(unwrapped, (Command, Group, ContextMenu)):
unwrapped.default_permissions = permissions
else: else:
func.__discord_app_commands_default_permissions__ = permissions # type: ignore # Runtime attribute assignment func.__discord_app_commands_default_permissions__ = permissions # type: ignore # Runtime attribute assignment

View File

@@ -534,6 +534,10 @@ class HybridCommand(Command[CogT, P, T]):
HybridAppCommand(self) if self.with_app_command else None HybridAppCommand(self) if self.with_app_command else None
) )
@property
def __discord_app_commands_unwrap__(self) -> Optional[HybridAppCommand[CogT, Any, T]]:
return self.app_command
@property @property
def cog(self) -> CogT: def cog(self) -> CogT:
return self._cog return self._cog
@@ -702,6 +706,10 @@ class HybridGroup(Group[CogT, P, T]):
return None return None
return self.app_command.get_command(self.fallback) # type: ignore return self.app_command.get_command(self.fallback) # type: ignore
@property
def __discord_app_commands_unwrap__(self) -> Optional[app_commands.Group]:
return self.app_command
@property @property
def cog(self) -> CogT: def cog(self) -> CogT:
return self._cog return self._cog