I got this function from another stackoverflow question. I fixed it to work with Discord.js v12 by changing channel.fetchMessages
to channel.messages.fetch
. The function worked at first and everything was fine, but then one time when I started up my program it started showing this error: "TypeError: Cannot read property 'id' of undefined" This error occurs at line 55 which is last_id = messages.last().id;
I did not change the function at all and it just stopped working. Any ideas?
async function lots_of_messages_getter(channel, limit = 6000) {
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;
}