How to make bot react to users' mentions? #5877
-
I want to make a minigame using my Discordpy bot. The idea is:
My only problem in this case is the 4th stage: I can't make my bot detect mentions when the user answers its question. I've tried 2 different codes based on what I found at the internet: First Code:
Second Code:
I've managed to make the TimeoutError work. However, as I've tried to correct the code, I messed up with it and now not even the Timeout is working. The situation is the same as the first code: after PS: I do not want to use users IDs or databases in order to make the bot detect mentions. Links I've checked: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Your second code is closer to what you want to do, however the check function is wrong. There is no such thing as Messages have a def check(message):
return message.author == ctx.author and len(message.mentions) == 1 and message.mentions[0] != ctx.author This There is another issue in your second code and that's the handling of your try:
msg = await client.wait_for('message', timeout=20, check=check)
except asyncio.TimeoutError:
return await ctx.send("Cráááááá! What's the problem? Are you scared? Crááá! Call me when you stop crying out of fear! Cráááá!")
else:
# In here, we didn't time out and msg is a valid discord.Message object
opponent = msg.mentions[0] Notice that I call After putting all this together you should have both a working program and an |
Beta Was this translation helpful? Give feedback.
Your second code is closer to what you want to do, however the check function is wrong. There is no such thing as
discord.mentions
and taking thestr
out of it makes little sense.Messages have a
Message.mentions
attribute that allow you to get the members being mentioned in the message. So a simple way of doing it is just checking if they mentioned one person like so:This
check
predicate checks whether there was a mention and also makes sure that the person mentioned isn't the person using the command.There is another issue in your second code and that's the …