From 8fbe72b3270cdb8dd103d956a07e8e0415c95abc Mon Sep 17 00:00:00 2001 From: noolaan Date: Sat, 11 Apr 2020 07:56:52 -0600 Subject: [PATCH] command handler MEAT --- .../components/commands/utility/Ping.js | 7 + .../components/observers/CommandHandler.js | 175 +++++++++++++----- 2 files changed, 140 insertions(+), 42 deletions(-) diff --git a/structure/client/components/commands/utility/Ping.js b/structure/client/components/commands/utility/Ping.js index d3d3c85..fdf1039 100644 --- a/structure/client/components/commands/utility/Ping.js +++ b/structure/client/components/commands/utility/Ping.js @@ -20,6 +20,13 @@ class PingCommand extends Command { type: 'INTEGER', types: ['FLAG', 'VERBAL'], default: 0 + }), + new Argument(client, { + name: 'carrot', + aliases: ['cars', 'carrots'], + type: 'STRING', + required: true, + types: ['FLAG', 'VERBAL'] }) ] }); diff --git a/structure/client/components/observers/CommandHandler.js b/structure/client/components/observers/CommandHandler.js index 1dcd9d6..0215ff9 100644 --- a/structure/client/components/observers/CommandHandler.js +++ b/structure/client/components/observers/CommandHandler.js @@ -1,5 +1,5 @@ const { stripIndents } = require('common-tags'); -const { escapeRegex } = require('escape-string-regexp'); +const escapeRegex = require('escape-string-regexp'); const { Observer } = require('../../../interfaces/'); @@ -124,70 +124,160 @@ class CommandHandler extends Observer { const parsedFlags = {}; const { shortFlags, longFlags, keys } = await this._createFlags(command.arguments); - - // -prune 50 -c #test - // -prune 50 -b -c #test - + const regex = new RegExp(`([0-9]*)(${Object.keys(longFlags).map(k=>escapeRegex(k)).join('|')})([0-9]*)`, 'i'); + console.log(regex); + let currentArgument = null; - for(const word of args) { + let params = []; + + for(let i=0; i 0 && ['INTEGER', 'FLOAT'].includes(currentArgument.type)) { //15 pts + console.log("asgsaiughasiguassag") + const lastItem = params[params.length-1]; + const beforeError = await this._handleTypeParsing(currentArgument, lastItem); + if(beforeError) { + continue; + } else { + params.pop(); + currentArgument.value = lastItem; + parsedArguments.push(currentArgument); + currentArgument = null; + continue; + } + } + const value = match[1] || match[3]; + const error = await this._handleTypeParsing(currentArgument, value); + if(value) { + if(error) { + if(currentArgument.required) { + console.error(`Argument ${currentArgument.name} is required and failed to meet requirements.`); + return undefined; + } else { + parsedArguments.push(currentArgument); + currentArgument = null; + continue; + } + } else { + currentArgument.value = value; + parsedArguments.push(currentArgument); + currentArgument = null; + continue; + } + } else { + continue; + } + } else { + if(currentArgument) { + const error = await this._handleTypeParsing(currentArgument, word); + if(error) { + if(currentArgument.default) { + params.push(word); + currentArgument.value = currentArgument.default; + parsedArguments.push(currentArgument); + currentArgument = null; + continue; + } + if(currentArgument.required) { + if(currentArgument.infinite) { + if(currentArgument.value.length === 0) { + console.error(`1 Argument ${currentArgument.name} is required and failed to meet requirements.`); + return undefined; + } else { + parsedArguments.push(currentArgument); + currentArgument = null; + params.push(word); + continue; + } + } else { + console.error(`2 Argument ${currentArgument.name} is required and failed to meet requirements.`); + return undefined; + } + } else { + currentArgument = null; + params.push(word); + continue; + } + } else { + if(currentArgument.infinite) continue; + parsedArguments.push(currentArgument); + currentArgument = null; + continue; + } + } else { + params.push(word); + continue; + } } - if(!keys[match[2]]) { - //do sometihng there - } - currentArgument = longFlags[match[2]]; - const value = match[1] || match[3]; - - } } + + message.channel.send(stripIndents`**arguments:** ${parsedArguments.map(a=>`${a.name}: ${a.value}`).join(' | ')} + **words:** ${params.join(', ')}`); + + } async _handleTypeParsing(argument, string) { - const { error, value } = await this.constructor.parseType(argument, string); //Cannot access static functions through "this". - if(error) { - //Failed to parse correctly. prompt.invalid + const parse = async (argument, string) => { + + const { error, value } = await this.constructor.parseType(argument.type, string); //Cannot access static functions through "this". + if(error) return { error: true }; + + if(['INTEGER', 'FLOAT'].includes(argument.type)) { + const { min, max } = argument; + if(value > max && max !== null) { + return { error: true }; + } + if(value < min && min !== null) { + return { error: true }; + } + } + + return { error: false, value }; + } - if(['INTEGER', 'FLOAT'].includes(argument.type)) { - const { min, max } = argument; - if(value > max) { - //Failed to parse correctly. prompt.invalid - } - if(value < min) { - //Failed to parse correctly. prompt.invalid - } + const { error, value } = await parse(argument, string); + + if(!error) { + argument.infinite + ? argument.value.push(value) + : argument.value = value; } + return error; + } async _createFlags(args) { @@ -254,6 +344,7 @@ class CommandHandler extends Observer { } } + return await types[type](str); }