简体   繁体   中英

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. Except I want images instead of sentences. However, I would like it to say ${message.author} gave ${user} a hug! 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.

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

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