Discord.py 2.0中无法为交互消息添加反应的问题求助
Discord.py 2.0 交互消息添加反应失败的解决方案
错误原因
你遇到的AttributeError源于两个核心问题:
- 通过
@bot.tree.command()定义的Slash命令,其interaction.message属性本身就是None——这类命令由Discord交互系统触发,并非基于用户发送的普通消息,不存在关联的原始消息对象。 interaction.response.send_message()方法的返回值是None,你赋值的message变量并没有指向实际发送出去的响应消息。
修复后的代码
@bot.tree.command() @app_commands.describe(question="Give a title") async def poll(interaction: discord.Interaction, question: str): emb = discord.Embed(title=f":bar_chart: {question}", type="rich") # 发送交互响应 await interaction.response.send_message(embed=emb) # 获取刚发送的响应消息对象 response_message = await interaction.original_response() # 添加反应 await response_message.add_reaction("✅")
关键要点
- 使用
interaction.original_response()获取交互的初始响应消息,这是Discord.py 2.0中获取此类消息对象的标准方式。 - Slash命令场景下,
interaction.message属性永远为None,不要尝试使用它。
内容的提问来源于stack exchange,提问作者Zephyrus
相关产品推荐
相关产品推荐

