discord bot 自定义命令 JS
discord bot custom command JS
我正在尝试使用 Discord、js-commando 库编写一个简单的 Discord 机器人,到目前为止我已经完成了大部分工作,但我现在遇到了一个问题所以我得到了这个命令做 !roll 它会随机选择一个从 1 到 6 的数字,但我想让它更自定义一点所以这是它的样子
!roll(从 1 掷到 6)
!roll 25(从 1 掷到 25)
!roll 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
let hasNumber = /^[0-9]$/
您的正则表达式仅测试 1 个数字。尝试:
let hasNumber = /^[0-9]+$/
尝试更改
if (!hasNumber.test(roll[0]) || !hasNumber.test(roll[1])) {
到
if (!hasNumber.test(roll[0]) && !hasNumber.test(roll[1])) {
并尝试将 hasNumber
正则表达式更改为 /^[0-9]+$/
,否则它可能会在超过一位数的任何数字上失败。
我正在尝试使用 Discord、js-commando 库编写一个简单的 Discord 机器人,到目前为止我已经完成了大部分工作,但我现在遇到了一个问题所以我得到了这个命令做 !roll 它会随机选择一个从 1 到 6 的数字,但我想让它更自定义一点所以这是它的样子
!roll(从 1 掷到 6)
!roll 25(从 1 掷到 25)
!roll 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
let hasNumber = /^[0-9]$/
您的正则表达式仅测试 1 个数字。尝试:
let hasNumber = /^[0-9]+$/
尝试更改
if (!hasNumber.test(roll[0]) || !hasNumber.test(roll[1])) {
到
if (!hasNumber.test(roll[0]) && !hasNumber.test(roll[1])) {
并尝试将 hasNumber
正则表达式更改为 /^[0-9]+$/
,否则它可能会在超过一位数的任何数字上失败。