Discord.py中reaction_add事件校验范围问题:如何限制仅对机器人自身消息的表情交互生效
Hey there! I see the problem here—your current code only checks that the reacting user is the command author and the emoji is one of your targets, but it doesn't verify that the reaction is on the exact message your bot sent. That's why reactions on other messages are triggering the logic.
Here's how to fix it: we need to add an extra check to ensure the reaction is attached to the msg object (the one your bot added reactions to). Let's update the wait_for check:
import asyncio emojis = ['✅', '❌'] for emoji in emojis: await msg.add_reaction(emoji) try: # Add r.message == msg to ensure the reaction is on your bot's message emojiWait, user = await client.wait_for( 'reaction_add', check=lambda r, user: user == ctx.author and r.emoji in emojis and r.message == msg, timeout=30.0 ) except asyncio.TimeoutError: # Code when user didn't react for 30s print("User didn't react in time!") else: if emojiWait.emoji == emojis[0]: # Code when user reacted with ✅ print("User confirmed!") elif emojiWait.emoji == emojis[1]: # Code when user reacted with ❌ print("User declined!")
What changed?
The key addition is r.message == msg in the lambda check. This makes sure:
- The reacting user is the same as the one who ran the command (
user == ctx.author) - The emoji used is one of your specified options (
r.emoji in emojis) - The reaction is placed on the exact message your bot sent (
r.message == msg)
This way, any reactions on other messages (even with the same emojis) won't trigger your bot's logic—only interactions with the specific prompt message will count.
内容的提问来源于stack exchange,提问作者KamilZachod

