【问题标题】:Discord JS - Command to output randomized array value as commandDiscord JS - 将随机数组值作为命令输出的命令
【发布时间】:2021-01-18 12:15:42
【问题描述】:

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

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);

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

【问题讨论】:

    标签: javascript arrays discord


    【解决方案1】:

    它总是相同的值,因为你有消息侦听器的释放。

    你需要有这个:

    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!');
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2021-01-31
      • 2019-04-19
      • 2020-05-11
      • 2021-01-06
      • 2021-04-02
      • 2021-12-13
      • 1970-01-01
      • 2020-10-05
      相关资源
      最近更新 更多