How to Require One of Two Roles for a Discord Python Bot Command?

Estimated read time 2 min read

To require one of two roles for a Discord Python bot command, you can use the has_any_role() function provided by the library. The has_any_role() function allows you to check if a user has any of the specified roles before executing the command. Here’s an example:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@commands.has_any_role('Role1', 'Role2')
async def my_command(ctx):
    # Command code goes here
    await ctx.send('Command executed successfully!')'YOUR_BOT_TOKEN')

In this example, the @commands.has_any_role() decorator is used to specify that the command requires one of two roles: 'Role1' or 'Role2'. The user invoking the command must have at least one of these roles in their Discord server roles.

The my_command() function is the actual command function that will be executed when the command is invoked. You can replace the await ctx.send('Command executed successfully!') line with the actual code for your command.

Make sure to replace 'Role1' and 'Role2' with the names or IDs of the roles you want to require. The names are case-sensitive.

Additionally, you’ll need to replace 'YOUR_BOT_TOKEN' with the actual token of your Discord bot, which you can obtain by creating a bot on the Discord Developer Portal.

With this setup, when a user tries to execute the my_command command, the bot will check if the user has either 'Role1' or 'Role2'. If the user has any of these roles, the command will be executed. Otherwise, the user will receive an “Insufficient role” error message.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply