galactic-bot/structure/interfaces/Argument.js

77 lines
2.8 KiB
JavaScript

const Constants = {
Defaults: { //these dont really mean anything, just default values. Most important one is the boolean one.
STRING: 'this shouldnt be here',
INTEGER: 0,
FLOAT: 0,
BOOLEAN: true
},
Types: [
'STRING',
'INTEGER',
'FLOAT',
'BOOLEAN',
'MEMBER',
'CHANNEL',
'TEXTCHANNEL',
'VOICECHANNEL',
'TIME'
],
ArgumentTypes: [
'FLAG',
'VERBAL'
]
};
class Argument {
constructor(client, options = {}) {
this.client = client;
this.name = options.name;
this.aliases = options.aliases || []; //Aliases will work for both verbal and flag types. Careful for multiple aliases starting with different letters: more flags, more confusing.
this.type = options.type && Constants.Types.includes(options.type) ? options.type : 'BOOLEAN'; //What type the argument is ['STRING', 'INTEGER', 'FLOAT', 'BOOLEAN'].
this.types = options.types || ['FLAG', 'VERBAL']; //['FLAG'], ['VERBAL'], or ['FLAG', 'VERBAL']. Declares if argument can be used verbally-only, flag-only, or both.
//NOTE: Instead of telling the person the argument is missing and is required, ask them and continue the command execution afterwards. More work to do.
this.required = options.type === 'BOOLEAN' ? false : Boolean(options.required); //If the argument must be required for the command to work. Booleans
this.ignoreInvalid = Boolean(options.ignoreInvalid);
this.default = options.default === null ? Constants.Defaults[options.type] : options.default;
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.options = options.options || [];
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.parser = options.parser || null; //Option to pass a function to verify values.
this.value = this.infinite ? [] : null; //The value provided to the flag; assigned in the command handler.
this._flag = false; //used internally
}
setDefault(guild) {
let def = this.default;
if(typeof this.default === 'function') {
def = this.default(guild);
}
return this.infinite //eslint-disable-line no-return-assign
? this.value = [ def ]
: this.value = def;
}
get empty() {
return this.infinite
? this.value.length === 0
: this.value === null || this.value === undefined;
}
}
module.exports = Argument;