Fspluver
Fspluver

Reputation: 21

How to make a discord.py command check the user's role?

For example, I might want only admins to be able to use the following command:

if ('!hello') in message.content.lower():
        await message.channel.send('Hello friend!')

How do I make it so only users with the "admin" role will be able to use this command? I can't seem to find this in the documentation.

Upvotes: 1

Views: 1059

Answers (1)

Computing Corn
Computing Corn

Reputation: 137

You can find the discord.py documentation for the admin permission here.

The method you are looking for is this:

ctx.message.author.guild_permissions.administrator

Below I have written a mock example of a command that purges the channel and is only available to the admin.

@client.command() #Clear (purge) messages from channel (can only be used by users with administrator permissions)
    async def clear(ctx, amount=100):
        channel = ctx.message.channel
        if ctx.message.author.guild_permissions.administrator:
            await channel.purge(limit=amount,check=None,bulk=True)
        else:
            await ctx.send("You can't use that command, you are not an administrator!")

Upvotes: 1

Related Questions