galactic-bot/structure/interfaces/Argument.js

77 lines
2.8 KiB
JavaScript
Raw Normal View History

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',
2020-06-04 19:59:09 +02:00
'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.
2020-05-23 09:11:05 +02:00
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);
2020-05-23 09:11:05 +02:00
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.
2020-04-11 12:00:53 +02:00
// 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.
2020-04-11 12:00:53 +02:00
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;
}
2020-04-11 10:10:52 +02:00
get empty() {
return this.infinite
? this.value.length === 0
: this.value === null;
}
}
module.exports = Argument;