async function fetchMessagesUntil(channel, endDate, lastID) {
let messages = [];
try {
messages = (await channel.messages.fetch({ limit: 100, before: lastID })).array();
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));
} catch {
return messages;
}
}
示例用法
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));
1条答案
按热度按时间j8yoct9x1#
这将是我的方法,请随意发布您自己更好的答案:3
示例用法