Help with a Discord Bot function, Add role by text trigger

Me and my friend want to add a function to our bot that gives a user the role “Baguette Team” AND posts a message saying that they recived the role if they post a message containing “I love baguettes”.

This is my code so far:


import os, re, discord
from discord.ext import commands

DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

bot = commands.Bot(command_prefix="!", intents= discord.Intents.default())

@bot.event
async def on_ready():
    print(f"{bot.user} has connected to Discord!")

bot.run(DISCORD_TOKEN)

We will not code it for you, but you can use the on_message event to check if text is in a message. You can find out how to add roles in the docs: https://discordpy.readthedocs.io/, you can also find how to send a message.

Thank you. I figured out the on_message parameter, turns out I needed to use intents.all instead of intents.default. Now I have another issue. The console gives the following error when I want to add the role to the member:

image

What am I missing now?


import os, re, discord
from discord.ext import commands

DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

client = commands.Bot(command_prefix="!", intents= discord.Intents.all())

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return

    if message.content == 'I love baguettes':
        await message.author.add_roles(["Baguette Team"], reason="You said the magic words!", atomic=True)
        await message.channel.send('So do I! In fact, I have given you the Baguette Team role!')

client.run(DISCORD_TOKEN)

Should be this I think:
await message.author.add_roles(message.author, [brole], reason="You said the magic words!", atomic=True)

Also, the roles should be role object not strings. (brole is the var for the role object)

1 Like

What is the role object?
Is it brole = role.id?

brole = role, just give it the full object. I am pretty sure that is how it works, I do not have access to the docs right now.

If I knew how to make it, I would totally do it for them.

That is not good, doing it for them will not help them learn, it will only make them dependent on help. If you want to teach them, do not write it for them.

brole = Baguette_Team throws an error. Do I need quotation marks, at sign, etc.?

1 Like

What error, and where is Baguette_Team defined?

@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return

    if message.content == 'I love baguettes':
        brole = Bagutte_Team
        await message.author.add_roles(message.author, [brole], reason="You said the magic words!", atomic=True)
        await message.channel.send('So do I! In fact, I have given you the Baguette Team role!')

The error was:
image

:person_facepalming:
You have to define it!
Like, brole = discord.utils.get(member.server.roles, name="Baguette Team")
And please adapt that, it will not work if you just copy and paste XD

Okay, sorry.
My code now reads:


import os, re, discord
from discord.ext import commands

DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

client = commands.Bot(command_prefix="!", intents= discord.Intents.all())

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return

    if message.content == 'I love baguettes':
        role = discord.utils.get(message.author.server.roles, name="Baguette_Team")
        await message.author.add_roles(message.author, [role], reason="You said the magic words!", atomic=True)
        await message.channel.send('So do I! In fact, I have given you the Baguette Team role!')

client.run(DISCORD_TOKEN)

But I get this error now:
image

Wait, let me try something…

Do message.guild, that one was kind of confusing ngl.

Bro why are you so rude? You literally made him apologize to you…

1 Like

Okay. Still not working. I get 404 on fetching the role. I verified spelling, too. The code sounds like you are getting the role object from the user that posted the message. The problem is that the user that posted the message may not have that role.

No, it should be the whole guild. Try using the id instead, it is more sure-fire.

I tried message.guild.id and message.author.guild.id, neither worked. I have to go now, I’ll be on later to try some more.

I mean the role id. Used id= instead of name= in get(). And use the id, not the name

What am I doing wrong now? It says it cannot iterate over an int.
role = discord.utils.get(message.guild.id, id="Baguette_Team")