-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40: Implement Command Permissions
- Loading branch information
Showing
7 changed files
with
279 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Permissions | ||
=========== | ||
|
||
Discord allows defining permissions for each slash command in a specific guild | ||
or at the global level. Flask-Discord-Interactions provides a Permission class | ||
and two major ways of setting the permissions of a command. | ||
|
||
Permission class | ||
---------------- | ||
|
||
The :class:`.Permission` class accepts either a role ID or a user ID. | ||
|
||
.. autoclass:: flask_discord_interactions.Permission | ||
:members: | ||
|
||
|
||
Slash Command constructor | ||
------------------------- | ||
|
||
You can define permissions when defining a Slash Command. These will be | ||
registered immediately after your Slash Command is registered. | ||
|
||
You can set the ``default_permission``, then use the ``permissions`` parameter | ||
to specify any overwrites. | ||
|
||
.. code-block:: python | ||
@discord.command(default_permission=False, permissions=[ | ||
Permission(role="786840072891662336") | ||
]) | ||
def command_with_perms(ctx): | ||
"You need a certain role to access this command" | ||
return "You have permissions!" | ||
Context object | ||
-------------- | ||
|
||
You can also use the :meth:`.Context.overwrite_permissions` method to overwrite | ||
the permissions for a command. By default, this is the command currently | ||
invoked. However, you can specify a command name. | ||
|
||
.. code-block:: python | ||
@discord.command(default_permission=False) | ||
def locked_command(ctx): | ||
"Secret command that has to be unlocked" | ||
return "You have unlocked the secret command!" | ||
@discord.command() | ||
def unlock_command(ctx): | ||
"Unlock the secret command" | ||
ctx.overwrite_permissions( | ||
[Permission(user=ctx.author.id)], "locked_command") | ||
return "Unlocked!" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import sys | ||
|
||
from flask import Flask | ||
|
||
sys.path.insert(1, ".") | ||
|
||
from flask_discord_interactions import DiscordInteractions, Permission | ||
|
||
|
||
app = Flask(__name__) | ||
discord = DiscordInteractions(app) | ||
|
||
app.config["DISCORD_CLIENT_ID"] = os.environ["DISCORD_CLIENT_ID"] | ||
app.config["DISCORD_PUBLIC_KEY"] = os.environ["DISCORD_PUBLIC_KEY"] | ||
app.config["DISCORD_CLIENT_SECRET"] = os.environ["DISCORD_CLIENT_SECRET"] | ||
|
||
discord.update_slash_commands() | ||
|
||
|
||
@discord.command(default_permission=False, permissions=[ | ||
Permission(role="786840072891662336") | ||
]) | ||
def command_with_perms(ctx): | ||
"You need a certain role to access this command" | ||
|
||
return "You have permissions!" | ||
|
||
|
||
@discord.command(default_permission=False) | ||
def locked_command(ctx): | ||
"Secret command that has to be unlocked" | ||
|
||
return "You have unlocked the secret command!" | ||
|
||
|
||
@discord.command() | ||
def unlock_command(ctx): | ||
"Unlock the secret command" | ||
|
||
ctx.overwrite_permissions( | ||
[Permission(user=ctx.author.id)], "locked_command") | ||
|
||
return "Unlocked!" | ||
|
||
|
||
discord.set_route("/interactions") | ||
|
||
|
||
discord.update_slash_commands(guild_id=os.environ["TESTING_GUILD"]) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
AsyncContext, | ||
CommandOptionType, | ||
ChannelType, | ||
Permission, | ||
Member, | ||
User, | ||
Role, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.