Wednesday, 5 April 2017

discord bot custom command JS

I'm trying to code a simple Discord bot with the Discord,js-commando library I got most of it working so far but I'm stuck at the moment with a problem so i got this command where you can do !roll and it will random choose a number from 1 to 6 but I wanted to make it little bit more custom so here is what it looks like

!roll (Rolls from 1 to 6)

!roll 25 (Rolls from 1 to 25)

!roll 100 200 (Rolls from 100 to 200)

the problem is when I try to do the !roll 25 my validation keeps saying it's not a valid number but all the other commands works just fine it only happens when I do !roll and then some number I can't figure out why it doesn't work it's probably an easy fix thanks in advance

const commando = require('discord.js-commando')
const _ = require('lodash')

 class DiceRollCommand extends commando.Command {

    constructor(bot) {
        super(bot, {
            name: 'roll',
            group: 'random',
            memberName: 'roll',
            description: 'Rolls a dice.'
        })
    }

    async run(message, args) {
        let roll = args.split(' ')
        let hasNumber = /^[0-9]$/

        if (roll[0] || roll[1]) {
            if (!hasNumber.test(roll[0]) || !hasNumber.test(roll[1])) {
                console.log('roll[1] -> '+ !hasNumber.test(roll[0])) // returns true
                console.log('roll[2] -> '+ !hasNumber.test(roll[1])) // returns true

                message.reply('[DEBUG] Syntax Error input must be a number')
                return
            }
        }
        if (roll.length >= 3) {
            message.reply('[DEBUG] Syntax Error cannot use more than 2 parameters')
            return
        }
        if (roll[0] > 1000000 || roll[1] > 1000000) {
            message.reply('Unfortunately for you, computers have a limited amount of memory, so unless you want me to run out, stop sending ludicrous numbers. Thanks.')
            return
        }
       if (message.content.match(/^!roll$/)) {
           message.reply('rolled ' + _.random(1, 6))
       }
       if (message.content.match(/^!roll [0-9]+\b/)) {
           message.reply('rolled ' + _.random(1, roll[0]))
       }
       if (message.content.match(/^!roll ([0-9]*) ([0-9]*)+\b/)) {
           message.reply('rolled ' + _.random(roll[0], roll[1]))
       }

    }

}

module.exports = DiceRollCommand



via John

No comments:

Post a Comment