Javascript Discord JS-将随机化数组值作为命令输出的命令

Javascript Discord JS-将随机化数组值作为命令输出的命令,javascript,arrays,discord,Javascript,Arrays,Discord,我目前正在尝试为我的机器人创建一个命令,每次运行该命令时,该命令都会从以下数组中随机给出答案: const valMapsList = ['Ascent', 'Bind', 'Split', 'Haven']; 我试过这样做: const Discord = require('discord.js'); const client = new Discord.Client(); const prefix = '>!'; client.once('ready', () => {

我目前正在尝试为我的机器人创建一个命令,每次运行该命令时,该命令都会从以下数组中随机给出答案:

const valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];
我试过这样做:

const Discord = require('discord.js');
const client = new Discord.Client();

const prefix = '>!';

client.once('ready', () => {
    console.log('Bot is now ONLINE!')
});

let valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];
let map = valMapsList[Math.floor(Math.random() * valMapsList.length)];

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'map'){
        message.channel.send("Selected Map: " + map);
    } else if (command == 'ping') {
        message.channel.send('Pong!');
    }
});
这是可行的,但只会给出相同的答案,因为代码只是在启动时执行的。所以我需要一个函数,我可以在

if(command === 'map'){
            message.channel.send("Selected Map: " + map);

将重新运行随机化的部分。

它始终是相同的值,因为您有消息侦听器发出的消息

您需要具备以下功能:

const Discord = require('discord.js');
const client = new Discord.Client();

const prefix = '>!';

client.once('ready', () => {
    console.log('Bot is now ONLINE!')
});

const valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];


client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'map'){
        let map = valMapsList[Math.floor(Math.random() * valMapsList.length)];
        message.channel.send("Selected Map: " + map);
    } else if (command == 'ping') {
        message.channel.send('Pong!');
    }
});