简体   繁体   中英

Discord.js Embed Random Image

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)

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);
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM