From e4c7db88e97420973ba72520633d39f9cb354bf7 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 1 Jul 2021 22:30:38 +0800 Subject: [PATCH] Implement permissions decorator for cogs. (#237) --- discord_slash/client.py | 6 +++--- discord_slash/cog_ext.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/discord_slash/client.py b/discord_slash/client.py index c5649444f..27dbe3914 100644 --- a/discord_slash/client.py +++ b/discord_slash/client.py @@ -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): diff --git a/discord_slash/cog_ext.py b/discord_slash/cog_ext.py index 6f4158e6f..c4cfa8644 100644 --- a/discord_slash/cog_ext.py +++ b/discord_slash/cog_ext.py @@ -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) @@ -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) @@ -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,