首页 文章

获取发送到 Channels 的最后消息

提问于
浏览
0

我已经有一个包含特定 Channels 的变量,但是如何获取发送到 Channels 的最后一条消息?我想让我的机器人只执行一个动作,如果通道的最后一条消息不是它 .

1 回答

  • 1

    如果您已将特定通道存储在变量中,则非常简单 . 您可以在该特定通道上调用 fetchMessages 方法并获取最新消息 .

    例:

    let channel // <-- your pre-filled channel variable
    
    channel.fetchMessages({ limit: 1 }).then(messages => {
      let lastMessage = messages.first();
    
      if (!lastMessage.author.bot) {
        // The author of the last message wasn't a bot
      }
    })
    .catch(console.error);
    

    但是,如果您没有将完整的通道对象保存在变量中但只保存通道ID,则需要先执行以下操作来获取正确的通道:

    let channel = bot.channels.get("ID of the channel here");
    

相关问题