Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
const { SlashCommandBuilder } = require("discord.js");
const { imageLinks, broadcast } = require("../../server.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("create_library")
    .setDescription("Creates Image Library"),
  async execute(interaction) {
    await fetchImages(interaction);
    await interaction.reply("Getting Images");
  },
};

async function fetchImages(interaction) {
  interaction.guild.channels.cache.forEach(async (channel) => {
    if (channel.type === 0) {
      await lots_of_messages_getter(channel);
    }
  });
  console.log(imageLinks);
  broadcast({ type: "imageLinks", data: imageLinks });
  imageLinks.splice(0, imageLinks.length);
}

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

  return sum_messages;
}
Editor is loading...