我已经有一个包含特定频道的变量,但是如何获取发送到该频道的最后一条消息?我想让我的漫游器仅在该频道没有最后一条消息的情况下才执行操作。
答案 0 :(得分:6)
最近我相信它们已经从channel.fetchMessages()
变为channel.messages.fetch()
channel.messages.fetch({ limit: 1 }).then(messages => {
let lastMessage = messages.first();
// do what you need with lastMessage below
})
.catch(console.error);
答案 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");
答案 2 :(得分:1)
有一个属性,包含最后写入的消息的对象。因此,获取最新消息的最简短版本是:
let lm = channel.lastMessage;
当然,@ Tyler的版本仍在工作。但是我的IDE表示他不认识first()
。所以有一天可能会弃用吗?!?我不知道。
无论如何,您都可以通过两种方式检索消息的对象。如果您想拥有例如您可以执行的文字
let msgText = lm.content; // channel.lastMessage.content works as well