discord.py中如何从RawReactionActionEvent获取反应对应的原消息
解决方案
1. 使用RawReactionActionEvent属性获取原消息
你可以直接通过事件携带的三个属性完成消息定位,步骤如下:
- 先获取服务器/频道对象:
若为服务器内消息,先通过guild_id拿服务器实例,再拿对应频道:
若为私信场景,直接拿频道即可:guild = client.get_guild(payload.guild_id) channel = guild.get_channel(payload.channel_id)channel = client.get_channel(payload.channel_id) - 拉取原消息:
其中target_message = await channel.fetch_message(payload.message_id)payload为你拿到的RawReactionActionEvent实例。
2. Embed序列化报错处理
报错原因
你直接调用client.http的底层方法时,直接传入了Embed类实例,底层HTTP接口仅支持可序列化的Python字典,因此非Embed场景下字符串可正常传递,Embed场景会触发类型错误。
解决方法
方案一:使用高层实例方法(推荐)
拿到target_message对象后直接调用实例自带的操作方法,框架会自动处理Embed序列化逻辑:
- 编辑消息:
await target_message.edit(content="新内容", embed=你的Embed实例),多Embed场景替换为embeds=[Embed实例1, Embed实例2] - 删除消息:
await target_message.delete()
方案二:手动序列化Embed(适合必须用底层http方法的场景)
调用Embed实例自带的to_dict()方法,将实例转为可序列化的字典后再传入底层方法:
# 编辑消息示例 await client.http.edit_message( channel_id=你的channel_id, message_id=你的message_id, content="新内容", embed=你的Embed实例.to_dict() ) # 多Embed场景替换为 embeds=[e.to_dict() for e in 你的Embed列表]
内容的提问来源于stack exchange,提问作者MichaelT572
相关产品推荐
相关产品推荐

