From c33d11c50845e7bed2defba96738a17aceb3f022 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 25 Jul 2022 01:19:16 +0300 Subject: [PATCH] text based commands parsing --- .../en_gb/observers/en_gb_commandHandler.lang | 3 +++ .../components/observers/CommandHandler.js | 27 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/localization/en_gb/observers/en_gb_commandHandler.lang b/src/localization/en_gb/observers/en_gb_commandHandler.lang index 264ece6..bbe55fc 100644 --- a/src/localization/en_gb/observers/en_gb_commandHandler.lang +++ b/src/localization/en_gb/observers/en_gb_commandHandler.lang @@ -80,3 +80,6 @@ Command Error [O_COMMANDHANDLER_COMMAND_NORESPONSE] Command returned no response. This should not happen and is likely a bug. +[O_COMMANDHANDLER_UNRECOGNISED_FLAG] +Unrecognised flag: `{flag}` +See command help for valid flags. \ No newline at end of file diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index 6ea421d..73c6f90 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -92,7 +92,9 @@ class CommandHandler extends Observer { _parseResponse(invoker, response) { const { command } = invoker; - if (response.error) { + if (response.error && response.index) { + return invoker.reply(response); + } else if (response.error) { let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { option: response.option.name, min: response.option.minimum, max: response.option.maximum }); @@ -275,10 +277,29 @@ class CommandHandler extends Observer { const activeCommand = subcommand || command; const flags = activeCommand.options.filter((opt) => opt.flag); - for (const flag of flags) { - + params = Util.parseQuotes(params.join(' ')).map(([x]) => x); + + for (let index = 0; index < params.length;) { + + const match = (/(?:^| )(?(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu).exec(params[index]); + if (!match) { + index++; + continue; + } + + const _flag = match.groups.flag.replace(/--?/u, ''); + const flag = flags.find((f) => f.name === _flag.toLowerCase()); + if (!flag) return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_FLAG', params: { flag: _flag } }; + + params.splice(index, 1); + args[flag.name] = flag.clone(params[index]); + params.splice(index, 1); + } + const options = activeCommand.options.filter((opt) => !opt.flag); + + return { options: { args, parameters: params }, verbose: true }; }