How can I program my Discord bot to respond to image uploads?

Hey everyone! I’m working on a Discord bot and I’ve got it doing some cool stuff already. It can chat with users and even fetch weather info for different zip codes using slash commands. But I’m stuck on one thing.

I want my bot to say something like ‘Looks like someone is sharing a pic!’ whenever a user posts an image in any channel. I’ve searched online but the solutions I found are pretty old and aren’t working for me.

Has anyone done this recently? What’s the best way to detect image uploads and make the bot respond? I’d really appreciate any tips or code snippets that could help me out with this. Thanks in advance!

I’ve implemented something similar recently. Here’s a more efficient approach:

@bot.event
async def on_message(message):
if any(attachment.content_type.startswith(‘image’) for attachment in message.attachments):
await message.channel.send(‘Looks like someone is sharing a pic!’)

This uses a generator expression to check for image attachments, which is more memory-efficient for large numbers of attachments. It also avoids unnecessary iteration once an image is found.

Consider adding a cooldown mechanism to prevent spam in active channels. You might also want to customize the response based on the image type or user posting it for a more engaging experience.

yo, i had the same issue! finally figured it out tho. you gotta use the on_message event and check for attachments. something like:

@bot.event
async def on_message(message):
if message.attachments:
await message.channel.send(‘pic alert!’)

hope that helps bro!

I’ve been working with Discord bots for a while, and here’s what I’ve found works well for detecting image uploads:

You’ll want to use the on_message event, as MiaDragon42 mentioned. However, it’s good to add some additional checks to ensure you’re only responding to actual images.

Here’s a more robust approach:

@bot.event
async def on_message(message):
if message.attachments:
for attachment in message.attachments:
if attachment.content_type.startswith(‘image’):
await message.channel.send(‘Looks like someone is sharing a pic!’)
break

This checks each attachment’s content type, so you’re not triggering on every file upload. It also breaks after the first image is detected to avoid multiple messages for multi-image posts.

Remember to handle permissions and rate limiting to avoid spamming channels. Good luck with your bot!