fix minimum and maximum limits
This commit is contained in:
parent
69c52d580a
commit
171c0e9bcb
@ -100,7 +100,10 @@ class Parser extends EventEmitter
|
||||
|
||||
const command = this.matchCommand(commandName);
|
||||
if (!command)
|
||||
{
|
||||
this.debug(`No command found for ${commandName}`);
|
||||
return null;
|
||||
}
|
||||
this.debug(`Matched command ${command.name}`);
|
||||
|
||||
if (typeof commandFilter === 'function')
|
||||
@ -280,10 +283,15 @@ class Parser extends EventEmitter
|
||||
continue;
|
||||
}
|
||||
cloned.rawValue = [ params[index] ];
|
||||
({ removed, error } = await cloned.parse(guild));
|
||||
// eslint-disable-next-line init-declarations
|
||||
let valid;
|
||||
({ removed, error, valid } = await cloned.parse(guild));
|
||||
// eslint-disable-next-line max-depth
|
||||
if (!error)
|
||||
break;
|
||||
// eslint-disable-next-line max-depth
|
||||
if (typeof valid !== 'undefined' && valid)
|
||||
throw new ParserError(`Value ${params[index]} is out of bounds, must be in range [${cloned.minimum}, ${cloned.maximum}]`);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +86,9 @@ class CommandOption implements ICommandOption
|
||||
this.dependsOn = def.dependsOn || [];
|
||||
this.dependsOnMode = def.dependsOnMode || 'AND';
|
||||
|
||||
this.minimum = def.minimum;
|
||||
this.maximum = def.maximum;
|
||||
|
||||
this.required = def.required || false;
|
||||
}
|
||||
|
||||
@ -157,9 +160,9 @@ class CommandOption implements ICommandOption
|
||||
if (isNaN(integer))
|
||||
return { error: true };
|
||||
if (this.minimum !== undefined && integer < this.minimum)
|
||||
return { error: true };
|
||||
return { error: true, valid: true };
|
||||
if (this.maximum !== undefined && integer > this.maximum)
|
||||
return { error: true };
|
||||
return { error: true, valid: true };
|
||||
return { value: integer, removed: [ this.rawValue[0] ] };
|
||||
}
|
||||
|
||||
@ -171,9 +174,9 @@ class CommandOption implements ICommandOption
|
||||
if (isNaN(float))
|
||||
return { error: true };
|
||||
if (this.minimum !== undefined && float < this.minimum)
|
||||
return { error: true };
|
||||
return { error: true, valid: true };
|
||||
if (this.maximum !== undefined && float > this.maximum)
|
||||
return { error: true };
|
||||
return { error: true, valid: true };
|
||||
return { value: float, removed: [ this.rawValue[0] ] };
|
||||
}
|
||||
|
||||
|
@ -65,6 +65,8 @@ type DependsOnMode = 'AND' | 'OR';
|
||||
|
||||
type ParseResult = {
|
||||
error: boolean,
|
||||
// Valid is set if the value is out of bounds
|
||||
valid: boolean,
|
||||
removed: string[],
|
||||
value: never
|
||||
}
|
||||
|
@ -1,34 +1,47 @@
|
||||
import { Parser, Command, OptionType } from '../build/esm/index.js';
|
||||
|
||||
// const command = new Command({
|
||||
// name: 'create',
|
||||
// options: [{
|
||||
// name: 'registration-code',
|
||||
// aliases: ['code'],
|
||||
// type: OptionType.SUB_COMMAND,
|
||||
// options: [{
|
||||
// name: 'amount',
|
||||
// flag: true,
|
||||
// type: OptionType.INTEGER,
|
||||
// defaultValue: 1,
|
||||
// valueOptional: true,
|
||||
// required: true
|
||||
// }]
|
||||
// }]
|
||||
// });
|
||||
const command = new Command({
|
||||
const createCommand = new Command({
|
||||
name: 'create',
|
||||
options: [{
|
||||
name: 'registration-code',
|
||||
aliases: ['code'],
|
||||
type: OptionType.SUB_COMMAND,
|
||||
options: [{
|
||||
name: 'amount',
|
||||
flag: true,
|
||||
type: OptionType.INTEGER,
|
||||
defaultValue: 1,
|
||||
valueOptional: true,
|
||||
required: true
|
||||
}]
|
||||
}]
|
||||
});
|
||||
const botbanCommand = new Command({
|
||||
name: 'botban',
|
||||
options: [
|
||||
{ name: 'users', type: OptionType.STRING, required: true },
|
||||
{ name: 'service', choices: ['support', 'reports'], valueAsAlias: true, required: true }
|
||||
]
|
||||
});
|
||||
const parser = new Parser({ commands: [command], prefix: '', debug: true });
|
||||
|
||||
const volumeCommand = new Command({
|
||||
name: 'volume',
|
||||
options: [
|
||||
{
|
||||
name: 'volume',
|
||||
type: OptionType.INTEGER,
|
||||
maximum: 100,
|
||||
minimum: 0
|
||||
},
|
||||
]
|
||||
});
|
||||
const parser = new Parser({ commands: [createCommand, botbanCommand, volumeCommand], prefix: '', debug: true });
|
||||
parser.on('debug', console.log)
|
||||
// console.log(await parser.parseMessage('create code -a 1'));
|
||||
// console.log(await parser.parseMessage('create code --help'));
|
||||
// // console.log(await parser.parseMessage('create --help'));
|
||||
console.log((await parser.parseMessage('create code -a 1')).args);
|
||||
console.log((await parser.parseMessage('create code --help')).args);
|
||||
// console.log(await parser.parseMessage('create --help'));
|
||||
|
||||
// console.log(await parser.parseMessage('create code'));
|
||||
console.log((await parser.parseMessage('create code')).args);
|
||||
|
||||
console.log(await parser.parseMessage('botban support dingus'));
|
||||
console.log((await parser.parseMessage('botban support dingus')).args);
|
||||
console.log((await parser.parseMessage('volume 500')).args);
|
Loading…
Reference in New Issue
Block a user