forked from Galactic/galactic-bot
text based commands parsing
This commit is contained in:
parent
10aa85f149
commit
c33d11c508
@ -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.
|
@ -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 = (/(?:^| )(?<flag>(?:--[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 };
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user