本文介紹了如何在特定日期之前獲取消息?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從文本頻道中獲取從最新/最新消息開始直到特定日期的消息.例如直到兩天前的日期.
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));
這篇關于如何在特定日期之前獲取消息?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!