不和谐bot自定义命令JS
问题描述:
我想编写一个简单的Discord与Discord,js-commando库的机器人我得到它的大部分工作到目前为止,但我困在此刻的问题,所以我得到了这个命令在这里你可以做!滚动,它会随机选择从1数到6,但我想让它多一点的定制所以这里是什么样子不和谐bot自定义命令JS
!卷(卷1至6)
!辊25(辊从1到25)
!辊100 200(罗尔斯从100到200)
问题是当我尝试做!roll 25我的验证一直说这不是一个有效的数字,但所有其他命令工作得很好它只发生在我做!roll,然后一些数字,我不明白为什么它不工作很可能是一个简单的办法提前感谢
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
正则表达式来/^[0-9]+$/
因为否则它可能会在任意数量不能超过一个数字。
答
let hasNumber = /^[0-9]$/
您正则表达式只是测试了1位。尝试:
let hasNumber = /^[0-9]+$/
谢谢你这么做;) – John
这仍然是错误的,因为[“b”,“3”]将通过数字验证 – xDreamCoding
你是什么意思由[“b”,“3”]?据我所知,现在所有的作品 – John