【问题标题】:Get a message's content from their id从他们的 id 获取消息的内容
【发布时间】:2018-08-31 07:02:06
【问题描述】:

有没有办法从它的 id 获取消息的内容?如果是这样,我该怎么做?我已经阅读了它的文档,但我什么也没找到。

在文档中,它说您可以使用.id 获得discord.Message 的ID,但我没有discord.Message 对象。

谢谢。

【问题讨论】:

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


    【解决方案1】:

    如果您没有频道 id 而只有消息 id,您只需遍历可访问的频道并检查哪些频道*具有与您的 id 匹配的消息。

    from discord import NotFound
    
    
    for channel in client.get_all_channels():
        try:
            msg = await channel.get_message(id)
        except NotFound:
            continue
        print(msg.content)
    
    # where `id` is the message id and `client` is the bot or user
    

    * 消息 id 不是系统唯一的,这就是为什么唯一的方法是遍历所有可用通道的原因,因为消息 id 仅对通道是唯一的。有可能(虽然不太可能)匹配来自两个不同渠道的具有相同 id 的两条不同消息。

    【讨论】:

    【解决方案2】:

    在 discord.py 的 rewrite 分支中,您可以使用 get_message() 协程(找到的文档 here)使用其 ID 查找消息。我不确定 0.16.12 是否有办法做到这一点

    【讨论】:

    • get_message 存在于 0.16.x 中,但它也需要频道 ID。
    • 有意思,我翻文档的时候没看到。感谢您的信息
    猜你喜欢
    • 2020-09-05
    • 2021-12-13
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多