如何在特定日期之前获取消息?

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));

这篇关于如何在特定日期之前获取消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Discord.js Arguments With Spaces(Discord.js 带空格的参数)
DiscordJS fetching more then 100 messages(DiscordJS 获取超过 100 条消息)
my discord.js bot doesn#39;t reply to a user message even if there isn#39;t any error(即使没有任何错误,我的 discord.js 机器人也不会回复用户消息)
Make a bot wait for some time before continuing the code(在继续代码之前让机器人等待一段时间)
Invalid Form Body in Discord.js ban command(Discord.js 禁止命令中的无效表单正文)
Set Discord Bot Activity to the number of Online users. Discord.js(将 Discord Bot 活动设置为在线用户数.不和谐.js)