I’m having trouble getting my Discord bot to randomly select and display images from an array. I’ve been working on this for a while and can’t figure out what’s wrong. When I test it with just one image, everything works perfectly fine. But when I try to make it choose randomly from multiple images, something goes wrong.
I think there might be an issue with how I’m handling the random selection or sending the message. Any ideas what could be causing this problem? I’m still learning JavaScript so maybe I’m missing something obvious.
quick tip - just remove those square brackets around math.floor. you’re making selectedImage an array, but you need the image path. also, discord requires MessageAttachment to send images, so make sure to use that instead of just sending the path.
The bug is those extra brackets around your random number generator. You’re creating an array with just the index instead of using it to grab the actual image path.
But honestly - managing Discord bots with file uploads and random selections gets messy fast when you code it all manually. Been there.
I use Latenode now. Set up a workflow that handles random selection, manages your image library, and connects to Discord automatically. No more debugging array syntax or file path headaches.
Best part? You can expand it later without touching code. More images? Update the workflow. Different image categories? Easy. Track which images get sent most? Built in.
Set up something similar for our Slack bot and saved hours of maintenance. The visual workflow makes everything clear, and you can test each step separately.
Had the exact same problem building my first bot. Your random selection’s broken, plus you’re not sending files right. You can’t just pass file paths as parameters - Discord.js needs attachment objects for images. Try this instead: const Discord = require('discord.js'); const attachment = new Discord.MessageAttachment(imageList[Math.floor(Math.random() * imageList.length)]); msg.channel.send('woof', attachment); Also double-check those file paths actually exist. I got burned by that so many times starting out.
You’re wrapping the index in square brackets instead of using it to grab the actual image. When you write var selectedImage = [Math.floor(Math.random() * (imageList.length))];, you’re making an array with just the index number - not getting the image path itself. Use that index to grab the image from your array: var selectedImage = imageList[Math.floor(Math.random() * imageList.length)];. Then send it as a Discord.js attachment, not as a second parameter. The send method wants a string message and attachment object, not a file path dumped in as a parameter.
you’re using square brackets around selectedImage when you should use imageList[selectedImage]. right now you’re just sending the index number, not the actual image path.