The problem’s likely your conditional check. startswith('dog') only triggers if the message starts with ‘dog’, but you want it to trigger whenever someone mentions dog. Change it to if 'dog' in message.content.lower(): - this will catch the word anywhere in the message, regardless of capitalization. I’ve faced a similar issue when I first started building my bot. These trigger conditions can be tricky. Additionally, ensure that your bot has the necessary file permissions for the channel you are testing in.
You’re likely missing import random at the beginning of your script. Additionally, verify your file paths; if your images are in a subfolder, reference them correctly like 'images/dog1.JPG'. I’ve encountered this same issue when I was starting out with discord.py file handling. Also, consider adding if message.author == client.user: return at the start of your on_message function to prevent the bot from replying to its own messages, which can lead to unexpected behavior.
Your code structure works, but file paths and random selection get messy quick when you scale up.
I hit this same problem building internal tools. Instead of handling files in Python, I moved everything to Latenode. Set up a webhook that triggers on Discord messages, check for “dog”, then randomly pick from image URLs in a database or Google Sheet.
Best part? No worrying about local paths, server storage, or keeping your bot running 24/7. Latenode handles Discord API calls, random selection, and image sending automatically.
You can add more images by just updating your data source - no code changes needed. Way cleaner than hardcoding filenames in arrays.
hey jackhero, your code looks good but check if those image files are actually in the same directory as your bot script. add some error handling around the random.choice part too - if the files don’t exist, that’s probably why it’s breaking
Check your file extensions and capitalization - I wasted hours debugging because my files were .jpg but my code had .JPG. Python’s case-sensitive on most systems. Also caught me: running the bot from a different directory than where I stored the images. Use absolute paths like '/full/path/to/dog1.JPG' first to confirm it works, then sort out the relative path stuff. discord.File() gets picky about finding files if you’re not in the right directory when running the script.
Skip hardcoding filenames - use os.listdir() to grab all images from a folder instead. Saved me hours when I added more images to my bot. Make a ‘dog_images’ folder and use random.choice(os.listdir('dog_images/')). Just filter for actual image files like .jpg, .png. Also check your bot’s file permissions on that directory. I had silent failures because the bot couldn’t access certain folders on my server.
wrap it in a try/except block - discord.py can get weird with file handling. also, make sure your image sizes aren’t too big since discord will just silently fail if the files are oversized.