![](/img/trans.png)
[英]How to make multiple random alphanumeric strings with one const array in a node.js discord bot
[英]node.js - Is there a way to make a discord bot send random pictures pulled from a file? node.js
所以我是编码新手,我想制作一个 discord 机器人,我知道如何去做,但我不知道如何去做我想做的事。 我想知道是否有一种方法可以将一堆照片添加到文件中,并让 discord 机器人能够从中提取一张随机图片进行发送? 以及可能会拉出 5 个随机的并发送它们?
我不知道该怎么做,也没有找到任何帮助。
目前我正在做一个我必须自己添加每个图像 ID 并为每个命令编写非常长的代码行的方法。
有任何想法吗???
您可以使用Math.random()
从图像数组中选择一个随机图像。
const imageArray = [...];
const randomImage = imageArray[Math.floor(Math.random()*imageArray.length)];
let arr = ["a", "b", "c"];
Object.defineProperty(arr, "random", {
get: () => {
return arr[Math.floor(Math.random()*arr.length)];
}
});
console.log(arr.random);
(新的编辑,因为我之前的回答似乎不适合某些人)
您可以将文件存储在一个数组中并创建一个 object 来调用随机逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.