Discord.js嵌入随机图像

时间:2020-04-26 14:54:37

标签: javascript bots discord.js

我一直试图让我的不和谐机器人为我尝试创建的嵌入选择一个随机图像,但我只是想不通

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

我做错什么了吗? (关于javascript,我是一个完整的初学者,所以请给我一些放松)

2 个答案:

答案 0 :(得分:0)

假设这些ImageN占位符是图像的链接,请使用此

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

答案 1 :(得分:0)

image变量只是索引,所以我建议将图像重命名为index,然后将其更改为setImage(images[index])或将图像重估为images[Math.floor(Math.random * images.length)],这样您就可以执行setImage(image)

我不确定为什么其他评论者这么做了String([images[image]]),在这里不需要

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