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

标签 javascript discord discord.js

我试图获取在发送它的同一 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/

相关文章:

javascript - 如何获取文件的绝对路径

python - 通过 node.js 运行 python 文件

javascript - 如何测试字符串是否有数组条目

javascript - 显示包含特定字符串的 div

javascript - 进度条 AJAX 和 PHP

javascript,保存对嵌套对象文字的引用

python-3.x - 如何获取 channel 中已发送的消息数量

python - 从不和谐消息中选择单词不起作用

javascript - Discord.js 关于消息命令不起作用

javascript - 在给定的discord channel 中搜索满足条件的所有消息并删除