如何从文本频道中获取从最新/最新消息开始直到特定日期的消息.例如直到两天前的日期.
How would one fetch messages from text channel beginning with the latest/newest message until a specific date. For example until the date two days ago.
想要的结果是有一个函数可以完成这项工作并返回一个范围内的消息数组:现在 ->指定为函数参数的结束日期.
The desired result is having a function that will do the job and return an array of messages dating in a range: now -> end date specified as the function's argument.
这将是我的方法,请随时发布您自己更好的答案:3
This would be my approach, feel free to post your own better answers :3
async function fetchMessagesUntil(channel, endDate, lastID) {
let messages = (await channel.messages.fetch({ limit: 100, before: lastID })).array();
if (messages.length == 0) return messages;
for (let i = 0; i < messages.length; i++) {
if (messages[i].createdAt.getTime() < endDate.getTime()) {
return messages.slice(0, i);
}
}
return messages.concat(
await fetchMessagesUntil(channel, endDate, messages[messages.length - 1].id)
);
}
示例用法
let end = new Date();
end.setDate(end.getDate() - 2); // Subtract two days from now
(await fetchMessagesUntil(message.channel, end)).forEach(x => console.log(x.content));
这篇关于如何在特定日期之前获取消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!