Attaching already existing attachments to message

Hello there. So, as the title suggests, I want to attach already-existing message attachments on a new message. I am making a messageDelete event handler and I want to attach any files that have been deleted to the log message. Here is the code that I have so far:
Cog.add = new Event("messageDelete", async (message) => {
if (!logChannel) {logChannel = await message.client.channels.fetch(logChannelId)}
if (message.author.bot) {return}

const embed = new EmbedBuilder()
.setAuthor({name: message.author.tag, iconURL: await message.author.displayAvatarURL()})
.setTitle("Message Deleted")
.setDescription(`**Message sent by ${message.author} deleted in ${message.channel}**${(message.content) ? ":\n" + message.content : "."}`)
.setColor("#ca1919")
.setTimestamp()

await logChannel.send({embeds: [embed, ...message.embeds], files: [...message.attachments], stickers: [...message.stickers]})
})
Cog.add = new Event("messageDelete", async (message) => {
if (!logChannel) {logChannel = await message.client.channels.fetch(logChannelId)}
if (message.author.bot) {return}

const embed = new EmbedBuilder()
.setAuthor({name: message.author.tag, iconURL: await message.author.displayAvatarURL()})
.setTitle("Message Deleted")
.setDescription(`**Message sent by ${message.author} deleted in ${message.channel}**${(message.content) ? ":\n" + message.content : "."}`)
.setColor("#ca1919")
.setTimestamp()

await logChannel.send({embeds: [embed, ...message.embeds], files: [...message.attachments], stickers: [...message.stickers]})
})
Now, when I delete a message with an image attached, I get the following error:
TypeError: Cannot read properties of undefined (reading 'path')
at findName (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:244:17)
at MessagePayload.resolveFile (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:258:31)
at C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:225:85
at Array.map (<anonymous>)
at MessagePayload.resolveFiles (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:225:56)
at TextChannel.send (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:50)
at Client.<anonymous> (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\Cogs\Misc\generalLogs.js:32:22)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of undefined (reading 'path')
at findName (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:244:17)
at MessagePayload.resolveFile (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:258:31)
at C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:225:85
at Array.map (<anonymous>)
at MessagePayload.resolveFiles (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\MessagePayload.js:225:56)
at TextChannel.send (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:50)
at Client.<anonymous> (C:\Users\Batman212369\Desktop\Discord Bots\RHS-Bot V2\Cogs\Misc\generalLogs.js:32:22)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I really don't understand why this is a problem. I did think that it might be because I am not using an AttachmentBuilder, but the wiki clearly states that Array <Attachment> is also a viable option for the files argument. If someone could help me, that would be awesome. Thank you for your time and help, it is truly appreciated.
6 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Batimius
BatimiusOP2y ago
Alright, so right after posting, I figured out that the problem is that message.attachments is a Collection. Using Object.map(message.attachments) seems to suppress the error, but it won't attach the image.
d.js docs
d.js docs2y ago
MessageEmbed#attachFiles has been removed. Files should be attached via the message option object instead:
const attachment = new MessageAttachment('./image.png', 'image1.png');
const embed = new MessageEmbed()
- .attachFiles([attachment])
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

- channel.send(embed)
+ channel.send({
+ embeds: [embed],
+ files: [attachment]
+ });
const attachment = new MessageAttachment('./image.png', 'image1.png');
const embed = new MessageEmbed()
- .attachFiles([attachment])
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

- channel.send(embed)
+ channel.send({
+ embeds: [embed],
+ files: [attachment]
+ });
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Batimius
BatimiusOP2y ago
My intelligence strikes once more. Object.values() does not work on Collections. I used message.attachments.values() and it worked perfectly. Worked, I just had to use message.attachments.values(). Here's how the final line looks (for anyone who might have the same issue in the future):
await logChannel.send({embeds: [embed, ...message.embeds], files: [...message.attachments.values()], stickers: [...message.stickers]})
await logChannel.send({embeds: [embed, ...message.embeds], files: [...message.attachments.values()], stickers: [...message.stickers]})
Regardless, thank you for your help, it is truly appreciated.
d.js docs
d.js docs2y ago
MessageEmbed#attachFiles has been removed. Files should be attached via the message option object instead:
const attachment = new MessageAttachment('./image.png', 'image1.png');
const embed = new MessageEmbed()
- .attachFiles([attachment])
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

- channel.send(embed)
+ channel.send({
+ embeds: [embed],
+ files: [attachment]
+ });
const attachment = new MessageAttachment('./image.png', 'image1.png');
const embed = new MessageEmbed()
- .attachFiles([attachment])
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

- channel.send(embed)
+ channel.send({
+ embeds: [embed],
+ files: [attachment]
+ });
Want results from more Discord servers?
Add your server