首页 > 解决方案 > 选择机器人滚动的次数(Discord.js)

问题描述

现在我正在研究一个随机数生成器,就像一个虚拟的掷骰子。我让用户输入一个数字,机器人随机选择一个介于 1 和用户输入的任何数字之间的数字。我一直在尝试添加一种方式,让用户首先说出他们希望它滚动多少次。我对如何使用我已经拥有的代码执行此操作感到非常困惑。

快速运行:我希望机器人在用户发送最大滚动数后询问我希望机器人询问他们想要滚动多少次。

代码:

const Discord = require('discord.js');

const client = new Discord.Client();

function getRandomInt(max){
    return Math.floor(Math.random() * Math.floor(max));
}

const prefix = 'roll';

client.once('ready', () => {
    console.log('your bot name is online!');
    client.user.setActivity("Type 'Role Help'", {type: 3}); 
});

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

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

    if(command === ''){
       message.channel.send("What's the **Max** role number you want?");
       message.channel.awaitMessages(m => m.author.id == message.author.id,
        {max: 1, time: 10000}).then(collected => {
            console.log('collected :' + collected.first().content);
            if (isNaN(collected.first().content)){
                return message.channel.send("Please provide a valid number!")};
            
            var n = (getRandomInt(collected.first().content));  
            message.channel.send("You Rolled a " + (n + 1));
            }).catch(() => {
                message.channel.send("*Roll has been cancelled.*");
     
            })} else if(command === 'Help' || 'help' || 'h' || 'H'){

                message.channel.send("**How to use Roll Bot**");
                message.channel.send("*1. Type Roll to begin*");
                message.channel.send("*2. Input a number or type stop to end the roll.*");
                message.channel.send("**Have Fun!**");
        }
});

client.login(process.env.token);

标签: javascriptnode.jsdiscorddiscord.js

解决方案


推荐阅读