【问题标题】:Get the previous message to a discord channel将上一条消息发送到不和谐频道
【发布时间】:2020-11-06 22:59:01
【问题描述】:

给定一个 discord 消息 id 和一个频道 id,有没有办法找到同一个 discord 频道的上一条消息?

消息 id 是整数,我的印象是单调递增。因此,如果有一种方法可以查找频道收到的所有消息 id,我认为它就像找到小于您感兴趣的 id 的最高值一样简单。

【问题讨论】:

  • 谢谢!看起来很有帮助。我不确定如何使用它。当收到一条消息时,您能否使用打印上一条消息文本的机器人来回答 - 我认为这会给我想要的一切!

标签: discord discord.py


【解决方案1】:

我不确定您是要查找消息本身的内容还是之前的上一条消息,但您可以在 txt_channel.history here 上阅读更多内容:

历史记录的作用是让您查看文本频道的消息历史记录。
因此,如果您想使用它的 id 查找特定消息的内容,您可以执行以下操作: (当然需要获取文本通道,可以看here

async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
    if msg.id == ID_OF_MSG: # you found the message
       print("the message with the id has been found")

现在这段代码 ^^ 将指向您使用当前 ID 提供的消息,但如果您想获取之前的消息,您只需添加以下内容:

async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
    found_message = False
    if msg.id == ID_OF_MSG: # you found the message
       found_message =True
    elif found_message:
       found_message = False
       print("previous message")

当然,您可以将打印的最后一行更改为:

print(msg.content)

查看它的内容,或者您​​可以将其发送回频道,这取决于您。

不是: 我试了一下,好像消息的id不是一个接一个,所以不能做id-1来获取上一个消息的id。

【讨论】:

    猜你喜欢
    • 2020-05-07
    • 2021-01-19
    • 2019-04-11
    • 2020-10-25
    • 2021-06-17
    • 2021-05-08
    • 2020-07-14
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多