2020-05-24 10:54:33 +02:00
|
|
|
const { MessageAttachment } = require('discord.js');
|
2020-05-22 22:13:47 +02:00
|
|
|
const { Command } = require('../../../../interfaces');
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const { PermissionNames } = require('../../../../../util/Constants.js');
|
2020-05-06 01:40:46 +02:00
|
|
|
const { stripIndents } = require('common-tags');
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-05-17 15:27:35 +02:00
|
|
|
class SettingsCommand extends Command {
|
2020-05-05 01:35:01 +02:00
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
super(client, {
|
2020-05-17 15:27:35 +02:00
|
|
|
name: 'settings',
|
2020-07-28 05:00:24 +02:00
|
|
|
module: 'administration',
|
2020-05-05 01:35:01 +02:00
|
|
|
aliases: [
|
2020-05-17 15:27:35 +02:00
|
|
|
'setting',
|
2020-05-05 01:35:01 +02:00
|
|
|
'set'
|
|
|
|
],
|
2020-08-16 09:27:49 +02:00
|
|
|
usage: "['list'|'reset'|setting] [value..]",
|
2020-05-06 01:40:46 +02:00
|
|
|
arguments: [
|
2020-05-08 08:50:54 +02:00
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: 'export',
|
2020-05-06 01:40:46 +02:00
|
|
|
type: 'BOOLEAN',
|
2020-08-16 09:27:49 +02:00
|
|
|
types: ['FLAG'],
|
2020-05-06 01:40:46 +02:00
|
|
|
default: true
|
2020-05-08 08:50:54 +02:00
|
|
|
},
|
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: 'user',
|
2020-05-06 01:40:46 +02:00
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
2020-05-24 10:54:33 +02:00
|
|
|
},
|
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: 'all',
|
2020-05-24 10:54:33 +02:00
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
2020-08-16 09:27:49 +02:00
|
|
|
default: true,
|
|
|
|
archivable: false
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
2020-05-06 01:40:46 +02:00
|
|
|
],
|
2020-05-22 22:13:47 +02:00
|
|
|
showUsage: true,
|
|
|
|
examples: [
|
|
|
|
'prefix !',
|
|
|
|
'modlogs #channel'
|
2020-05-24 10:54:33 +02:00
|
|
|
],
|
|
|
|
throttling: {
|
2020-08-16 09:27:49 +02:00
|
|
|
usages: 2,
|
|
|
|
duration: 5
|
2020-05-24 10:54:33 +02:00
|
|
|
}
|
2020-05-05 01:35:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
async execute(message, { params, args }) {
|
2020-08-16 09:27:49 +02:00
|
|
|
|
2020-07-24 20:31:52 +02:00
|
|
|
const type = !message.guild || args.user ? 'USER' : 'GUILD';
|
2020-08-16 09:27:49 +02:00
|
|
|
if(type === 'GUILD') {
|
|
|
|
const permissions = this.client.permissions.execute(message, message.command, ['ADMINISTRATOR', 'MANAGE_GUILD']);
|
|
|
|
if(permissions.error) return message.respond(message.format('C_SETTINGS_MISSINGPERMISSIONS'), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
if(args.export) return this._exportSettings(message, type);
|
2020-05-24 10:54:33 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
if(params.length === 0) return message.embed(this.usageEmbed(message));
|
|
|
|
const target = params[0].toLowerCase(); //params[0] should never be null
|
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
if(target === 'list') {
|
2020-08-16 09:27:49 +02:00
|
|
|
return this._listSettings(message, type, Boolean(args.all));
|
2020-05-05 01:35:01 +02:00
|
|
|
} else if(target === 'reset') {
|
2020-08-16 09:27:49 +02:00
|
|
|
const prompt = await message.prompt(message.format('C_SETTINGS_RESETPROMPT', { type: message.format('C_SETTINGS_FORMATTYPE', { type }, true) }), {
|
|
|
|
emoji: 'warning'
|
|
|
|
});
|
|
|
|
if(!prompt) return undefined;
|
|
|
|
const boolean = this.client.resolver.resolveBoolean(prompt.content.toLowerCase());
|
|
|
|
if(boolean === null) {
|
|
|
|
return message.respond(message.format('C_SETTINGS_RESETINVALID'), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
|
|
|
} else if(!boolean) {
|
|
|
|
return message.respond(message.format('C_SETTINGS_RESETABORT'), {
|
|
|
|
emoji: 'success'
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
return this._resetSettings(message, type);
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const settings = this.client.resolver.components(target, 'setting', true).sort((c) => c.resolve === type);
|
|
|
|
if(settings.length === 0) {
|
|
|
|
return message.respond(message.format('C_SETTINGS_SETTINGNOTFOUND'), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
2020-05-07 01:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const [ setting ] = settings;
|
2020-05-24 00:10:29 +02:00
|
|
|
message._settingCaller = target;
|
2020-05-06 09:11:00 +02:00
|
|
|
|
2020-05-17 15:27:35 +02:00
|
|
|
const parameters = params.splice(1);
|
2020-08-16 09:27:49 +02:00
|
|
|
if(parameters.length === 0) {
|
|
|
|
return this._showSetting(message, setting);
|
2020-05-17 15:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
if(setting.resolve === 'GUILD') {
|
|
|
|
if(setting.clientPermissions.length > 0) {
|
|
|
|
const missing = message.guild.permissionsFor(message.guild.me).missing(setting.clientPermissions);
|
|
|
|
if(missing.length > 0) {
|
|
|
|
await message.respond(message.format('C_SETTINGS_MISSINGCLIENTPERMISSIONS', { permissions: missing.map((m) => `\`${PermissionNames[m]}\``).join(', ') }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parameters.join(' ').toLowerCase() === 'reset') {
|
|
|
|
return this._resetSetting(message, setting);
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
if(setting.premium > message.guild.premium && !message.author.developer) {
|
|
|
|
return message.respond(message.format('C_SETTINGS_PREMIUM', { current: message.guild.premium, required: setting.premium }), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
|
|
|
}
|
2020-07-11 22:40:25 +02:00
|
|
|
|
2020-05-17 15:27:35 +02:00
|
|
|
const response = await setting.handle(message, parameters);
|
2020-05-25 13:13:34 +02:00
|
|
|
if(response.ignore) return undefined;
|
2020-08-16 09:27:49 +02:00
|
|
|
|
|
|
|
message.respond(response.msg, {
|
|
|
|
emoji: response.error ? 'failure' : 'success',
|
|
|
|
embed: response.embed || null
|
|
|
|
});
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
async _listSettings(message, type, all = false) {
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const prefix = message?.guild?.prefix
|
|
|
|
|| this.client._options.bot.prefix;
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-07-24 20:31:52 +02:00
|
|
|
const fields = [];
|
2020-05-06 01:40:46 +02:00
|
|
|
const sorted = this.client.registry.components
|
2020-07-24 20:49:32 +02:00
|
|
|
.filter((c) => c.type === 'module')
|
2020-05-06 01:40:46 +02:00
|
|
|
.sort((a, b) => {
|
2020-07-24 20:31:52 +02:00
|
|
|
const filter = (c) => c.type === 'setting';
|
2020-08-16 09:27:49 +02:00
|
|
|
return b.components.filter(filter).size - a.components.filter(filter).size;
|
2020-05-06 01:40:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for(const module of sorted.values()) {
|
2020-07-24 20:31:52 +02:00
|
|
|
const field = {
|
2020-05-06 01:40:46 +02:00
|
|
|
name: module.id,
|
|
|
|
value: '',
|
|
|
|
inline: true
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const setting of module.components.values()) {
|
2020-08-16 09:27:49 +02:00
|
|
|
if(setting.type !== 'setting'
|
2020-07-24 20:31:52 +02:00
|
|
|
|| setting.resolve !== type && !all
|
|
|
|
|| !setting.archivable && !all) continue;
|
2020-05-06 01:40:46 +02:00
|
|
|
field.value += `\`${setting.display}\`\n`;
|
|
|
|
}
|
|
|
|
if(field.value) fields.push(field);
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
2020-08-16 09:27:49 +02:00
|
|
|
|
|
|
|
for(let i = 0; i < fields.length % 3; i++) {
|
|
|
|
fields.push({
|
|
|
|
name: '\u200b',
|
|
|
|
value: '\u200b',
|
|
|
|
inline: true
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
const embed = {
|
|
|
|
author: {
|
|
|
|
name: `${type === 'GUILD' ? message.format('C_SETTINGS_GUILDSETTINGSTITLE') : message.format('C_SETTINGS_USERSETTINGSTITLE')}`,
|
2020-08-16 09:27:49 +02:00
|
|
|
icon_url: type === 'GUILD' ? message.guild.iconURL() : message.author.displayAvatarURL() //eslint-disable-line camelcase
|
2020-05-06 01:40:46 +02:00
|
|
|
},
|
|
|
|
description: stripIndents`${message.format('C_SETTINGS_LISTSETTINGS', { prefix })}
|
|
|
|
|
|
|
|
${type === 'USER' ? '' : message.format('C_SETTINGS_LISTSETTINGSALT', { prefix })}`,
|
|
|
|
fields
|
|
|
|
};
|
|
|
|
|
|
|
|
return message.embed(embed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:13:24 +02:00
|
|
|
async _showSetting(message, setting) {
|
2020-05-21 12:47:58 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const embed = setting.usageEmbed(message);
|
2020-05-21 12:47:58 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
let dataFields = setting.fields(message.guild);
|
|
|
|
if(dataFields instanceof Promise) dataFields = await dataFields;
|
2020-05-18 01:13:24 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
embed.fields = embed.fields.concat(dataFields);
|
|
|
|
embed.author.icon_url = setting.resolve === 'GUILD' ? message.guild.iconURL() : message.author.displayAvatarURL(); //eslint-disable-line camelcase
|
2020-05-18 01:13:24 +02:00
|
|
|
|
2020-07-24 20:49:32 +02:00
|
|
|
return message.embed(embed);
|
2020-05-18 01:13:24 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
async _exportSettings(message, type) {
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const settings = type === 'USER'
|
|
|
|
? await message.author.settings()
|
|
|
|
: await message.guild.settings();
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
const string = JSON.stringify(settings);
|
|
|
|
const attachment = new MessageAttachment(Buffer.from(string), 'settings.json');
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
return message.respond(message.format('C_SETTINGS_EXPORT'), {
|
|
|
|
emoji: 'success',
|
|
|
|
files: [ attachment ]
|
|
|
|
});
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
async _resetSettings(message, type) { //Reset ALL settings.
|
|
|
|
if(type === 'USER') {
|
|
|
|
await message.author._resetSettings();
|
|
|
|
} else {
|
|
|
|
await message.guild._resetSettings();
|
|
|
|
}
|
2020-05-24 10:54:33 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
return message.respond(message.format('C_SETTINGS_RESETSUCCESS', { type: message.format('C_SETTINGS_FORMATTYPE', { type }, true) }), {
|
|
|
|
emoji: 'success'
|
|
|
|
});
|
|
|
|
}
|
2020-05-24 10:54:33 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
async _resetSetting(message, setting) { //Reset an INDIVIDUAL setting.
|
|
|
|
const { error, response } = await setting._handleReset(message);
|
|
|
|
if(error) {
|
|
|
|
return message.respond(message.format('C_SETTINGS_RESETSETTINGFAIL'), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
|
|
|
}
|
2020-05-24 10:54:33 +02:00
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
return message.respond(response, {
|
|
|
|
emoji: 'success'
|
|
|
|
});
|
2020-05-24 10:54:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:31:52 +02:00
|
|
|
module.exports = SettingsCommand;
|