简体   繁体   English

Discord.js 随机图像

[英]Discord.js random image

Okay, so I have a reaction command that so far shows one image when the command is used like it is supposed to.好的,所以我有一个反应命令,到目前为止,当命令按预期使用时显示一个图像。 However, I have multiple images that I want it to choose from and show one randomly.但是,我有多个图像可供选择并随机显示一个。 Like how a math.random will choose a random sentence from a list.就像 math.random 从列表中随机选择一个句子一样。 Except I want images instead of sentences.除了我想要图像而不是句子。 However, I would like it to say ${message.author} gave ${user} a hug!但是,我想说 ${message.author} 给了 ${user} 一个拥抱! and then show the image, as how this code does:然后显示图像,就像这段代码的作用一样:

if(command === "hug") {
    if(message.mentions.members.size == 1) {
        let member = message.mentions.members.first()
        message.channel.send(`${message.author} gave ${member} a hug!`, {
            file: "https://media.giphy.com/media/CZpro4AZHs436/giphy.gif"
        });
    }
}

Again, I want it to take a random image from a list of images just like math.random does.同样,我希望它像 math.random 一样从图像列表中随机获取图像。

Say you have an array:假设你有一个数组:

const rando_imgs = [
'https://media.giphy.com/media/CZpro4AZHs436/giphy.gif',
'https://media.giphy.com/media/CZpro4AZHs436/giphy2.gif',
'https://media.giphy.com/media/CZpro4AZHs436/giphy3.gif',
]

You might pick from this array via:您可以通过以下方式从该数组中选择:

message.channel.send(`${message.author} gave ${member} a hug!`, {
    file: rando_imgs[Math.floor(Math.random() * rando_imgs.length)]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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