Locate (multiple) images in post and repost to different channel

I'm able to read the original message, locate the image URL and repost a single image. However, if multiple images get posted, I only return the first URL and corresponding image. I'm stuck... Forgive me if it's something silly, this is my 2nd day using discord.js. Using discord.js v14.16.3 and node v22.11.0.

``const { Client, IntentsBitField, MessageCollector, Collection } = require('discord.js');

const client = new Client({
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMembers,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent,
    ],
});

client.on('ready', (c) => { 
    console.log(
${c.user.username} is searching for profit posts...
)
});

//Update with proper channel ID
const sourceChannelId = ('1209261196142845994');
//Update with proper channel ID
const targetChannelId = ('1306686272919568405');

client.on('messageCreate', (message) => {

  //Prevent bot from messaging itself, looping, and sending reply to incorrect channel
  if (message.author.bot || message.channelId !== sourceChannelId) return;

  //Print data to terminal
  console.log(message.attachments.map(attachment => attachment.url));

  const discordId = (message.author.id);
  //Update with proper role ID
  const roleId = ('1306765206080192612')
  const targetChannel = client.channels.cache.get(targetChannelId);

  const imageAttachment = message.attachments.find(attachment => {
        return attachment.url.includes('.png') ||
               attachment.url.includes('.gif') ||
               attachment.url.includes('.jpeg') ||
               attachment.url.includes('.webp') ||
               attachment.url.includes('.jpg'); 
              });

              if(imageAttachment) {
                targetChannel.send({
                  content: 
<@&${roleId}> profit post from <@${discordId}>`,
files: [imageAttachment.url]});
}
});
Was this page helpful?