I'm trying to fetch more than 100 messages in DiscordJS. I've found this code here but it doesn't work:
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.fetch(options);
sum_messages.push(...messages.array());
last_id = messages.last().id;
if (messages.size != 100 || sum_messages >= limit) {
break;
}
}
return sum_messages;
}
client.on("message", async message => {
const channel = client.channels.cache.get("12345");
if (message.content.startsWith(prefix+"random")){
console.log(lots_of_messages_getter());
}
});
It gives me this error:
(node:6312) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetch' of undefined
How can this be fixed? I'm kinda new to Node.js.