How to detect image uploads and respond automatically with Discord bot

Hey everyone! I’ve got a Discord bot that’s working pretty well. It can chat with users and I even added some cool features like weather updates and time info using slash commands. Everything’s going smooth so far.

But now I’m stuck on something. I want my bot to automatically detect when someone posts a picture in any channel and then send a message like “I see someone shared an image!” or something similar.

I’ve been searching around but most of the tutorials I found are really old and the code doesn’t seem to work anymore. Has anyone done this recently? What’s the best way to check for image attachments in messages and trigger a response?

Any help would be awesome, thanks!

just did this myself actually! the trick is using message.attachments.forEach() to loop through each attachment and check if its valid. make sure your bot has the right intents enabled tho - you need MESSAGE_CONTENT_INTENT or it wont see the attachments properly. also test it in a quiet channel first becuase it can get spammy real quick lol

I ran into this exact issue a few months back when building my moderation bot. The key is using the message event listener and checking the attachments property. You’ll want to set up an event handler that triggers on every new message, then check if message.attachments.size is greater than zero. From there you can filter by file extensions if needed - most image formats will have extensions like .png, .jpg, .gif etc. One thing to watch out for is that your bot might get rate limited if it’s responding to every single image in busy servers, so consider adding some cooldown logic or only responding in specific channels. The Discord.js documentation has been updated recently and their examples for handling attachments are pretty solid now.

Actually implemented this feature last week for my server’s art sharing channel. You need to hook into the messageCreate event and examine the message.attachments collection. What worked for me was checking both the attachment count and the content type using attachment.contentType to specifically target images rather than all files. Something like checking if contentType starts with ‘image/’ will catch most formats without having to hardcode extensions. Also worth mentioning that embeds containing images won’t trigger this since they’re not technically attachments, so if you want to catch those too you’ll need to check message.embeds as well. Performance wise it’s been running fine on my server with about 200 active users without any noticeable lag.