DocOzzy
DocOzzy

Reputation: 13

How to make a discord bot check for a user role before it proceeds with the rest of the function?

I'm trying to create a bot for my discord with a simple use : reacting to a trigger phrase in a message by adding reactions on that message, only if the user who sent the message has a certain role.

Here is the working code :


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

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

default_emojis = [
]

custom_emojis = [
    "war_tank",
    "war_dps",
    "SP",
    "DP",
    "r_drood",
    "feral",
    "equi",
    "drood_tank",
    "cham_elem",
    "cham_amelio",
    "r_cham",
    "rogue",
    "demono",
    "mage",
    "hunt",
    "DK_DPS",
    "DK_Tank",
    "pal_prot",
    "pal_ret",
    "h_pal"
]

async def react(message):
    for emoji in default_emojis:
        await message.add_reaction(emoji)
    for emoji in message.guild.emojis:
        if emoji.name in custom_emojis:
            await message.add_reaction(emoji)

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if "indiquez votre spé" in message.content.lower():
        await react(message)



As of right now, this is working for the "sending the reactions at the trigger phrase" part, but I can't figure out how to make it check for the user's role.

The current working code was provided by somebody else already, as I'm struggling with coding. I wanted something simple and thought my meager knowledge of programming would suffice, but this is a language I don't really know and I'm having trouble understanding how the Discord API works for my problem.

So if someone has an idea of how I could add something that would check for the role before it reacts to the message blindly, it would be great.

Thank you for reading and for your time.

Upvotes: 1

Views: 2136

Answers (2)

KowaiiNeko
KowaiiNeko

Reputation: 327

You start off by finding the server then the role. You have to put them inside an async function else it won't work, that's just how it works.

Hope this is what you wanted help on.

import discord
from discord.ext import commands
from discord.utils import get,find
bot=commands.Bot(command_prefix='.')


role=[]
async def info():
    server=get(bot.servers,name='Yuno')
    rol=get(server.roles,name='Admin')
    role.append(rol)

@bot.event
async def on_ready():
    await info()
    print(bot.user.name)

@bot.event
async def on_message(msg):
    if role[0] in msg.author.roles:
        await bot.add_reactions(msg,'emoji')


bot.run('')

Upvotes: 1

Patrick Haugh
Patrick Haugh

Reputation: 60974

You can use discord.utils.get to check the users roles against certain criteria. If any role matches those criteria, it will be returned, otherwise it will return None.

from discord.utils import get

if get(message.author.roles, name="Role name") and "indiquez votre spé" in message.content.lower():
    await react(message)

Upvotes: 1

Related Questions