Skip to content

Commit

Permalink
fix: remove decorator check in InvokableUserCommand (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv authored Nov 26, 2024
1 parent 795316e commit f2594ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog/1252.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
|commands| Fix incorrect exception when using the :func:`~ext.commands.default_member_permissions` decorator on a :func:`~ext.commands.user_command` while also using the cog-level ``user_command_attrs`` field.
8 changes: 1 addition & 7 deletions disnake/ext/commands/ctx_menus_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,9 @@ def __init__(
self.auto_sync: bool = True if auto_sync is None else auto_sync

try:
default_perms: int = func.__default_member_permissions__
default_member_permissions = func.__default_member_permissions__
except AttributeError:
pass
else:
if default_member_permissions is not None:
raise ValueError(
"Cannot set `default_member_permissions` in both parameter and decorator"
)
default_member_permissions = default_perms

dm_permission = True if dm_permission is None else dm_permission

Expand Down
47 changes: 32 additions & 15 deletions tests/ext/commands/test_base_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
from disnake.ext import commands


class DecoratorMeta:
def __init__(self, type: str) -> None:
self.decorator = {
"slash": commands.slash_command,
"user": commands.user_command,
"message": commands.message_command,
}[type]
self.attr_key = f"{type}_command_attrs"


class TestDefaultPermissions:
def test_decorator(self) -> None:
@pytest.fixture(params=["slash", "user", "message"])
def meta(self, request):
return DecoratorMeta(request.param)

def test_decorator(self, meta: DecoratorMeta) -> None:
class Cog(commands.Cog):
@commands.slash_command(default_member_permissions=64)
@meta.decorator(default_member_permissions=64)
async def cmd(self, _) -> None:
...

@commands.default_member_permissions(64)
@commands.slash_command()
@meta.decorator()
async def above(self, _) -> None:
...

@commands.slash_command()
@meta.decorator()
@commands.default_member_permissions(64)
async def below(self, _) -> None:
...
Expand All @@ -29,52 +43,55 @@ async def below(self, _) -> None:
assert c.above.default_member_permissions == Permissions(64)
assert c.below.default_member_permissions == Permissions(64)

def test_decorator_overwrite(self) -> None:
def test_decorator_overwrite(self, meta: DecoratorMeta) -> None:
# putting the decorator above should fail
with pytest.raises(ValueError, match="Cannot set `default_member_permissions`"):

class Cog(commands.Cog):
@commands.default_member_permissions(32)
@commands.slash_command(default_member_permissions=64)
@meta.decorator(default_member_permissions=64)
async def above(self, _) -> None:
...

# putting the decorator below shouldn't fail
# (this is a side effect of how command copying works,
# putting the decorator below shouldn't fail, for now
# FIXME: (this is a side effect of how command copying works,
# and while this *should* probably fail, we're just testing
# for regressions for now)
class Cog2(commands.Cog):
@commands.slash_command(default_member_permissions=64)
@meta.decorator(default_member_permissions=64)
@commands.default_member_permissions(32)
async def below(self, _) -> None:
...

for c in (Cog2, Cog2()):
assert c.below.default_member_permissions == Permissions(32)

def test_attrs(self) -> None:
class Cog(commands.Cog, slash_command_attrs={"default_member_permissions": 32}):
@commands.slash_command()
def test_attrs(self, meta: DecoratorMeta) -> None:
kwargs = {meta.attr_key: {"default_member_permissions": 32}}

class Cog(commands.Cog, **kwargs):
@meta.decorator()
async def no_overwrite(self, _) -> None:
...

@commands.slash_command(default_member_permissions=64)
@meta.decorator(default_member_permissions=64)
async def overwrite(self, _) -> None:
...

@commands.default_member_permissions(64)
@commands.slash_command()
@meta.decorator()
async def overwrite_decorator_above(self, _) -> None:
...

@commands.slash_command()
@meta.decorator()
@commands.default_member_permissions(64)
async def overwrite_decorator_below(self, _) -> None:
...

assert Cog.no_overwrite.default_member_permissions is None
assert Cog().no_overwrite.default_member_permissions == Permissions(32)

# all of these should overwrite the cog-level attr
assert Cog.overwrite.default_member_permissions == Permissions(64)
assert Cog().overwrite.default_member_permissions == Permissions(64)

Expand Down

0 comments on commit f2594ee

Please sign in to comment.