首页 > 解决方案 > 记录来自频道的所有消息

问题描述

我正在尝试从频道中获取所有消息,然后从这些消息中记录内容,有没有办法做到这一点?

我试过这个,但它不起作用:

const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(message => console.log(`[${message.author.name}]${message.content}`));

这是我得到的结果:
Undefined,它
甚至[${message.author.name}]
不返回任何东西,因为你不能从undefined.

标签: javascriptdiscorddiscord.js

解决方案


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 之外,您可能需要考虑使用cleanContentawait将and结合起来也不是一个好习惯then。选择一个可能是个好主意。


推荐阅读