不和谐bot自定义命令JS
我正在尝试编写一个简单的不和谐的机器人,js-commando库的工作到目前为止我工作的最多,但是现在我被卡住了,所以我得到了这个命令,你可以做!会随机select一个从1到6的数字,但我想使它更有一点自定义,所以这就是它的样子
!卷(从1滚到6)
卷25(从1到25卷)
卷100 200(从100卷到200)
问题是当我尝试做!roll 25我的validation不断说这不是一个有效的号码,但其他所有的命令工作得很好,只有当我做!roll,然后一些数字,我不明白为什么它doesn没有工作,这可能是一个简单的解决方法,在此先感谢
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
尝试改变
if (!hasNumber.test(roll[0]) || !hasNumber.test(roll[1])) {
至
if (!hasNumber.test(roll[0]) && !hasNumber.test(roll[1])) {
并尝试改变你的hasNumber
正则expression式为/^[0-9]+$/
因为否则它可能会失败的任何数字与多个数字。
let hasNumber = /^[0-9]$/
你的Regex只testing1位数。 尝试:
let hasNumber = /^[0-9]+$/