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
- 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!:method: Collection#filter()
@2.1.1
Identical to Array.filter(), but returns a Collection instead of an Array.
Thanks for the help @Qjuh