better func definition

This commit is contained in:
Erik 2022-07-29 21:15:42 +03:00
parent 7d00001504
commit 6686aa1fc1
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 5 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "commandparser", "name": "commandparser",
"version": "1.0.18", "version": "1.0.19",
"description": "Parser meant to parse commands and their options for discord bots", "description": "Parser meant to parse commands and their options for discord bots",
"main": "build/index.js", "main": "build/index.js",
"author": "Navy.gif", "author": "Navy.gif",

View File

@ -67,12 +67,13 @@ class Parser extends EventEmitter {
* @param {string} message The text to parse command from * @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 {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 {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. * @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>)} * @return {Promise<ParseResult | null>} {(Promise<ParseResult | null>)}
* @memberof Parser * @memberof Parser
*/ */
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
async parseMessage(message: string, prefix = this.prefix, guild?: unknown, commandFilter?: Function): Promise<ParseResult | null> { async parseMessage(message: string, prefix = this.prefix,
guild?: unknown, commandFilter?: (command: ICommand) => boolean): Promise<ParseResult | null> {
if (!message.startsWith(prefix) || !message.length) return null; if (!message.startsWith(prefix) || !message.length) return null;
let params: string[] = message.replace(prefix, '').split(' ').filter((str) => str.length); let params: string[] = message.replace(prefix, '').split(' ').filter((str) => str.length);
@ -81,7 +82,7 @@ class Parser extends EventEmitter {
const command = this.matchCommand(commandName); const command = this.matchCommand(commandName);
if (!command) return null; if (!command) return null;
if (commandFilter && typeof commandFilter === 'function' && commandFilter(command)) return null; if (commandFilter && typeof commandFilter === 'function' && await commandFilter(command)) return null;
this.debug(`Matched command ${command.name}`); this.debug(`Matched command ${command.name}`);
const { subcommands, subcommandGroups } = command; const { subcommands, subcommandGroups } = command;