Skip to content

Commit 31cd41d

Browse files
krittickDorukyum
andauthored
Add permissions support to context menu commands (#631)
* add permissions support to ContextMenuCommand * Update discord/commands/commands.py Co-authored-by: Dorukyum <[email protected]> Co-authored-by: Dorukyum <[email protected]>
1 parent 758ff5f commit 31cd41d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

discord/commands/commands.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,14 @@ class ContextMenuCommand(ApplicationCommand):
946946
The coroutine that is executed when the command is called.
947947
guild_ids: Optional[List[:class:`int`]]
948948
The ids of the guilds where this command will be registered.
949+
default_permission: :class:`bool`
950+
Whether the command is enabled by default when it is added to a guild.
951+
permissions: List[:class:`.Permission`]
952+
The permissions for this command.
953+
954+
.. note::
955+
If this is not empty then default_permissions will be set to ``False``.
956+
949957
cog: Optional[:class:`Cog`]
950958
The cog that this command belongs to. ``None`` if there isn't one.
951959
checks: List[Callable[[:class:`.ApplicationContext`], :class:`bool`]]
@@ -990,8 +998,10 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
990998

991999
self.validate_parameters()
9921000

993-
# Context Menu commands don't have permissions
994-
self.permissions = []
1001+
self.default_permission = kwargs.get("default_permission", True)
1002+
self.permissions: List[Permission] = getattr(func, "__app_cmd_perms__", []) + kwargs.get("permissions", [])
1003+
if self.permissions and self.default_permission:
1004+
self.default_permission = False
9951005

9961006
# Context Menu commands can't have parents
9971007
self.parent = None
@@ -1034,7 +1044,7 @@ def qualified_name(self):
10341044
return self.name
10351045

10361046
def to_dict(self) -> Dict[str, Union[str, int]]:
1037-
return {"name": self.name, "description": self.description, "type": self.type}
1047+
return {"name": self.name, "description": self.description, "type": self.type, "default_permission": self.default_permission}
10381048

10391049

10401050
class UserCommand(ContextMenuCommand):

0 commit comments

Comments
 (0)