diff --git a/.eslintrc.json b/.eslintrc.json index d4677c5..c32c043 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,7 +11,7 @@ "parser": "@typescript-eslint/parser", "plugins": [ "@typescript-eslint" ], "parserOptions": { - "ecmaVersion": 2022, + "ecmaVersion": "latest", "sourceType": "module" }, "rules": { @@ -87,9 +87,13 @@ "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", @@ -133,7 +137,7 @@ ], "max-lines-per-function": [ "warn", - 100 + 140 ], "max-depth": [ "warn", @@ -191,7 +195,10 @@ "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", @@ -202,6 +209,7 @@ "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", @@ -238,7 +246,6 @@ "warn", "before" ], - "padding-line-between-statements": "warn", "padded-blocks": [ "warn", { diff --git a/package.json b/package.json index 1035a64..b73e8bf 100644 --- a/package.json +++ b/package.json @@ -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" + "lint": "eslint --fix src/" } } diff --git a/src/Parser.ts b/src/Parser.ts index 5854a23..43175f9 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -30,8 +30,9 @@ type ParserOptions = { type ParseOptions = { prefix?: string, - commandFilter?: (cmd: ICommand) => boolean, guild?: unknown + commandFilter?: (cmd: ICommand) => boolean | Promise, + commandFound?: (cmd: Cmd) => boolean | Promise } const flagReg = /(?:^| )(?(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu; @@ -69,22 +70,21 @@ class Parser extends EventEmitter } /** - * - * - * @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} {(Promise)} + * @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 * @memberof Parser */ - // eslint-disable-next-line @typescript-eslint/ban-types async parseMessage ( message: string, { prefix = this.prefix, guild, - commandFilter + commandFilter, + commandFound, }: ParseOptions = {} ): Promise { @@ -100,7 +100,7 @@ class Parser extends EventEmitter return null; this.debug(`Matched command ${command.name}`); - if (commandFilter && typeof commandFilter === 'function') + if (typeof commandFilter === 'function') { const result = await commandFilter(command); if (!result) @@ -110,6 +110,9 @@ 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 }; diff --git a/src/util/Errors/ParserError.ts b/src/util/Errors/ParserError.ts index 4ccaf15..771ccbd 100644 --- a/src/util/Errors/ParserError.ts +++ b/src/util/Errors/ParserError.ts @@ -1,5 +1,7 @@ -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'; } diff --git a/tests/playground.js b/tests/playground.js index 30f6b02..0982bce 100644 --- a/tests/playground.js +++ b/tests/playground.js @@ -1,22 +1,34 @@ 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: 'create', - options: [{ - name: 'registration-code', - aliases: ['code'], - type: OptionType.SUB_COMMAND, - options: [{ - name: 'amount', - flag: true, - type: OptionType.INTEGER, - defaultValue: 1, - valueOptional: true - }] - }] + 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 }); 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')); \ No newline at end of file +// 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')); \ No newline at end of file