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]}); } });
3 Replies
d.js toolkit
d.js toolkit2d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs2d ago
:method: Collection#filter() @2.1.1 Identical to Array.filter(), but returns a Collection instead of an Array.
collection.filter(user => user.username === 'Bob');
collection.filter(user => user.username === 'Bob');
Travisᵐᵒᵐᵒ⨳ᵀᴬ
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 = ('1306013045021212724');

// Update with proper channel ID
const targetChannelId = ('607218826983243779');

// Send Message
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);

// Update with proper role ID
const roleId = ('1073347163570045031');

// Filter images that start with 'image'
const imageAttachments = message.attachments.filter(attachment => attachment.contentType && attachment.contentType.startsWith('image/'));

// Map to attachment.url
const attachmentURLs = imageAttachments.map(attachment => attachment.url);
const allImageURLs = [...attachmentURLs];

// Validation
const targetChannel = client.channels.cache.get(targetChannelId);
const discordId = (message.author.id);

if (allImageURLs.length) {
console.log('Located profit post URL(s):', [allImageURLs]);
targetChannel.send({
content: `<@&${roleId}> profit post from <@${discordId}>`,
files: [...attachmentURLs]});
}
});
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 = ('1306013045021212724');

// Update with proper channel ID
const targetChannelId = ('607218826983243779');

// Send Message
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);

// Update with proper role ID
const roleId = ('1073347163570045031');

// Filter images that start with 'image'
const imageAttachments = message.attachments.filter(attachment => attachment.contentType && attachment.contentType.startsWith('image/'));

// Map to attachment.url
const attachmentURLs = imageAttachments.map(attachment => attachment.url);
const allImageURLs = [...attachmentURLs];

// Validation
const targetChannel = client.channels.cache.get(targetChannelId);
const discordId = (message.author.id);

if (allImageURLs.length) {
console.log('Located profit post URL(s):', [allImageURLs]);
targetChannel.send({
content: `<@&${roleId}> profit post from <@${discordId}>`,
files: [...attachmentURLs]});
}
});
Thanks for the help @Qjuh
Want results from more Discord servers?
Add your server