Discord.py:如何为机器人自身发送的消息添加表情反应?
问题分析与解决办法
你的核心问题是没有保存机器人发送的消息对象,而且命令参数里的**message完全用错了,导致你调用add_reaction时根本没指向机器人自己发的那条消息。
具体错误点:
**message这个参数是多余且错误的,discord.py的命令参数里,这样写只会捕获关键字参数,根本不是你要的消息对象。- 你调用
await bot.get_channel(moderator_channel).send(embed=embed)时,没有把返回的消息存下来,这条send方法会返回机器人发送的Message对象,只有对这个对象调用add_reaction才会生效。 - 之前试的
ctx.message是用户触发命令时发的消息,所以只会给用户的消息加反应;bot.message、client.message这类属性根本不存在,自然报错。
修正后的代码:
bot = commands.Bot(command_prefix = "!", intents=intents) @bot.command(pass_context=True) async def rnick(ctx, *, nick): embed=discord.Embed(color=embed_color) embed.add_field(name="****", value="*******************", inline=False) embed.add_field(name="*********", value="******", inline=True) embed.add_field(name="*******",value="*****") embed.set_footer(text=str(ctx.author.id) + " • " + ct) # 保存机器人发送的消息对象 sent_msg = await bot.get_channel(moderator_channel).send(embed=embed) # 对保存的消息对象添加反应 await sent_msg.add_reaction("✅") await sent_msg.add_reaction("❌")
额外注意事项:
- 确保你的机器人在
moderator_channel这个频道里有添加反应的权限。 - 检查intents配置,至少开启
messages相关的intents(如果是discord.py v2.x,还需要开启message_content),否则机器人可能无法正确处理消息相关操作。
内容的提问来源于stack exchange,提问作者Julian
相关产品推荐
相关产品推荐

