Creating flexible dice rolling command for Discord bot using JavaScript

Need help with Discord bot dice command validation

I’m building a Discord bot using discord.js-commando and I’m having trouble with my dice rolling feature. The bot should handle these three scenarios:

  • !dice (generates random number 1-6)
  • !dice 50 (generates random number 1-50)
  • !dice 10 30 (generates random number 10-30)

The issue is with the second format. When I type !dice 50, my number validation fails and shows an error message. The other two formats work perfectly. I think there’s something wrong with my regex pattern but I can’t spot the issue.

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

class RandomNumberCommand extends commando.Command {

    constructor(client) {
        super(client, {
            name: 'dice',
            group: 'utility',
            memberName: 'dice',
            description: 'Generates random numbers.'
        })
    }

    async run(msg, arguments) {
        let params = arguments.split(' ')
        let numberPattern = /^[0-9]$/

        if (params[0] || params[1]) {
            if (!numberPattern.test(params[0]) || !numberPattern.test(params[1])) {
                console.log('param1 -> '+ !numberPattern.test(params[0])) // shows true
                console.log('param2 -> '+ !numberPattern.test(params[1])) // shows true

                msg.reply('[ERROR] Invalid input - numbers only please')
                return
            }
        }
        if (params.length >= 3) {
            msg.reply('[ERROR] Too many arguments - maximum 2 allowed')
            return
        }
        if (params[0] > 1000000 || params[1] > 1000000) {
            msg.reply('Numbers too large! Please use smaller values to avoid memory issues.')
            return
        }
       if (msg.content.match(/^!dice$/)) {
           msg.reply('result: ' + utils.random(1, 6))
       }
       if (msg.content.match(/^!dice [0-9]+\b/)) {
           msg.reply('result: ' + utils.random(1, params[0]))
       }
       if (msg.content.match(/^!dice ([0-9]*) ([0-9]*)+\b/)) {
           msg.reply('result: ' + utils.random(params[0], params[1]))
       }

    }

}

module.exports = RandomNumberCommand

Any ideas what could be causing this validation problem?

The problem is in your regex pattern /^[0-9]$/ - it only matches a single digit, not multi-digit numbers. That’s why !dice 50 fails validation since “50” has two digits. Change your numberPattern to /^[0-9]+$/ to match one or more digits. Also, there’s a logical issue in your validation - you’re checking both params even when only one is provided. For the single parameter case like !dice 50, params[1] will be undefined, and testing undefined against your regex will always fail. You should modify the validation to check if the parameter exists before testing it: if (params[0] && !numberPattern.test(params[0])) { // handle error } if (params[1] && !numberPattern.test(params[1])) { // handle error } This way you’re only validating parameters that actually exist in the input.

I ran into this exact same issue when building my first Discord bot. The regex pattern definitely needs fixing as others mentioned, but there’s another problem - your parameter splitting logic is flawed. When you use arguments.split(' ') on a single argument like ‘50’, you get an array with one element, but your validation code assumes both params[0] and params[1] exist. What helped me was restructuring the validation entirely. Instead of checking both parameters upfront, validate them based on array length first. Something like checking if (params.length === 1 && !numberPattern.test(params[0])) for single param cases, then if (params.length === 2 && (!numberPattern.test(params[0]) || !numberPattern.test(params[1]))) for dual param cases. This approach prevented the undefined parameter validation errors I was getting. Also worth noting that your current regex will fail on any number with more than one digit, so definitely change it to /^[0-9]+$/ to match multiple consecutive digits.

your regex is mising the + quantifier - /^[0-9]$/ only matches single digits but /^[0-9]+$/ matches multiple digits like 50. thats why validation fails on two digit numbers