Hey everyone! I’m pulling my hair out trying to get my Discord bot to log deleted images. It’s supposed to track both deleted messages and images, but it’s only picking up on the text messages. I’ve been at it for hours and I’m stumped.
Here’s a snippet of what I’ve got so far:
const Discord = require('discord.js')
module.exports = async (bot, msg) => {
if (!msg.guild || !msg.content) return;
const logChannel = msg.guild.channels.find(ch => ch.name === 'delete-log');
if (!logChannel) {
console.log(`Warning: No log channel in ${msg.guild.name}`);
return;
}
if (msg.attachments.size > 0) {
msg.attachments.forEach(att => {
const embed = new Discord.RichEmbed()
.setAuthor(msg.author.tag, msg.author.avatarURL)
.setDescription(`Image deleted by ${msg.author.tag} in #${msg.channel.name}`)
.setImage(att.url)
.setColor('#FF0000')
.setTimestamp()
logChannel.send(embed);
});
} else {
// Code for text messages...
}
}
Any ideas on what I’m doing wrong? Thanks in advance for any help!
I encountered a similar issue with tracking deleted images on my Discord bot. In my experience, the problem lies in the fact that Discord invalidates the attachment URL almost immediately once an image is deleted, making it inaccessible when the deletion event fires. One approach that worked for me was to cache the attachment URLs when the messages are initially sent, then reference the cache if a message is later deleted. This solution does require careful memory management and a method to clean up old cache entries over time, but it significantly improves reliability.
The problem you’re encountering is often due to Discord invalidating attachment URLs immediately once an image is deleted. One effective workaround is to save the URL in a cache when the message is first sent. Then, if a deletion occurs, you can check the cache for the message’s ID and use the stored URL to build your embed. This method improves your ability to log deleted images consistently, but it is important to periodically clean the cache to prevent memory leaks. In my experience, this approach has significantly increased the bot’s reliability.
hey luna, have u tried using message.fetchAttachments() before trying to access the attachments? sometimes the attachments arent immediately available when the deletion event fires. also, make sure ur bot has the necessary permissions to view deleted messages in the server. those two things fixed it for me when i had a similar issue