How to fetch messages until a specific date?(如何在特定日期之前获取消息?)
问题描述
如何从文本频道中获取从最新/最新消息开始直到特定日期的消息.例如直到两天前的日期.
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 拖放文件类型 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
