javascript Discord.js 随机图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49677884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Discord.js random image
提问by Vampy Maria
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:
好的,所以我有一个反应命令,到目前为止,当命令按预期使用时,它会显示一个图像。但是,我有多个图像可供选择并随机显示一个。就像 math.random 从列表中随机选择一个句子一样。除了我想要图像而不是句子。但是,我想说 ${message.author} 给了 ${user} 一个拥抱!然后显示图像,就像这段代码的作用一样:
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 一样从图像列表中随机获取图像。
采纳答案by Vampy Maria
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)]
});

