Skip to content

Commit

Permalink
Implement permissions decorator for cogs. (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 authored Jul 1, 2021
1 parent 5b9183e commit e4c7db8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions discord_slash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,11 @@ def wrapper(cmd):
def permission(self, guild_id: int, permissions: list):
"""
Decorator that add permissions. This will set the permissions for a single guild, you can use it more than once for each command.
:param guild_id: ID of the guild for the permissions.
:type guild_id: int
:param permissions: Permission requirements of the slash command. Default ``None``.
:type permissions: dict
:param permissions: List of permissions to be set for the specified guild.
:type permissions: list
"""

def wrapper(cmd):
Expand Down
31 changes: 31 additions & 0 deletions discord_slash/cog_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ async def ping(self, ctx: SlashContext):
:param connector: Kwargs connector for the command. Default ``None``.
:type connector: dict
"""
if not permissions:
permissions = {}

def wrapper(cmd):
decorator_permissions = getattr(cmd, "__permissions__", None)
if decorator_permissions:
permissions.update(decorator_permissions)

desc = description or inspect.getdoc(cmd)
if options is None:
opts = manage_commands.generate_options(cmd, desc, connector)
Expand Down Expand Up @@ -138,8 +144,14 @@ async def group_say(self, ctx: SlashContext, text: str):
base_description = base_description or base_desc
subcommand_group_description = subcommand_group_description or sub_group_desc
guild_ids = guild_ids if guild_ids else []
if not base_permissions:
base_permissions = {}

def wrapper(cmd):
decorator_permissions = getattr(cmd, "__permissions__", None)
if decorator_permissions:
base_permissions.update(decorator_permissions)

desc = description or inspect.getdoc(cmd)
if options is None:
opts = manage_commands.generate_options(cmd, desc, connector)
Expand Down Expand Up @@ -177,6 +189,25 @@ def wrapper(cmd):
return wrapper


def permission(guild_id: int, permissions: list):
"""
Decorator that add permissions. This will set the permissions for a single guild, you can use it more than once for each command.
:param guild_id: ID of the guild for the permissions.
:type guild_id: int
:param permissions: List of permissions to be set for the specified guild.
:type permissions: list
"""

def wrapper(cmd):
if not getattr(cmd, "__permissions__", None):
cmd.__permissions__ = {}
cmd.__permissions__[guild_id] = permissions
return cmd

return wrapper


def cog_component(
*,
messages: typing.Union[int, discord.Message, list] = None,
Expand Down

0 comments on commit e4c7db8

Please sign in to comment.