goose.mp4
goose.mp4

Reputation: 272

Discord.py: How would you restrict a certain command to a role or people with specific permissions?

Could someone show how you'd restrict, for example, the following command to lets say a role named 'Moderator' or only to people that have kick permissions? I don't quite understand how that'd work.

@client.command()
async def kick(ctx, member:discord.Member)
    await member.kick()
    await ctx.send(f'{member.mention} has been kicked.')

Upvotes: 1

Views: 1655

Answers (1)

Mystico
Mystico

Reputation: 68

To restrict commands to certain roles named "Moderator" or "Admin" use has_any_role

@client.command(pass_context=True)
@commands.has_any_role("Moderator", "Admin")
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.channel.send(f'{member.mention} has been kicked.')

To restrict commands to certain permissions of roles for example if a role has administrator you use has_permissions

@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.channel.send(f'{member.mention} has been kicked.')

Upvotes: 4

Related Questions