我试图获取在发送它的同一 channel 中发送的倒数第二条消息,但是当我使用 .fetchMessages
限制两条消息并尝试获取第二条消息时,它不起作用。
我尝试过这个
channel.fetchMessages({limit:2}).then(res =>{
let lm = res[1]
console.log(lm)
})
但是不起作用
最佳答案
channel.fetchMessages()
返回 Collection消息,它是 Map 的扩展类。它们不能像 res[1]
那样进行迭代,而是可以执行两件事之一。
1.使用.last()
- ref
这将为您提供集合中的最后一个元素,即倒数第二条消息。
channel.fetchMessages({limit: 2}).then(res => {
let lm = res.last()
console.log(lm)
})
2.使用.array()
- ref
这会将 Collection 转换为可以迭代的数组。
channel.fetchMessages({limit: 2}).then(res => {
let lm = res.array()[1]
console.log(lm)
})
关于javascript - 使用 Discord.Js 获取倒数第二条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596705/