如何从数组中获取多个元素?

时间:2020-09-19 15:12:27

标签: discord.js

我正在使用discord.js,我想用密码创建一个“服务器”。我希望密码长6个字母,到目前为止我只能做一个。 它也必须是随机的。就像“ ZSHWJK”而不是“ AAAAAA”

代码:


var serverPasscode = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

module.exports = {
    name: "createserver",
    description: "Creates a server",
    run(message, args, client){
        const newServer = new MessageEmbed()
        .setTitle(`${message.author.name}'s server`)
        .setFooter(`${serverPasscode[Math.floor(Math.random() * serverPasscode.length)]}`)

        message.channel.send(newServer);
    }
};

现在这只能返回一个我不想要的元素。

1 个答案:

答案 0 :(得分:0)

您可以使用for循环并创建如下函数:

function makeGuildPassword(length) {
  var result = ''; // create empty string that you will add random characters to
  var characters = // list of characters (you can change this however you want)
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
  for (var i = 0; i < length; i++) { // create a loop
    // add a random character to the string; restart the loop
    result += characters.charAt(Math.floor(Math.random() * characters.length));
  }
  return result;
};

console.log(makeGuildPassword(6))