Skip to content

Commit 7f2c26f

Browse files
DorukyumLulalaby
authored andcommitted
Update slash command permissions example
1 parent 2f0e176 commit 7f2c26f

File tree

1 file changed

+17
-83
lines changed

1 file changed

+17
-83
lines changed
Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,28 @@
11
import discord
22

3-
# Imports permissions from discord.commands
4-
from discord.commands import permissions
5-
63
bot = discord.Bot()
74

8-
# Note: If you want you can use commands.Bot instead of discord.Bot
9-
# Use discord.Bot if you don't want prefixed message commands
10-
11-
# With discord.Bot you can use @bot.command as an alias
12-
# of @bot.slash_command but this is overridden by commands.Bot
13-
14-
# by default, default_permission is set to True, you can use
15-
# default_permission=False to disable the command for everyone.
16-
# You can add up to 10 permissions per Command for a guild.
17-
# You can either use the following decorators:
18-
# --------------------------------------------
19-
# @permissions.permission(role_id/user_id, permission)
20-
# @permissions.has_role("ROLE_NAME") <-- can use either a name or id
21-
# @permissions.has_any_role("ROLE_NAME", "ROLE_NAME_2") <-- can use either a name or id
22-
# @permissions.is_user(USER_ID) <-- id only
23-
# @permissions.is_owner()
24-
# Note: you can supply "guild_id" to limit it to 1 guild.
25-
# Ex: @permissions.has_role("Admin", guild_id=GUILD_ID)
26-
# --------------------------------------------
27-
# or supply permissions directly in @bot.slash_command
28-
# @bot.slash_command(
29-
# default_permission=False,
30-
# permissions=[
31-
# permissions.CommandPermission(id=ID, type=TYPE, permission=True, guild_id=GUILD_ID)
32-
# ]
33-
# )
34-
35-
# Note: Please replace token, GUILD_ID, USER_ID and ROLE_NAME.
36-
37-
# Guild Slash Command Example with User Permissions
38-
39-
GUILD_ID = ...
40-
USER_ID = ...
41-
42-
43-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
44-
@permissions.is_user(USER_ID)
45-
async def user(ctx):
46-
"""Say hello to the author""" # the command description can be supplied as the docstring
47-
await ctx.respond(f"Hello {ctx.author}!")
48-
49-
50-
# Guild Slash Command Example with Owner Permissions
51-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
52-
@permissions.is_owner()
53-
async def owner(ctx):
54-
"""Say hello to the author""" # the command description can be supplied as the docstring
55-
await ctx.respond(f"Hello {ctx.author}!")
56-
57-
58-
# Guild Slash Command Example with Role Permissions
59-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
60-
@permissions.has_role("ROLE_NAME")
61-
async def role(ctx):
62-
"""Say hello to the author""" # the command description can be supplied as the docstring
63-
await ctx.respond(f"Hello {ctx.author}!")
64-
655

66-
# Guild Slash Command Example with Any Specified Role Permissions
67-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
68-
@permissions.has_any_role("ROLE_NAME", "ROLE_NAME2")
69-
async def multirole(ctx):
70-
"""Say hello to the author""" # the command description can be supplied as the docstring
71-
await ctx.respond(f"Hello {ctx.author}!")
6+
@bot.slash_command()
7+
@discord.default_permissions(
8+
administrator=True
9+
) # only members with this permission can use this command
10+
async def admin_only(ctx):
11+
await ctx.respond(f"Hello {ctx.author}, you are an administrator.")
7212

7313

74-
# Guild Slash Command Example with Permission Decorator
75-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
76-
@permissions.permission(user_id=USER_ID, permission=True)
77-
async def permission_decorator(ctx):
78-
"""Say hello to the author""" # the command description can be supplied as the docstring
79-
await ctx.respond(f"Hello {ctx.author}!")
14+
@bot.slash_command()
15+
@discord.default_permissions(
16+
manage_messages=True, ban_members=True
17+
) # you can supply multiple permissions
18+
async def staff_only(ctx):
19+
await ctx.respond(f"Hello {ctx.author}, you can manage messages and ban members.")
8020

8121

82-
# Guild Slash Command Example with Permissions Kwarg
83-
@bot.slash_command(
84-
guild_ids=[GUILD_ID],
85-
default_permission=False,
86-
permissions=[permissions.CommandPermission(id=USER_ID, type=2, permission=True)],
87-
)
88-
async def permission_kwarg(ctx):
89-
"""Say hello to the author""" # the command description can be supplied as the docstring
90-
await ctx.respond(f"Hello {ctx.author}!")
22+
"""
23+
Server owners and administrators have all permissions and can change required permissions per-server.
24+
If the member viewing commands does not have the required permissions, the commands will show up disabled.
25+
"""
9126

9227

93-
# To learn how to add descriptions, choices to options check slash_options.py
94-
bot.run("token")
28+
bot.run("TOKEN")

0 commit comments

Comments
 (0)