2020-04-08 18:08:46 +02:00
|
|
|
const Component = require('./Component.js');
|
2020-08-04 11:36:20 +02:00
|
|
|
const { stripIndents } = require('common-tags');
|
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);
|
2020-07-11 22:32:43 +02:00
|
|
|
this.premium = opts.premium || 0;
|
2020-04-08 18:08:46 +02:00
|
|
|
|
2020-06-02 12:09:28 +02:00
|
|
|
this.description = opts.description || `S_${opts.name.toUpperCase()}_DESCRIPTION`;
|
2020-05-21 12:47:58 +02:00
|
|
|
this.examples = opts.examples || [];
|
2020-07-26 22:10:55 +02:00
|
|
|
this.rawExamples = Boolean(opts.rawExamples);
|
2020-05-21 12:47:58 +02:00
|
|
|
this.usage = opts.usage || '';
|
2020-05-25 13:13:34 +02:00
|
|
|
this.archivable = opts.archivable === undefined ? true : Boolean(opts.archivable);
|
2020-04-08 18:08:46 +02:00
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
this.tags = opts.tags || [];
|
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
this.index = opts.index || opts.name;
|
|
|
|
this.aliases = opts.aliases || [];
|
2020-05-21 12:47:58 +02:00
|
|
|
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;
|
2020-05-06 01:40:46 +02:00
|
|
|
this.arguments = opts.arguments || [];
|
|
|
|
this.custom = Boolean(opts.custom);
|
2020-05-25 13:13:34 +02:00
|
|
|
// this.display = opts.display?.toLowerCase() || opts.name.toLowerCase();
|
2020-04-08 18:08:46 +02:00
|
|
|
|
|
|
|
this.memberPermissions = opts.memberPermissions || [];
|
|
|
|
this.clientPermissions = opts.clientPermissions || [];
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
this._commandHandler = null;
|
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
async handle() {
|
|
|
|
this.client.logger.error(`No handle function found in ${this.moduleResolveable}.`);
|
|
|
|
}
|
|
|
|
|
2020-05-25 13:13:34 +02:00
|
|
|
async _parseArguments(params, guild) {
|
|
|
|
const response = await this.commandHandler._parseArguments(params, this.arguments, guild);
|
2020-05-21 12:47:58 +02:00
|
|
|
if(response.error) return response;
|
2020-06-16 00:15:13 +02:00
|
|
|
return { parsedArguments: response.args, params: response.parameters.map((p) => p[0]), error: false };
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
|
|
|
|
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-07-11 22:32:43 +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 resolvers due to the nature of this method, might break otherwise) NOTE: REMEMBER TO BIND 'this' ARG!
|
|
|
|
* @param {Guild} guild The guild for the resolver to use when resolving
|
|
|
|
* @param {Boolean} caseSensitive Whether or not the arguments are case sensitive
|
2020-05-24 00:12:52 +02:00
|
|
|
* @returns {Object}
|
|
|
|
* @memberof Resolver
|
|
|
|
*/
|
2020-09-30 18:25:04 +02:00
|
|
|
resolveMethod() {
|
|
|
|
// eslint-disable-next-line prefer-rest-params
|
|
|
|
return this.client.resolver.resolveMethod(...arguments);
|
2020-05-24 00:12:52 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
reason(executor) {
|
|
|
|
return `[${this.moduleResolveable}] Executed by ${executor.tag} (${executor.id}).`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handleReset(message) {
|
2020-08-16 09:27:49 +02:00
|
|
|
const payload = { error: false, response: null };
|
|
|
|
try {
|
|
|
|
await message.guild._removeSettings(this.index);
|
|
|
|
payload.response = message.format('GENERAL_SETTINGRESET', { setting: this.name.toLowerCase() });
|
|
|
|
} catch(error) {
|
|
|
|
this.client.logger.error(`Error reseting setting data: ${error}`);
|
|
|
|
payload.error = true;
|
|
|
|
}
|
|
|
|
return payload;
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 14:38:53 +02:00
|
|
|
async _processChannelIgnore(args, setting, guild, langIndex) {
|
|
|
|
|
|
|
|
const { resolver } = this.client;
|
|
|
|
const langParams = {};
|
|
|
|
|
|
|
|
const resolved = await resolver.resolveMethod(args, {
|
|
|
|
existing: setting.ignore,
|
|
|
|
resolver: resolver.resolveChannels.bind(resolver),
|
|
|
|
guild,
|
|
|
|
allowedMethods: ['add', 'remove', 'set', 'reset']
|
|
|
|
});
|
|
|
|
if (!resolved) return {
|
|
|
|
error: true,
|
|
|
|
msg: guild.format('ERR_INVALID_METHOD', { method: args[0] })
|
|
|
|
};
|
|
|
|
else if (args.length < 2 && resolved.method !== 'reset') return {
|
|
|
|
error: true,
|
|
|
|
msg: guild.format('MISSING_ARGS')
|
|
|
|
};
|
|
|
|
|
|
|
|
langParams.index = langIndex + resolved.method.toUpperCase();
|
|
|
|
langParams.changed = resolved.resolved.join(', '); //.filter((ch) => !setting.ignore.includes(ch.id))
|
|
|
|
setting.ignore = resolved.result;
|
|
|
|
return langParams;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async _processRoleBypass(args, setting, guild, langIndex) {
|
|
|
|
|
|
|
|
const { resolver } = this.client;
|
|
|
|
const langParams = {};
|
|
|
|
|
|
|
|
const resolved = await resolver.resolveMethod(args, {
|
|
|
|
existing: setting.bypass,
|
|
|
|
resolver: resolver.resolveRoles.bind(resolver),
|
|
|
|
guild,
|
|
|
|
allowedMethods: ['add', 'remove', 'set', 'reset']
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!resolved) return {
|
|
|
|
error: true,
|
|
|
|
msg: guild.format('ERR_INVALID_METHOD', { method: args[0] })
|
|
|
|
};
|
|
|
|
else if (args.length < 2 && resolved.method !== 'reset') return {
|
|
|
|
error: true,
|
|
|
|
msg: guild.format('MISSING_ARGS')
|
|
|
|
};
|
|
|
|
|
|
|
|
langParams.index = langIndex + resolved.method.toUpperCase();
|
|
|
|
langParams.changed = resolved.resolved.map((role) => role.name).join(', '); // .filter((ch) => !setting.bypass.includes(ch.id))
|
|
|
|
setting.bypass = resolved.result;
|
|
|
|
return langParams;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
get commandHandler() {
|
|
|
|
if(this._commandHandler) return this._commandHandler;
|
2020-06-02 12:09:28 +02:00
|
|
|
this._commandHandler = this.client.registry.components.get('observer:commandHandler');
|
2020-05-21 12:47:58 +02:00
|
|
|
return this._commandHandler;
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get moduleResolveable() {
|
|
|
|
return `${this.module.id}:${this.id}`;
|
|
|
|
}
|
2020-05-25 13:13:34 +02:00
|
|
|
|
|
|
|
get display() {
|
|
|
|
return this.name.toLowerCase();
|
|
|
|
}
|
2020-07-26 22:10:55 +02:00
|
|
|
|
|
|
|
usageEmbed(message, verbose = false) {
|
|
|
|
|
|
|
|
const { guild } = message;
|
|
|
|
const prefix = guild?.prefix || this.client.prefix;
|
|
|
|
const fields = [];
|
|
|
|
|
|
|
|
if (this.examples.length) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${message.format('GENERAL_EXAMPLES')}`,
|
|
|
|
value: this.examples.map((e) => this.rawExamples ? `\`${prefix}${e}\`` : `\`${prefix}setting ${e}\``).join('\n')
|
2020-07-26 22:10:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.aliases.length && verbose) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${message.format('GENERAL_ALIASES')}`,
|
2020-07-26 22:10:55 +02:00
|
|
|
value: this.aliases.join(', ')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.arguments.length && verbose) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${message.format('GENERAL_ARGUMENTS')}`,
|
2020-07-26 22:10:55 +02:00
|
|
|
value: this.arguments.map((a) => `\`${a.types.length === 1 && a.types.includes('FLAG') ? '--' : ''}${a.name}${a.usage ? ` ${a.usage}` : ''}\`: ${message.format(`A_${a.name.toUpperCase()}_${this.name.toUpperCase()}_DESCRIPTION`)}`)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
author: {
|
2020-09-27 20:16:09 +02:00
|
|
|
name: `${this.display}${this.module ? ` (${this.module.resolveable})` : ''}`
|
2020-07-26 22:10:55 +02:00
|
|
|
},
|
2020-09-27 20:16:09 +02:00
|
|
|
description: stripIndents`\`${prefix}setting ${this.display}${this.usage ? ` ${this.usage}` : ''}\`${this.guildOnly ? ' *(guild-only)*' : ''}
|
2020-07-26 22:10:55 +02:00
|
|
|
${message.format(this.description)}`,
|
|
|
|
fields
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
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'
|
|
|
|
]
|
|
|
|
};
|