簡體   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