I'm trying to get the second last message sent in the same channel where it was sent, but when i use .fetchMessages
with limit of two messages and try to get the second one, it doesn't work.
I tried with this
channel.fetchMessages({limit:2}).then(res =>{
let lm = res[1]
console.log(lm)
})
But it doesn't works
channel.fetchMessages()
returns a Collection of messages, which is an extended class of Map . They can not be iterated over like res[1]
, instead you can do one of the two things.
.last()
- ref This will get you the last element from the collection, which is the 2nd last message.
channel.fetchMessages({limit: 2}).then(res => {
let lm = res.last()
console.log(lm)
})
.array()
- ref This will convert the Collection to an array which can be iterated over.
channel.fetchMessages({limit: 2}).then(res => {
let lm = res.array()[1]
console.log(lm)
})
In v12, channel.fetchMessages
does not work anymore instead it's channel.messages.fetch
So updating tintin9999's answer the two solutions will be:
1. Using .last()
channel.messages.fetch({limit: 2}).then(res => {
let lm = res.last()
console.log(lm)
})
2. Using .array()
channel.messages.fetch({limit: 2}).then(res => {
let lm = res.array()[1]
console.log(lm)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.