Home > Back-end >  Loop to workaround 100 message fetch limit no longer working after 1 day
Loop to workaround 100 message fetch limit no longer working after 1 day

Time:04-17

I created a discord bot yesterday from this post to export fields from embeds and it was working fine but it's not working today. The error I'm getting is TypeError: messages.array is not a function I tried uninstalling node and reinstalling but to no avail. I sent the file to a friend to run and it worked perfectly so it seems to be a problem with my machine. I also tried to run it on my windows server and I did not see any errors but the bot failed at exporting the fields (I don't intend on using my server to run the bot, just used for troubleshooting). Has anyone seen this error before and/or have a solution?

More detailed Error (One message):

"xxxx@xxxx-mbp embed bot % node main /Users/xxxx/Downloads/embed bot/main.js:24 sum_messages.push(...messages.array()); ^

TypeError: messages.array is not a function at lots_of_messages_getter (/Users/xxxx/Downloads/embed bot/main.js:24:39) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Client. (/Users/xxxx/Downloads/embed bot/main.js:36:20)"

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

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

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

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

return sum_messages;

}

CodePudding user response:

Messages is a Collection, there is no array method for that object.

According to the documentation you can convert it to a regular array and then use the spread operator.

sum_messages.push(...Array.from(messages));

CodePudding user response:

I changed “array” to “values” and it works perfectly now. Thanks to everyone that helped!

  • Related