javascript - 记录来自频道的所有消息
问题描述
我正在尝试从频道中获取所有消息,然后从这些消息中记录内容,有没有办法做到这一点?
我试过这个,但它不起作用:
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(message => console.log(`[${message.author.name}]${message.content}`));
这是我得到的结果:
Undefined
,它
甚至[${message.author.name}]
不返回任何东西,因为你不能从undefined
.
解决方案
fetchMessages
将始终返回一个集合,即使您使用limit: 1
. 所以,如果你想访问你需要的 Collection 的第一个元素
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(messages => console.log(`[${messages.first().author.name}]${messages.first().content}`));
如果您打算将消息保存在 Discord 之外,您可能需要考虑使用cleanContent。await
将and结合起来也不是一个好习惯then
。选择一个可能是个好主意。
推荐阅读
- controller - SDL2:触觉方法表现不如预期
- angular - NGXS @Action 方法未调用
- selenium - Macbook m1 上的硒
- java - 扫描仪中的分隔符,保留分隔符
- android - 从一台机器创建的项目复制到另一台机器时的 SDK 路径问题
- javascript - 未处理的拒绝(TypeError):无法读取 React.js 中 stripe.confirmCardPayment 中未定义的属性“id”
- javascript - 更改“-1”答案,当 indexof 未找到任何匹配项时
- html - 将鼠标悬停在响应式列图像上时如何显示文本?
- angularjs - $rootScope 存储在组件 $scope 中
- tensorflow - 使用用 coco 训练的图像分类模型