Skip to content

Commit

Permalink
Merge pull request #66: Command Group Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
breqdev authored Dec 22, 2021
2 parents 3c1a3fe + 7bec702 commit 0b50912
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
21 changes: 21 additions & 0 deletions docs/permissions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ to specify any overwrites.
return "You have permissions!"
Subcommands and Command Groups
------------------------------

Discord only supports attaching permissions overwrites to top-level commands.
Thus, there are no ``default_permission`` or ``permissions`` parameters for the
:meth:`.SlashCommandGroup.command` decorator. However, you can still set
permissions for an entire tree of subcommands using the
:meth:`.DiscordInteractions.command_group` function.

.. code-block:: python
group = discord.command_group("group", default_permission=False, permissions=[
Permission(role="786840072891662336")
])
@group.command()
def locked_subcommand(ctx):
"Locked subcommand"
return "You have unlocked the secret subcommand!"
Context object
--------------

Expand Down
21 changes: 21 additions & 0 deletions examples/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ def unlock_command(ctx):
return "Unlocked!"


# Command groups can have permissions at the top level

group = discord.command_group("group", default_permission=False, permissions=[
Permission(role="786840072891662336")
])

@group.command()
def locked_subcommand(ctx):
"Locked subcommand"

return "You have unlocked the secret subcommand!"

@group.command()
def lock_me_out(ctx):
"Lock me out of this group"

ctx.overwrite_permissions([Permission(user=ctx.author.id, allow=False)])

return "Locked!"


discord.set_route("/interactions")


Expand Down
31 changes: 31 additions & 0 deletions flask_discord_interactions/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,37 @@ def run(self, context, *subcommands, **kwargs):


class SlashCommandGroup(SlashCommandSubgroup):
"""
Represents a Subgroup of slash commands.
Attributes
----------
name
The name of this subgroup, shown in the Discord client.
description
The description of this subgroup, shown in the Discord client.
is_async
Whether the subgroup should be considered async (if subcommands
get an :class:`AsyncContext` instead of a :class:`Context`.)
default_permission
Whether the subgroup is enabled by default. Default is True.
permissions
List of permission overwrites. These apply to all subcommands of this
group.
"""

def __init__(self, name, description, is_async=False,
default_permission=True, permissions=None):
self.name = name
self.description = description
self.subcommands = {}
self.type = ApplicationCommandType.CHAT_INPUT

self.default_permission = default_permission
self.permissions = permissions or []

self.is_async = is_async

def subgroup(self, name, description="No description", is_async=False):
"""
Create a new :class:`SlashCommandSubroup`
Expand Down
10 changes: 8 additions & 2 deletions flask_discord_interactions/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def decorator(func):

return decorator

def command_group(self, name, description="No description", is_async=False):
def command_group(self, name, description="No description", is_async=False,
default_permission=None, permissions=None):
"""
Create a new :class:`SlashCommandGroup`
(which can contain multiple subcommands)
Expand All @@ -134,9 +135,14 @@ def command_group(self, name, description="No description", is_async=False):
is_async
Whether the subgroup should be considered async (if subcommands
get an :class:`.AsyncContext` instead of a :class:`Context`.)
default_permission
Whether the command group is enabled by default.
permissions
List of permission overwrites. These apply to the entire group.
"""

group = SlashCommandGroup(name, description, is_async)
group = SlashCommandGroup(
name, description, is_async, default_permission, permissions)
self.discord_commands[name] = group
return group

Expand Down

0 comments on commit 0b50912

Please sign in to comment.