I’m in the process of creating a Discord bot with the Discord.js-commando library, and I’m facing a challenge with my dice rolling command. The basic functionality is operational, but when I try to execute it with a single number, the validation incorrectly flags it as non-numeric.
Here’s what I’m aiming for:
!roll
- generates a number between 1 and 6!roll 30
- rolls from 1 to 30!roll 50 100
- generates a number between 50 and 100
The problem arises with the command !roll 30
. My validation is not recognizing it as a valid number, while the other command formats work correctly. I suspect there might be a straightforward solution that I’m overlooking.
const commando = require('discord.js-commando');
const _ = require('lodash');
class MyDiceCommand extends commando.Command {
constructor(bot) {
super(bot, {
name: 'roll',
group: 'random',
memberName: 'roll',
description: 'Rolls a dice randomly.'
});
}
async run(message, args) {
let rollArgs = args.split(' ');
let validNumber = /^[0-9]+$/;
if (rollArgs[0] || rollArgs[1]) {
if (!validNumber.test(rollArgs[0]) || !validNumber.test(rollArgs[1])) {
console.log('rollArgs[0] -> ' + !validNumber.test(rollArgs[0])); // should show true
console.log('rollArgs[1] -> ' + !validNumber.test(rollArgs[1])); // should show true
message.reply('Error: Please enter a valid number.');
return;
}
}
if (rollArgs.length >= 3) {
message.reply('Error: You can only provide up to 2 parameters.');
return;
}
if (rollArgs[0] > 1000000 || rollArgs[1] > 1000000) {
message.reply('Please use smaller numbers, as very large values may cause issues.');
return;
}
if (message.content.match(/^!roll$/)) {
message.reply('You rolled: ' + _.random(1, 6));
}
if (message.content.match(/^!roll [0-9]+/)) {
message.reply('You rolled: ' + _.random(1, rollArgs[0]));
}
if (message.content.match(/^!roll ([0-9]+) ([0-9]+)$/)) {
message.reply('You rolled: ' + _.random(rollArgs[0], rollArgs[1]));
}
}
}
module.exports = MyDiceCommand;