Compare commits

..

2 Commits

Author SHA1 Message Date
02b22486c7 v1.6.6 2024-03-27 10:10:42 +02:00
f9ea8b23c4 bugfix 2024-03-27 10:10:17 +02:00
3 changed files with 59 additions and 38 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/commandparser", "name": "@navy.gif/commandparser",
"version": "1.6.5", "version": "1.6.6",
"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

@ -10,7 +10,7 @@ import { ICommand } from '../index.js';
import { ParserError } from './util/Errors/ParserError.js'; import { ParserError } from './util/Errors/ParserError.js';
type ArgsResult = { type ArgsResult = {
[key: string]: CommandOption [key: string]: CommandOption | undefined
} }
type ParseResult = { type ParseResult = {
@ -36,7 +36,7 @@ type ParseOptions = {
commandFound?: <Cmd extends ICommand>(cmd: Cmd) => boolean | Promise<boolean> commandFound?: <Cmd extends ICommand>(cmd: Cmd) => boolean | Promise<boolean>
} }
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu; const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{2,})|(?:-[a-z]{1,2}))(?:$| )/iu;
class Parser extends EventEmitter class Parser extends EventEmitter
{ {
@ -178,6 +178,7 @@ class Parser extends EventEmitter
// Parse flags // Parse flags
for (let index = 0; index < params.length;) for (let index = 0; index < params.length;)
{ {
this.debug('');
const match = flagReg.exec(params[index]); const match = flagReg.exec(params[index]);
if (!match || !match.groups) if (!match || !match.groups)
{ {
@ -234,6 +235,9 @@ class Parser extends EventEmitter
// Clean up params for option parsing // Clean up params for option parsing
for (const flag of Object.values(args)) for (const flag of Object.values(args))
{ {
// To shut up TS
if (!flag)
throw new Error('Undefined flag');
this.debug(`Running parser for ${flag.name}`); this.debug(`Running parser for ${flag.name}`);
const result = await flag.parse(guild); const result = await flag.parse(guild);
if (result.error) if (result.error)

View File

@ -1,41 +1,57 @@
import { Parser, Command, OptionType } from '../build/esm/index.js'; import { Parser, Command, OptionType } from '../build/esm/index.js';
const createCommand = new Command({ const commands = [
name: 'create', new Command({
options: [{ name: 'create',
name: 'registration-code',
aliases: ['code'],
type: OptionType.SUB_COMMAND,
options: [{ options: [{
name: 'amount', name: 'registration-code',
flag: true, aliases: ['code'],
type: OptionType.INTEGER, type: OptionType.SUB_COMMAND,
defaultValue: 1, options: [{
valueOptional: true, name: 'amount',
required: true flag: true,
type: OptionType.INTEGER,
defaultValue: 1,
valueOptional: true,
required: true
}]
}] }]
}] }),
}); 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 } ]
] }),
}); new Command({
name: 'volume',
const volumeCommand = new Command({ options: [
name: 'volume', {
options: [ name: 'volume',
{ type: OptionType.INTEGER,
name: 'volume', maximum: 100,
type: OptionType.INTEGER, minimum: 0
maximum: 100, },
minimum: 0 ]
}, }),
] new Command({
}); name: 'search',
const parser = new Parser({ commands: [createCommand, botbanCommand, volumeCommand], prefix: '', debug: true }); options: [
{
name: 'artist',
flag: true
}, {
name: 'id',
flag: true,
type: OptionType.INTEGER
}, {
name: 'song'
}
]
})
]
const parser = new Parser({ commands, prefix: '', debug: true });
parser.on('debug', console.log) parser.on('debug', console.log)
console.log((await parser.parseMessage('create code -a 1')).args); 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 code --help')).args);
@ -44,4 +60,5 @@ console.log((await parser.parseMessage('create code --help')).args);
console.log((await parser.parseMessage('create code')).args); console.log((await parser.parseMessage('create code')).args);
console.log((await parser.parseMessage('botban support dingus')).args); console.log((await parser.parseMessage('botban support dingus')).args);
console.log((await parser.parseMessage('volume 500')).args); console.log((await parser.parseMessage('volume 100')).args);
console.log((await parser.parseMessage('search --id 5000')).args);