galactic-bot/structure/interfaces/Setting.js

100 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
const Component = require('./Component.js');
2020-04-08 16:27:34 +02:00
2020-04-08 18:08:46 +02:00
class Setting extends Component {
2020-04-08 16:27:34 +02:00
2020-04-08 18:08:46 +02:00
constructor(client, opts = {}) {
if(!opts) return null;
super(client, {
id: opts.name,
type: 'setting',
guarded: opts.guarded,
disabled: opts.disabled
});
this.name = opts.name;
this.module = opts.module;
this.restricted = Boolean(opts.restricted);
this.description = opts.description || `S_${opts.name.toUpperCase()}_DESCRIPTION`;
this.examples = opts.examples || [];
this.usage = opts.usage || '';
this.archivable = opts.archivable === undefined ? true : Boolean(opts.archivable);
2020-04-08 18:08:46 +02:00
this.index = opts.index || opts.name;
this.aliases = opts.aliases || [];
this.resolve = opts.resolve && Constants.Resolves.includes(opts.resolve) ? opts.resolve : 'GUILD'; //eslint-disable-line no-use-before-define
2020-04-08 18:08:46 +02:00
this.default = opts.default;
this.arguments = opts.arguments || [];
this.custom = Boolean(opts.custom);
// this.display = opts.display?.toLowerCase() || opts.name.toLowerCase();
2020-04-08 18:08:46 +02:00
this.memberPermissions = opts.memberPermissions || [];
this.clientPermissions = opts.clientPermissions || [];
this._commandHandler = null;
2020-04-08 18:08:46 +02:00
}
async handle() {
this.client.logger.error(`No handle function found in ${this.moduleResolveable}.`);
}
async _parseArguments(params, guild) {
const response = await this.commandHandler._parseArguments(params, this.arguments, guild);
if(response.error) return response;
return { parsedArguments: response.args, params: response.parameters, error: false };
}
2020-05-24 00:12:52 +02:00
/**
* Resolves methods used primarily for settings, also deals with appending the arguments into existing lists
*
* @param {Array<String>} args The incoming arguments with the first element being the method ex. ['add','ban','kick']
* @param {Array<String>} valid An array of items to compare to, if an argument doesn't exist in this array it'll be skipped over
* @param {Array<String>} [existing=[]] Existing values in the array, valid elements will be appended to this
2020-05-24 23:45:12 +02:00
* @param {Function} resolver One of the resolver functions used to resolve the passed values into objects (should always be one of the mass resolver due to the nature of this method, might break otherwise). NOTE: REMEMBER TO BIND 'this' ARG!
2020-05-24 00:12:52 +02:00
* @returns {Object}
* @memberof Resolver
*/
2020-05-24 23:45:12 +02:00
resolveMethod(args, valid, existing, resolver) {
return this.client.resolver.resolveMethod(args, valid, existing, resolver);
2020-05-24 00:12:52 +02:00
}
reason(executor) {
return `[${this.moduleResolveable}] Executed by ${executor.tag} (${executor.id}).`;
}
async _handleReset(message) {
await message.guild._removeSettings(this.index);
2020-05-24 00:12:52 +02:00
const msg = message.format('GENERAL_SETTINGRESET', { setting: this.name.toLowerCase() });
return {
error: false,
msg
};
}
get commandHandler() {
if(this._commandHandler) return this._commandHandler;
this._commandHandler = this.client.registry.components.get('observer:commandHandler');
return this._commandHandler;
}
get moduleResolveable() {
return `${this.module.id}:${this.id}`;
}
get display() {
return this.name.toLowerCase();
}
2020-04-08 18:08:46 +02:00
2020-04-08 16:27:34 +02:00
}
2020-04-08 18:08:46 +02:00
module.exports = Setting;
const Constants = {
Resolves: [
'GUILD',
'USER'
]
};