Reputation: 3
I've been trying to get my discord bot to pick a random image for this embed I'm trying to create but I just can't figure it out
var images = ["Image1", "Image2", "Image3", "Image4" ];
var image = Math.floor(Math.random() * images.length);
if (command === `randompic`) {
let random = new Discord.MessageEmbed()
.setTitle('Here is your random pic')
.setAuthor('mortis')
.setImage(+images[image])
message.channel.send(random);
}
Am I doing something wrong? (I'm a complete beginner when it comes to javascript so cut me some slack)
Upvotes: 0
Views: 8171
Reputation:
the image variable is just the index, so I suggest either renaming image to index and then change it to setImage(images[index])
or revaluing image to images[Math.floor(Math.random * images.length)]
, this way you can just do setImage(image)
I'm not sure why the other commenter did String([images[image]])
, don't need it here
const images = ["Image1", "Image2", "Image3", "Image4" ];
const image = images[Math.floor(Math.random() * images.length)];
if (command === `randompic`) {
const random = new Discord.MessageEmbed()
.setTitle('Here is your random pic')
.setAuthor('mortis')
.setImage(image)
message.channel.send(random);
}
Upvotes: 1
Reputation:
Assuming these ImageN
placeholders are links to images, use this
var images = ["Image1", "Image2", "Image3", "Image4" ];
var image = Math.floor(Math.random() * images.length);
if (command === `randompic`) {
let random = new Discord.MessageEmbed()
.setTitle('Here is your random pic')
.setAuthor('mortis')
.setImage(String([images[image]]))
message.channel.send(random);
}
Upvotes: 0