Compare commits

..

No commits in common. "4c7af5c5dd2b1ca07ec04abcd62403f6fed69f77" and "d73e1e5731068ea7158f0bb73b0e9f73e8c688ce" have entirely different histories.

5 changed files with 38 additions and 62 deletions

View File

@ -11,7 +11,7 @@
"parser": "@typescript-eslint/parser",
"plugins": [ "@typescript-eslint" ],
"parserOptions": {
"ecmaVersion": "latest",
"ecmaVersion": 2022,
"sourceType": "module"
},
"rules": {
@ -87,13 +87,9 @@
"id-blacklist": "warn",
"id-match": "warn",
"implicit-arrow-linebreak": "warn",
"indent": [
"warn",
4,
{
"SwitchCase": 1
}
],
"indent": ["warn", 4, {
"SwitchCase": 1
}],
"init-declarations": "warn",
"quotes": [
"error",
@ -137,7 +133,7 @@
],
"max-lines-per-function": [
"warn",
140
100
],
"max-depth": [
"warn",
@ -195,10 +191,7 @@
"no-restricted-modules": "warn",
"no-restricted-properties": "warn",
"no-restricted-syntax": "warn",
"no-return-assign": [
"warn",
"except-parens"
],
"no-return-assign": ["warn", "except-parens"],
"no-return-await": "warn",
"no-script-url": "warn",
"no-self-compare": "warn",
@ -209,7 +202,6 @@
"no-tabs": "warn",
"no-template-curly-in-string": "error",
"no-throw-literal": "warn",
"no-trailing-spaces": "warn",
"no-undef-init": "error",
"no-undefined": "error",
"no-unmodified-loop-condition": "warn",
@ -246,6 +238,7 @@
"warn",
"before"
],
"padding-line-between-statements": "warn",
"padded-blocks": [
"warn",
{

View File

@ -1,6 +1,6 @@
{
"name": "@navy.gif/commandparser",
"version": "1.6.3",
"version": "1.6.2",
"description": "Parser meant to parse commands and their options for discord bots",
"author": "Navy.gif",
"license": "MIT",
@ -36,6 +36,6 @@
"build": "tsc && tsc -p tsconfig.cjs.json && node ./scripts/declareTypes.js",
"test": "yarn build && jest",
"release": "yarn build && yarn publish",
"lint": "eslint --fix src/"
"lint": "eslint --fix"
}
}

View File

@ -30,9 +30,8 @@ type ParserOptions = {
type ParseOptions = {
prefix?: string,
commandFilter?: (cmd: ICommand) => boolean,
guild?: unknown
commandFilter?: (cmd: ICommand) => boolean | Promise<boolean>,
commandFound?: <Cmd extends ICommand>(cmd: Cmd) => boolean | Promise<boolean>
}
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
@ -70,21 +69,22 @@ class Parser extends EventEmitter
}
/**
* @param message The text to parse command from
* @param [prefix=this.prefix] Optional prefix to look for in front of the message
* @param [guild] The guild to pass to the parser if the command options require values that expect guild based structures, e.g. a guild member.
* @param [commandFilter] Function for filtering out commands, useful if you want to stop the parser on a command level before argument parsing, probably saves some processing time. If the function evaluates to true the parsing stops
* @param [commandFound] Callback that is called when a command is matched, can be used to halt further parsing by e.g. checking for necessary permissions, returning a truthy value will halt parsing
* @return
*
*
* @param {string} message The text to parse command from
* @param {string} [prefix=this.prefix] Optional prefix to look for in front of the message
* @param {unknown} [guild] The guild to pass to the parser if the command options require values that expect guild based structures, e.g. a guild member.
* @param {Function} [commandFilter] Function for filtering out commands, useful if you want to stop the parser on a command level before argument parsin, probably saves some processing time. If the function evaluates to true the parsing stops
* @return {Promise<ParseResult | null>} {(Promise<ParseResult | null>)}
* @memberof Parser
*/
// eslint-disable-next-line @typescript-eslint/ban-types
async parseMessage (
message: string,
{
prefix = this.prefix,
guild,
commandFilter,
commandFound,
commandFilter
}: ParseOptions = {}
): Promise<ParseResult | null>
{
@ -100,7 +100,7 @@ class Parser extends EventEmitter
return null;
this.debug(`Matched command ${command.name}`);
if (typeof commandFilter === 'function')
if (commandFilter && typeof commandFilter === 'function')
{
const result = await commandFilter(command);
if (!result)
@ -110,9 +110,6 @@ class Parser extends EventEmitter
}
}
if (typeof commandFound === 'function' && await commandFound(command))
return null;
const { subcommands, subcommandGroups } = command;
const args: ArgsResult = {};
const parseResult: ParseResult = { args, command, subcommand: null, subcommandGroup: null };

View File

@ -1,7 +1,5 @@
export class ParserError extends Error
{
constructor (message: string, options?: ErrorOptions)
{
export class ParserError extends Error {
constructor (message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'ParserError';
}

View File

@ -1,34 +1,22 @@
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({
name: 'botban',
options: [
{ name: 'users', type: OptionType.STRING, required: true },
{ name: 'service', choices: ['support', 'reports'], valueAsAlias: true, required: true }
]
name: 'create',
options: [{
name: 'registration-code',
aliases: ['code'],
type: OptionType.SUB_COMMAND,
options: [{
name: 'amount',
flag: true,
type: OptionType.INTEGER,
defaultValue: 1,
valueOptional: true
}]
}]
});
const parser = new Parser({ commands: [command], 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'));
console.log(await parser.parseMessage('botban support dingus'));
console.log(await parser.parseMessage('create code -a 1'));
console.log(await parser.parseMessage('create code --help'));
console.log(await parser.parseMessage('create --help'));