wey follower
wey follower

Reputation: 11

How do I set a required role for a command?

I'm trying to make my Discord bot require a rank to execute the Say command but I cannot find a way to make it work. I would like to ask you guys if you could help me setup a role required to execute the command.

import asyncio
import discord
from discord.ext.commands import Bot

bot = Bot('>') # the <#4> is essential so people cannot share it via DMs.

@bot.command(pass_context = True)
async def Say(ctx, *args):
    mesg = ' '.join(args)
    await bot.delete_message(ctx.message)
    return await bot.say(mesg)

bot.run('Token')

Upvotes: 1

Views: 615

Answers (1)

Taku
Taku

Reputation: 33714

Discord.py provides a simple decorator commands.has_role that is used for verifying roles by name. The has_role utilise the check function and both functions are part of the undocumented commands extension.

from discord.ext import commands

bot = commands.Bot('>')

@bot.command(pass_context = True)
@commands.has_role('admin')
async def Say(ctx, *args):
    mesg = ' '.join(args)
    await bot.delete_message(ctx.message)
    return await bot.say(mesg)

bot.run('Token')

Upvotes: 1

Related Questions