Answers for "discord.py command permissions"

7

discord.py ban

@commands.command() # uses command decorators, in this case inside a cog
@commands.has_permissions(ban_members=True) # only people that have permissions to ban users can use this command
async def ban(self, ctx, user: discord.Member, *, reason): # The person banning someone has to ping the user to ban, and input a reason. Remove self if you are outside a cog.
    await ctx.guild.ban(user, reason=reason) # Bans the user.
    await user.send(f"You have been banned in {ctx.guild} for {reason}") # Private messages user.
    await ctx.send(f"{user} has been successfully banned.") # messages channel to tell everyone it worked
Posted by: Guest on July-04-2020
5

python discord bot command permissions

@commands.guild_only()
# Command cannot be used in private messages.

@commands.is_owner()
# Command can only be used by the bot owner.

@commands.is_nsfw()
# Command can only be used in NSFW channels

@commands.has_role("name") 
# Check if member has a role with the name "name"

@commands.bot_has_role(11132312313213) 
# As above, but for the bot itself. (name can be replaced with id)

@commands.has_any_role(["role1","foo",11132312313213]) 
# Check if user has any of the roles with the names "role1", "foo", or the role with id 11132312313213

@commands.bot_has_any_role(*roles) 
# As above, but for the bot itself

@commands.has_permissions([ban_members=True, kick_members=True]) 
# Check if user has all of the passed permissions 
#  e.g. this command will require both kick and ban permissions

@commands.bot_has_permissions(**perms)
# As above, but for the bot itself.

@commands.has_guild_permissions(**perms)
@commands.bot_has_guild_permissions(**perms)
# As for the two above, but for guild permissions rather than channel permissions.

@commands.check(myfunction)
# Check against your own function that returns those able to use your command

@commands.check_any(*myfunctions)
# Command will be ran if the conditions of any of your own check functions are met

from discord.ext.commands.cooldowns import BucketType
# BucketType can be BucketType.default, member, user, guild, role, or channel
@commands.cooldown(rate,per,BucketType) 
# Limit how often a command can be used, (num per, seconds, BucketType)

@commands.max_concurrency(number, per=BucketType.default, *, wait=False)
# Limit how many instances of the command can be running at the same time.
# Setting wait=True will queue up additional commands. False will raise MaxConcurrencyReached

# Checks can be stacked, and will Raise a CheckFailure if any check fails.
Posted by: Guest on February-27-2020
2

how to limit a command to a permission in discord.py

# Make sure you don't have a command called "commands"
@client.command() # As usual
@commands.has_permissions(administrator=True) # Making sure the person executing the command has the permissions
async def foo(ctx):
	await ctx.send("Hello")
    #ect
Posted by: Guest on September-29-2020
3

discord.py how to use permissions

from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions

@bot.command(name="kick", pass_context=True)
@has_permissions(manage_roles=True, ban_members=True)
async def _kick(ctx, member: Member):
    await bot.kick(member)

@_kick.error
async def kick_error(error, ctx):
    if isinstance(error, MissingPermissions):
        text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
        await bot.send_message(ctx.message.channel, text)
Posted by: Guest on October-10-2020
0

discord py check if user has permission return message if not

@bot.command()
@has_permissions(kick_members=True)  
async def kick(self, ctx, Member: discord.Member):
          await bot.kick(Member)

@kick.error
async def kick_error(error, ctx):
   if isinstance(error, MissingPermissions):
       await ctx.send("You don't have permission to do that!")
Posted by: Guest on September-18-2020
0

set permissions role discord.py

server = ctx.message.server
perms = discord.Permissions(send_messages=False, read_messages=True)
await client.create_role(server, name='NoSend', permissions=perms)
Posted by: Guest on September-05-2020

Code answers related to "discord.py command permissions"

Python Answers by Framework

Browse Popular Code Answers by Language