繁体   English   中英

使用 Discord.Js 获取倒数第二条消息

[英]Get second last message with Discord.Js

我试图在发送它的同一频道中发送最后第二条消息,但是当我使用.fetchMessages限制两条消息并尝试获取第二条消息时,它不起作用。

我试过这个

channel.fetchMessages({limit:2}).then(res =>{
 let lm = res[1]
 console.log(lm)
})

但它不起作用

channel.fetchMessages()返回一个消息集合,它是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)
})

在 v12 中, channel.fetchMessages不再起作用,而是channel.messages.fetch

^ 参考

因此,更新 tintin9999 的答案,两个解决方案将是:

1. 使用.last()

channel.messages.fetch({limit: 2}).then(res => {
let lm = res.last()
console.log(lm)
})

2. 使用.array()

channel.messages.fetch({limit: 2}).then(res => {
 let lm = res.array()[1]
 console.log(lm)
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM