Skip to content

Commit

Permalink
feat: provide (hopefully) helpful error message when trying to set ne…
Browse files Browse the repository at this point in the history
…w fields on subcommands
  • Loading branch information
shiftinv committed Oct 1, 2024
1 parent fbd95c8 commit 3ca6790
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions disnake/ext/commands/slash_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3ca6790

Please sign in to comment.