I am trying to create a script that fetches messages from an API and then filters out messages that contain an image. The API has different endpoints, each with their own batch of messages. I am simply trying to collect all of them. In this example there is 3 links that's why it logs out "test" three times.
My issue is that the messageGetter() function doesn't fully finish looping before the broadcast() function is called. So right now when I execute the script for the first time it broadcasts an empty array but on the second execution it has the contents I need.
How could I fix this?
module.exports = {
  async execute(app) {
    await fetchImages(app);
    await interaction.reply("Getting Images");
  },
};
async function fetchImages(app) {
  app.link.forEach(async (link) => {
    if (link.type === 0) {
      await messageGetter(link);
    }
  });
  console.log("here");
  broadcast({ type: "imageLinks", data: imageLinks });
  imageLinks.splice(0, imageLinks.length);
}
async function messageGetter(link, limit = 500) {
  const sum_messages = [];
  let last_id;
  while (true) {
    const options = { limit: 100 };
    if (last_id) {
      options.before = last_id;
    }
    const messages = await link.messages.fetch(options);
    sum_messages.push(...messages.values());
    last_id = messages.last().id;
    if (messages.size != 100 || sum_messages >= limit) {
      break;
    }
  }
  sum_messages.forEach((message) => {
    if (message.attachments.size > 0) {
      message.attachments.forEach((attachment) => {
        if (attachment.url.match(/\.(jpeg|jpg|gif|png)$/i)) {
          imageLinks.push(attachment.url);
        }
      });
    }
  });
console.log("test");
}
Log Output:
here
test
test
test
 
    