获取超过 100 条消息

Fetch more than 100 messages(获取超过 100 条消息)
本文介绍了获取超过 100 条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试找出一种方法,通过 fetchMesasges()before 使用循环来获取不和谐的旧消息.我想使用循环获得超过 100 的限制,但我无法弄清楚,我能找到的每篇文章都只讨论如何使用循环删除超过 100 的限制,我只需要检索它们.

I'm trying to figure out a way to use loops to get old messages on discord using fetchMesasges() and before. I'd like to get more than the 100 limit using a loop but I cannot figure it out, and every post I can find only discuss how to use loops to DELETE more than the 100 limit, I just need to retrieve them.

我是编码新手,尤其是 javascript,所以我希望有人可以帮助我朝着正确的方向前进.

I'm new to coding and javascript in particular so I'm hoping someone can give me a nudge in the right direction.

这是我能够设法检索超过 100 条消息的唯一方法(在多次尝试使用循环失败之后):

Here is the only way I could manage to retrieve messages that are farther than 100 back(after many failed attempts at using loops):

channel.fetchMessages({ limit: 100 })
    .then(msg => {
        let toBeArray = msg;
        let firstLastPost = toBeArray.last().id;

        receivedMessage.channel
            .fetchMessages({ limit: 100, before: firstLastPost })
            .then(msg => {
                let secondToBeArray = msg;
                let secondLastPost = secondToBeArray.last().id;

                receivedMessage.channel
                    .fetchMessages({ limit: 100, before: secondLastPost })
                    .then(msg => {
                        let thirdArray = msg;
                        let thirdLastPost = thirdArray.last().id;

                        receivedMessage.channel
                            .fetchMessages({ limit: 100, before: thirdLastPost })
                            .then(msg => {
                                let fourthArray = msg;
                            });
                    });
            });
    });

推荐答案

你可以做的是使用 async/await 函数和一个循环来发出顺序请求

What you can do is use an async/await function and a loop to make sequntial requests

async function lots_of_messages_getter(channel, limit = 500) {
    const sum_messages = [];
    let last_id;

    while (true) {
        const options = { limit: 100 };
        if (last_id) {
            options.before = last_id;
        }

        const messages = await channel.fetchMessages(options);
        sum_messages.push(...messages.array());
        last_id = messages.last().id;

        if (messages.size != 100 || sum_messages >= limit) {
            break;
        }
    }

    return sum_messages;
}

这篇关于获取超过 100 条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)