获取发送到频道的最后一条消息
Get last message sent to channel
我已经有一个包含特定频道的变量,但如何获取发送到该频道的最后一条消息?我想让我的机器人仅在发送到频道的最后一条消息不是由它发送时才执行操作。
如果您已经将特定频道存储在变量中,这很容易。您可以在该特定频道上调用 MessageManager#fetch()
方法并获取最新消息。
示例:
let channel // <-- your pre-filled channel variable
channel.messages.fetch({ 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");
最近我相信他们已经从 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);
有一个 属性 包含最后写入消息的对象。所以获取最后一条消息的最短版本是:
let lm = channel.lastMessage;
当然@Tyler 的版本仍然有效。但是我的IDE说他不知道first()
。那么这可能有一天会被弃用吗?!?我不知道。
无论如何,在这两种方式中,您都可以检索消息的对象。如果你想拥有例如你可以这样做的文字
let msgText = lm.content; // channel.lastMessage.content works as well
我已经有一个包含特定频道的变量,但如何获取发送到该频道的最后一条消息?我想让我的机器人仅在发送到频道的最后一条消息不是由它发送时才执行操作。
如果您已经将特定频道存储在变量中,这很容易。您可以在该特定频道上调用 MessageManager#fetch()
方法并获取最新消息。
示例:
let channel // <-- your pre-filled channel variable
channel.messages.fetch({ 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");
最近我相信他们已经从 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);
有一个 属性 包含最后写入消息的对象。所以获取最后一条消息的最短版本是:
let lm = channel.lastMessage;
当然@Tyler 的版本仍然有效。但是我的IDE说他不知道first()
。那么这可能有一天会被弃用吗?!?我不知道。
无论如何,在这两种方式中,您都可以检索消息的对象。如果你想拥有例如你可以这样做的文字
let msgText = lm.content; // channel.lastMessage.content works as well