From 3ca6790e7bd6cbfc62408714007799cb10a1177d Mon Sep 17 00:00:00 2001 From: shiftinv Date: Tue, 1 Oct 2024 18:23:04 +0200 Subject: [PATCH] feat: provide (hopefully) helpful error message when trying to set new fields on subcommands --- disnake/ext/commands/slash_core.py | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index bf8ee52cf3..c19280061b 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -98,6 +98,23 @@ async def _call_autocompleter( return choices +_INVALID_SUB_KWARGS = frozenset( + {"dm_permission", "default_member_permissions", "integration_types", "contexts"} +) + + +# this is just a helpful message for users trying to set specific +# top-level-only fields on subcommands or groups +def _check_invalid_sub_kwargs(func: CommandCallback, kwargs: Dict[str, Any]) -> None: + invalid_keys = kwargs.keys() & _INVALID_SUB_KWARGS + if hasattr(func, "__default_member_permissions__"): + invalid_keys.add("default_member_permissions") + + if invalid_keys: + msg = f"Cannot set {utils.humanize_list(list(invalid_keys), 'or')} on subcommands or subcommand groups" + raise TypeError(msg) + + class SubCommandGroup(InvokableApplicationCommand): """A class that implements the protocol for a bot slash command group. @@ -157,15 +174,7 @@ def __init__( ) self.qualified_name: str = f"{parent.qualified_name} {self.name}" - # TODO - if ( - "dm_permission" in kwargs - or "default_member_permissions" in kwargs - or hasattr(func, "__default_member_permissions__") - ): - raise TypeError( - "Cannot set `default_member_permissions` or `dm_permission` on subcommand groups" - ) + _check_invalid_sub_kwargs(func, kwargs) @property def root_parent(self) -> InvokableSlashCommand: @@ -298,14 +307,7 @@ def __init__( ) self.qualified_name = f"{parent.qualified_name} {self.name}" - if ( - "dm_permission" in kwargs - or "default_member_permissions" in kwargs - or hasattr(func, "__default_member_permissions__") - ): - raise TypeError( - "Cannot set `default_member_permissions` or `dm_permission` on subcommands" - ) + _check_invalid_sub_kwargs(func, kwargs) @property def root_parent(self) -> InvokableSlashCommand: