问题描述
我正在尝试从通道中获取所有消息,然后从这些消息中记录内容,有没有办法做到这一点?
我已经尝试过了,但是没有用:
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(message => console.log(`[${message.author.name}]${message.content}`));
这是我得到的结果:
Undefined
和[${message.author.name}]
它甚至不返回任何内容,因为您无法从undefined读取任何内容。
1楼
Xzandro
1
已采纳
2019-02-25 01:29:23
即使您使用limit: 1 , fetchMessages也将始终返回Collection。
因此,如果要访问集合的第一个元素,则需要
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(messages => console.log(`[${messages.first().author.name}]${messages.first().content}`));
如果计划将消息保存在Discord之外,则可能需要考虑使用 。
组合then await也不是一个好习惯。
选择一个可能是个好主意。