Compare commits

...

3 Commits

Author SHA1 Message Date
3cba522baa v1.6.5 2024-03-24 21:39:15 +02:00
171c0e9bcb fix minimum and maximum limits 2024-03-24 21:38:48 +02:00
69c52d580a v1.6.4 2023-12-22 17:18:43 +02:00
5 changed files with 55 additions and 29 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/commandparser", "name": "@navy.gif/commandparser",
"version": "1.6.3", "version": "1.6.5",
"description": "Parser meant to parse commands and their options for discord bots", "description": "Parser meant to parse commands and their options for discord bots",
"author": "Navy.gif", "author": "Navy.gif",
"license": "MIT", "license": "MIT",

View File

@ -100,7 +100,10 @@ class Parser extends EventEmitter
const command = this.matchCommand(commandName); const command = this.matchCommand(commandName);
if (!command) if (!command)
{
this.debug(`No command found for ${commandName}`);
return null; return null;
}
this.debug(`Matched command ${command.name}`); this.debug(`Matched command ${command.name}`);
if (typeof commandFilter === 'function') if (typeof commandFilter === 'function')
@ -280,10 +283,15 @@ class Parser extends EventEmitter
continue; continue;
} }
cloned.rawValue = [ params[index] ]; 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 // eslint-disable-next-line max-depth
if (!error) if (!error)
break; 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++; index++;
} }
} }

View File

@ -86,6 +86,9 @@ class CommandOption implements ICommandOption
this.dependsOn = def.dependsOn || []; this.dependsOn = def.dependsOn || [];
this.dependsOnMode = def.dependsOnMode || 'AND'; this.dependsOnMode = def.dependsOnMode || 'AND';
this.minimum = def.minimum;
this.maximum = def.maximum;
this.required = def.required || false; this.required = def.required || false;
} }
@ -157,9 +160,9 @@ class CommandOption implements ICommandOption
if (isNaN(integer)) if (isNaN(integer))
return { error: true }; return { error: true };
if (this.minimum !== undefined && integer < this.minimum) if (this.minimum !== undefined && integer < this.minimum)
return { error: true }; return { error: true, valid: true };
if (this.maximum !== undefined && integer > this.maximum) if (this.maximum !== undefined && integer > this.maximum)
return { error: true }; return { error: true, valid: true };
return { value: integer, removed: [ this.rawValue[0] ] }; return { value: integer, removed: [ this.rawValue[0] ] };
} }
@ -171,9 +174,9 @@ class CommandOption implements ICommandOption
if (isNaN(float)) if (isNaN(float))
return { error: true }; return { error: true };
if (this.minimum !== undefined && float < this.minimum) if (this.minimum !== undefined && float < this.minimum)
return { error: true }; return { error: true, valid: true };
if (this.maximum !== undefined && float > this.maximum) if (this.maximum !== undefined && float > this.maximum)
return { error: true }; return { error: true, valid: true };
return { value: float, removed: [ this.rawValue[0] ] }; return { value: float, removed: [ this.rawValue[0] ] };
} }

View File

@ -65,6 +65,8 @@ type DependsOnMode = 'AND' | 'OR';
type ParseResult = { type ParseResult = {
error: boolean, error: boolean,
// Valid is set if the value is out of bounds
valid: boolean,
removed: string[], removed: string[],
value: never value: never
} }

View File

@ -1,34 +1,47 @@
import { Parser, Command, OptionType } from '../build/esm/index.js'; import { Parser, Command, OptionType } from '../build/esm/index.js';
// const command = new Command({ const createCommand = new Command({
// name: 'create', name: 'create',
// options: [{ options: [{
// name: 'registration-code', name: 'registration-code',
// aliases: ['code'], aliases: ['code'],
// type: OptionType.SUB_COMMAND, type: OptionType.SUB_COMMAND,
// options: [{ options: [{
// name: 'amount', name: 'amount',
// flag: true, flag: true,
// type: OptionType.INTEGER, type: OptionType.INTEGER,
// defaultValue: 1, defaultValue: 1,
// valueOptional: true, valueOptional: true,
// required: true required: true
// }] }]
// }] }]
// }); });
const command = new Command({ const botbanCommand = new Command({
name: 'botban', name: 'botban',
options: [ options: [
{ name: 'users', type: OptionType.STRING, required: true }, { name: 'users', type: OptionType.STRING, required: true },
{ name: 'service', choices: ['support', 'reports'], valueAsAlias: true, 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) parser.on('debug', console.log)
// console.log(await parser.parseMessage('create code -a 1')); console.log((await parser.parseMessage('create code -a 1')).args);
// console.log(await parser.parseMessage('create code --help')); console.log((await parser.parseMessage('create code --help')).args);
// // console.log(await parser.parseMessage('create --help')); // 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);