Javascript 如何从discord.js中的文件夹中随机选取图片

Javascript 如何从discord.js中的文件夹中随机选取图片,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我不确定如何从选定文件夹中随机选取图像。这是我到目前为止得到的 const fs = require('fs') const verifcationFile = fs.readdirSync('./samples'); const length = verifcationFile .length; const getRandomIndex = length => Math.floor( Math.random() * length ); var file = ? const atta

我不确定如何从选定文件夹中随机选取图像。这是我到目前为止得到的

const fs = require('fs')
const verifcationFile = fs.readdirSync('./samples');
const length = verifcationFile .length;
const getRandomIndex = length => Math.floor( Math.random() * length );


var file = ?

const attachment = new Discord
  .MessageAttachment(`./sample/${file}`, `${file}`);
const embed = new Discord.MessageEmbed()
     .setTitle('A picture!')
     .attachFiles(attachment)
     .setImage('attachment://${file}');

message.channel.send({embed});


我目前正在使用discord.js V12

//获取此文件夹中每个文件的数组
const images=fs.readdirSync('./sample');
//从数组中获取随机元素
const file=images[Math.floor(Math.random()*images.length)];
const attachment=new Discord.MessageAttachment(`./sample/${file}`,file);

在运行Discord.js回调之前,请确保读取所有文件

//读取Discord.js回调之外的所有文件
常量fs=require('fs')
const-verifyle=fs.readdirSync(“./samples”);
//函数随机选择一个文件
常量randomVeriPath=()=>
可验证的[Math.floor(Math.random()*verifyle.length]);
client.on(“message”,()=>{
const file=randomVeriPath();
const attach=new Discord.MessageAttachment(`./sample/${file}`、`${file}`);
...
})