Skip to content Skip to sidebar Skip to footer

Fetch More Than 100 Messages

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 figur

Solution 1:

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

Solution 2:

This works for me using discord.js v 11.5.1 and TypeScript. It is an updated version of Jason's post.

The reason I used this is because: DiscordAPI limit maxes out at 100 for fetching messages and the deprecated TextChannel#fetchMessages() method no longer exists.

I updated it to use the TextChannel#messages object's fetch(options?: ChannelLogsQueryOptions, cache?: boolean) method to fetch Collections of 100 or less messages.

async function getMessages(channel: TextChannel, limit: number = 100): Promise<Message[]> {
  let out: Message[] = []
  if (limit <= 100) {
    let messages: Collection < string, Message > = await channel.messages.fetch({ limit: limit })
    out.push(...messages.array())
  } else {
    let rounds = (limit / 100) + (limit % 100 ? 1 : 0)
    let last_id: string = ""
    for (let x = 0; x < rounds; x++) {
      const options: ChannelLogsQueryOptions = {
        limit: 100
      }
      if (last_id.length > 0) {
        options.before = last_id
      }
      const messages: Collection < string, Message > = await channel.messages.fetch(options)
      out.push(...messages.array())
      last_id = messages.array()[(messages.array().length - 1)].id
    }
  }
  return out
}

Post a Comment for "Fetch More Than 100 Messages"