Skip to content
Advertisement

When logging deleted messages, how can I log the deleted image attachments reliably?

So my current event listener will log deleted messages just fine, the problem I’m having right now is that if the message that was deleted has an image attachment to it, most of the time it won’t display properly because the image url cannot be accessed anymore. You will just see the discord loading image forever.

I want to ask if there is a way to consistently display the deleted images. I have seen another discord bot display these images without fail, so it must be possible somehow, but I cannot figure out how. Maybe by downloading and re-uploading the image?

This is what I have right now:

 @commands.Cog.listener()
 async def on_message_delete(self, message):

     #some code....

     if message.attachments:
        if len(message.attachments) == 1:
            if message.attachments[0].url.endswith(('.jpg', '.png', '.jpeg', '.gif')):
                embed.set_image(url=message.attachments[0].url)
            else:
                embed.add_field(name="Attachment", value=message.attachments[0].url)
        
     #some more code

Thanks in advance, help is very much appreciated

Advertisement

Answer

I’ve just done a quick experiment and looked into how another bot does this, and I believe I’ve found you a solution. When an image is sent, the URL for it is on the CDN subdomain of discordapp.com. When deleted, it is removed from this place, but you can still access it through media.discordapp.net.

Example: https://cdn.discordapp.com/attachments/836128641808990239/855081455420047360/d46d2f3581fe4b335539669da5853ec3.jpg https://media.discordapp.net/attachments/836128641808990239/855081348220583956/d46d2f3581fe4b335539669da5853ec3.jpg

These are both the same image, but the bot logs the second URL. Hopefully this helps.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement