Fetch all messages in a text channel

Hey there! I'm trying to fetch all messages in a channel for my /initialize command. I'm looking at the faq example:

async function fetchNine(manager) {
  const result = []; // this will combine the results of each fetch call
  let pivot = null;
  while (result.length < 9) {
      // fetch a pack
      // if no pivot is available, pass undefined so the "before" parameter is ignored
      const pack = await manager.fetch({limit: 3, before: pivot?.id});
      // move the starting point to the current pack
      pivot = pack.last();
      // add the pack to the result
      pack.forEach(entry => result.push(entry));
  }
  return result;
}


I think I need to modify the while conditional. The amount of messages in the channel is dynamic, so I can't base it off that. I don't want to manually copy the id of the oldest message either. I was thinking about approaching it by timestamp or by checking if there's a previous message before running it again. Are there any examples of this / suggestions? Thanks!
Was this page helpful?