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?