Discord.py用wait_for实现表情交互后发Embed代码不运行问题咨询
代码问题定位与修复方案
核心故障原因
- 变量覆盖导致校验逻辑完全失效:代码初始的
message变量存储的是用户发送的触发消息(内容为test),但执行到message = await channel.send(embed=myEmbed)时,message被覆盖为机器人发送的Embed消息,此时message.author是机器人本身,wait_for的校验条件要求触发反应的用户等于机器人,普通用户加反应永远无法满足条件,自然无法触发后续发送myEmbed2的逻辑。 - 缺少事件兜底逻辑:重写
on_message事件后如果没有添加await client.process_commands(message),后续你新增的其他前缀命令会全部无法触发。 - 无空值兼容逻辑:
client.get_channel如果传入错误的频道ID会返回None,后续调用send方法会直接抛出异常,建议增加空值判断。
修复后的代码示例
@client.event async def on_message(message): # 新增:保证其他命令可以正常触发 await client.process_commands(message) if message.content == 'test': # 替换为你实际的频道ID channel = client.get_channel(XXXXXXXXXXXXX) # 新增:判断频道是否存在,避免ID错误导致崩溃 if not channel: return myEmbed = discord.Embed(title="News Feed", description="The #announcements will be your most important channel here! You'll be able to keep up to date with everything which is happening within the server so you'll never miss a beat!", color=0x00ff00) myEmbed.add_field(name="It should only take a few minutes!", value="Press the button below to continue") myEmbed.set_footer(text="Christer - How does it work", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png") myEmbed2 = discord.Embed(title="News Feed", description="The #announcements will be your most important channel here! You'll be able to keep up to date with everything which is happening within the server so you'll never miss a beat!", color=0x00ff00) myEmbed2.add_field(name="It should only take a few minutes!", value="Press the button below to continue") myEmbed2.set_footer(text="Christer - How does it work", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png") # 修改变量名,避免覆盖用户触发的消息对象 bot_msg = await channel.send(embed=myEmbed) await bot_msg.add_reaction("🟢") # 修正校验逻辑:判断是触发命令的用户、表情正确、且是在机器人发送的这条消息上加的反应 await client.wait_for('reaction_add', check = lambda reaction, user: user == message.author and str(reaction.emoji) == "🟢" and reaction.message.id == bot_msg.id) await channel.send(embed=myEmbed2) # 此处可继续添加发送myEmbed3的逻辑,如果需要二次交互,重复上述加反应、监听反应的流程即可
内容的提问来源于stack exchange,提问作者Chagen
相关产品推荐
相关产品推荐

