如何从消息id中获取消息content/embed?

How to get the message content/embed from the message id?

我想知道如何从 message id 获取消息内容(特别是嵌入内容)?就像您可以使用 member id

获取成员一样

on_raw_reaction_add() 例子:

@bot.event
async def on_raw_reaction_add(payload):
    channel = bot.get_channel(payload.channel_id)
    msg = await channel.fetch_message(payload.message_id)
    embed = msg.embeds[0]
    # do something you want to

命令示例:

@bot.command()
async def getmsg(ctx, channel: discord.TextChannel, msgID: int):
    msg = await channel.fetch_message(msgID)
    await ctx.send(msg.embeds[0].description)

在我传递的命令中 channelmsgID,因此示例命令执行需要 !getmsg #general 112233445566778899 - 通道必须与您在同一台服务器上在 !

中执行命令

然后我使用 fetch_message() 协程获取消息对象,这允许我在所述消息中获取 embeds 的列表。然后我通过选择位置索引 0.

选择第一个也是唯一的嵌入

之后,机器人会发送嵌入的描述(或您想要的任何属性)。


参考文献: