forked from Galactic/galactic-bot
more command handler changers
This commit is contained in:
parent
065ec7747b
commit
852d155717
@ -125,34 +125,71 @@ class CommandHandler extends Observer {
|
|||||||
|
|
||||||
const { shortFlags, longFlags, keys } = await this._createFlags(command.arguments);
|
const { shortFlags, longFlags, keys } = await this._createFlags(command.arguments);
|
||||||
|
|
||||||
|
// -prune 50 -c #test
|
||||||
|
// -prune 50 -b -c #test
|
||||||
|
|
||||||
let currentArgument = null;
|
let currentArgument = null;
|
||||||
for(const word of args) {
|
for(const word of args) {
|
||||||
if(currentArgument) {
|
|
||||||
//what
|
|
||||||
}
|
|
||||||
const [one,two,...chars] = word.split('');
|
const [one,two,...chars] = word.split('');
|
||||||
if(one === '-' && two !== '-') { //short flag maybe?
|
if(one === '-' && two !== '-') { //short flag maybe?
|
||||||
const name = [ two, ...chars ].join('');
|
const name = [ two, ...chars ].join('');
|
||||||
currentArgument = shortFlags[name];
|
if(currentArgument) {
|
||||||
if(!currentArgument) continue;
|
if(currentArgument.required) {
|
||||||
|
//Cannot use another flag after a required flag.
|
||||||
|
//Handle requirement error for currentArgument.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!keys.includes(name)) continue;
|
||||||
|
currentArgument = shortFlags[name];
|
||||||
|
|
||||||
} else if(one === '-' && two === '-') { //long flag maybe?
|
} else if(one === '-' && two === '-') { //long flag maybe?
|
||||||
|
if(currentArgument.required) {
|
||||||
|
//Cannot use another flag after a required flag.
|
||||||
|
//Handle requirement error for currentArgument.
|
||||||
|
}
|
||||||
const name = chars.join('');
|
const name = chars.join('');
|
||||||
|
if(!keys.includes(name)) continue;
|
||||||
currentArgument = longFlags[name];
|
currentArgument = longFlags[name];
|
||||||
if(!currentArgument) continue;
|
|
||||||
} else { //verbal argument
|
} else {
|
||||||
const regex = new RegExp('([0-9]*)([a-zA-Z]+)([0-9]*)', 'g');
|
const regex = new RegExp('([0-9]*)([a-zA-Z]+)([0-9]*)', 'g');
|
||||||
let match = regex.exec(word);
|
let match = regex.exec(word);
|
||||||
if(!match) continue;
|
if(currentArgument && !match) {
|
||||||
|
await this._handleTypeParsing(currentArgument, word);
|
||||||
|
if(!currentArgument.infinite) currentArgument = null;
|
||||||
|
}
|
||||||
|
if(!keys[match[2]]) {
|
||||||
|
//do sometihng there
|
||||||
|
}
|
||||||
currentArgument = longFlags[match[2]];
|
currentArgument = longFlags[match[2]];
|
||||||
if(!currentArgument) continue;
|
|
||||||
const value = match[1] || match[3];
|
const value = match[1] || match[3];
|
||||||
console.log(currentArgument);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async _createFlags(args) {
|
async _createFlags(args) {
|
||||||
|
|
||||||
let shortFlags = {};
|
let shortFlags = {};
|
||||||
@ -187,6 +224,40 @@ class CommandHandler extends Observer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async parseType(type, str) { //this is in the class for a reason, will soon reference to a user resolver etc.
|
||||||
|
|
||||||
|
//INTEGER AND FLOAT ARE SAME FUNCTION
|
||||||
|
|
||||||
|
const types = {
|
||||||
|
STRING: (str) => {
|
||||||
|
return { error: false, value: `${str}` };
|
||||||
|
},
|
||||||
|
INTEGER: (str) => {
|
||||||
|
const int = parseInt(str);
|
||||||
|
if(Number.isNaN(int)) return { error: true }
|
||||||
|
return { error: false, value: int };
|
||||||
|
},
|
||||||
|
FLOAT: (str) => {
|
||||||
|
const float = parseInt(str);
|
||||||
|
if(Number.isNaN(float)) return { error: true }
|
||||||
|
return { error: false, value: float };
|
||||||
|
},
|
||||||
|
BOOLEAN: (str) => {
|
||||||
|
const truthy = ['yes', 'y', 'true', 't', 'on', 'enable'];
|
||||||
|
const falsey = ['no', 'n', 'false', 'f', 'off', 'disable'];
|
||||||
|
if(typeof str === 'boolean') return { error: false, value: str };
|
||||||
|
|
||||||
|
if(typeof str === 'string') str = str.toLowerCase();
|
||||||
|
if(truthy.includes(str)) return { error: false, value: true };
|
||||||
|
if(falsey.includes(str)) return { error: false, value: false };
|
||||||
|
return { error: true };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await types[type](str);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,13 +23,14 @@ class Argument {
|
|||||||
this.required = options.type === 'BOOLEAN' ? false : Boolean(options.required); //If the argument must be required for the command to work. Booleans
|
this.required = options.type === 'BOOLEAN' ? false : Boolean(options.required); //If the argument must be required for the command to work. Booleans
|
||||||
this.default = options.default || Constants.Defaults[options.type];
|
this.default = options.default || Constants.Defaults[options.type];
|
||||||
this.infinite = Boolean(options.infinite); //Accepts infinite amount of arguments e.g. -u @nolan @navy. If false, will only detect one user.
|
this.infinite = Boolean(options.infinite); //Accepts infinite amount of arguments e.g. -u @nolan @navy. If false, will only detect one user.
|
||||||
|
// Will turn value into an array instead of a string!!!
|
||||||
|
|
||||||
this.min = typeof options.min === 'number' ? options.min : null; //Min/max will only be used for INTEGER/FLOAT types.
|
this.min = typeof options.min === 'number' ? options.min : null; //Min/max will only be used for INTEGER/FLOAT types.
|
||||||
this.max = typeof options.max === 'number' ? options.max : null;
|
this.max = typeof options.max === 'number' ? options.max : null;
|
||||||
|
|
||||||
this.parser = options.parser || null; //Option to pass a function to verify values.
|
this.parser = options.parser || null; //Option to pass a function to verify values.
|
||||||
|
|
||||||
this.value = null; //The value provided to the flag; assigned in the command handler.
|
this.value = this.infinite ? [] : null; //The value provided to the flag; assigned in the command handler.
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -50,7 +51,9 @@ const Constants = {
|
|||||||
'STRING',
|
'STRING',
|
||||||
'INTEGER',
|
'INTEGER',
|
||||||
'FLOAT',
|
'FLOAT',
|
||||||
'BOOLEAN'
|
'BOOLEAN',
|
||||||
|
'MEMBER',
|
||||||
|
'CHANNEL'
|
||||||
],
|
],
|
||||||
ArgumentTypes: [
|
ArgumentTypes: [
|
||||||
'FLAG',
|
'FLAG',
|
||||||
|
Loading…
Reference in New Issue
Block a user