【问题标题】:How to get the message content/embed from the message id?如何从消息 ID 中获取消息内容/嵌入?
【发布时间】:2020-09-05 01:47:11
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x discord discord.py-rewrite


    【解决方案1】:

    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 选择第一个也是唯一一个嵌入。

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


    参考资料:

    【讨论】:

    • 有没有办法在没有频道的情况下获取消息?我只有消息 ID
    • 通常当你得到消息ID时,有一种方法也能得到频道。问题的上下文是什么?您是否想在特定事件中得到这个?
    • 所以我正在使用on_raw_reaction_add() 事件。我知道我可以使用on_reaction_add(),但我不喜欢这样一个事实,即如果消息不在机器人的缓存中,它不会告诉您是否有人对消息做出反应。所以在on_raw_reaction_add(),你只有消息ID。我想从那里提取嵌入。
    • 我将使用on_raw_reaction_add() 编辑我的答案。忍受我:)
    • @Jackie 好的,我已经更新了答案,希望对您有所帮助!
    猜你喜欢
    • 2021-12-13
    • 2021-05-24
    • 2021-12-12
    • 2021-07-03
    • 1970-01-01
    • 2018-08-31
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多