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:
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)
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.
@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!')
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
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)
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.