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
|
|
|
|
|
|
|
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-05-05 01:35:01 +02:00
|
|
|
module: 'utility',
|
|
|
|
aliases: [
|
2020-05-17 15:27:35 +02:00
|
|
|
'setting',
|
2020-05-05 01:35:01 +02:00
|
|
|
'set'
|
|
|
|
],
|
2020-05-17 15:27:35 +02:00
|
|
|
usage: "[list|reset|setting-name] <value..>",
|
2020-05-06 01:40:46 +02:00
|
|
|
arguments: [
|
2020-05-08 08:50:54 +02:00
|
|
|
{
|
2020-05-06 01:40:46 +02:00
|
|
|
name: 'user',
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
2020-05-08 08:50:54 +02:00
|
|
|
},
|
|
|
|
{
|
2020-05-06 01:40:46 +02:00
|
|
|
name: 'all',
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
2020-05-24 10:54:33 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'raw',
|
|
|
|
aliases: ['json'],
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
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: {
|
|
|
|
usages: 1,
|
|
|
|
duration: 3
|
|
|
|
}
|
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-05-05 01:35:01 +02:00
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
const type = (!message.guild || args.user) ? 'USER' : 'GUILD';
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-05-24 10:54:33 +02:00
|
|
|
if(args.raw) return this._showRaw(message, type);
|
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
const target = params[0].toLowerCase(); // params[0] should never be null, see showUsage
|
|
|
|
if(target === 'list') {
|
2020-05-06 01:40:46 +02:00
|
|
|
this._listSettings(message, type, Boolean(args.all));
|
|
|
|
return undefined;
|
2020-05-05 01:35:01 +02:00
|
|
|
} else if(target === 'reset') {
|
2020-05-21 12:47:58 +02:00
|
|
|
if(type === 'GUILD' && !this._checkAdministrator(message)) return undefined; //does not have admin
|
2020-05-06 01:40:46 +02:00
|
|
|
const prompt = await message.prompt(message.format('C_SETTINGS_RESET', { type: type.toLowerCase() }), { emoji: 'warning' });
|
|
|
|
return await this._handleReset(prompt, message, type);
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
const settings = this.client.resolver.components(target, 'setting', false).sort(c=>c.resolve === type);
|
|
|
|
const [ setting ] = settings;
|
2020-05-05 01:35:01 +02:00
|
|
|
if(!setting) {
|
2020-05-06 01:40:46 +02:00
|
|
|
await message.respond(message.format('C_SETTINGS_NONEXISTANT'), { emoji: 'failure' });
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-05-06 09:11:00 +02:00
|
|
|
//Setting permission handling
|
2020-05-21 12:47:58 +02:00
|
|
|
if(setting.clientPermissions.length > 0 && setting.resolve === 'GUILD') {
|
2020-05-07 01:26:16 +02:00
|
|
|
const missing = message.channel.permissionsFor(message.guild.me).missing(setting.clientPermissions);
|
2020-05-06 09:11:00 +02:00
|
|
|
if(missing.length > 0) {
|
|
|
|
await message.respond(message.format('C_SETTINGS_CLIENTPERMISSIONERROR', { setting: setting.moduleResolveable, missing: missing.join(', ')}), { emoji: 'failure' });
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-05-07 01:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 00:10:29 +02:00
|
|
|
message._settingCaller = target;
|
2020-05-21 12:47:58 +02:00
|
|
|
if(setting.resolve === 'GUILD' && !this._checkAdministrator(message)) return undefined; //does not have admin
|
2020-05-06 09:11:00 +02:00
|
|
|
|
2020-05-17 15:27:35 +02:00
|
|
|
const parameters = params.splice(1);
|
|
|
|
if(!parameters || parameters.length === 0) {
|
2020-05-18 01:13:24 +02:00
|
|
|
await this._showSetting(message, setting)
|
2020-05-17 15:27:35 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
if(parameters.join(' ').toLowerCase() === 'reset') {
|
2020-05-24 00:10:29 +02:00
|
|
|
const { msg } = await setting._handleReset(message);
|
|
|
|
return message.respond(msg, { emoji: 'success' });
|
2020-05-21 12:47:58 +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-05-06 01:40:46 +02:00
|
|
|
message.respond(response.msg, { emoji: response.error ? 'failure' : 'success' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_listSettings(message, type, all) {
|
|
|
|
if(!message.guild && type === 'GUILD') type = 'USER';
|
|
|
|
|
2020-05-24 05:50:35 +02:00
|
|
|
const prefix = message.guild?.prefix;
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
let fields = [];
|
|
|
|
const sorted = this.client.registry.components
|
2020-05-21 12:47:58 +02:00
|
|
|
.filter(c=>c.type == 'module')
|
2020-05-06 01:40:46 +02:00
|
|
|
.sort((a, b) => {
|
|
|
|
const filter = c=>c.type === 'setting';
|
2020-05-25 13:13:34 +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()) {
|
|
|
|
let field = {
|
|
|
|
name: module.id,
|
|
|
|
value: '',
|
|
|
|
inline: true
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const setting of module.components.values()) {
|
|
|
|
if(setting.type !== 'setting'
|
|
|
|
|| (setting.resolve !== type && !all)
|
2020-05-25 13:13:34 +02:00
|
|
|
|| (!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-05-06 01:40:46 +02:00
|
|
|
const embed = {
|
|
|
|
author: {
|
|
|
|
name: `${type === 'GUILD' ? message.format('C_SETTINGS_GUILDSETTINGSTITLE') : message.format('C_SETTINGS_USERSETTINGSTITLE')}`,
|
|
|
|
icon_url: type === 'GUILD' ? message.guild.iconURL() : message.author.avatarURL()
|
|
|
|
},
|
|
|
|
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
|
|
|
|
|
|
|
const prefix = message.guild.prefix;
|
2020-05-22 22:13:47 +02:00
|
|
|
let description = stripIndents`\`${prefix}settings ${setting.name}${setting.usage ? ` ${setting.usage}` : ''}\` ${setting.resolve === 'GUILD' ? ' *(guild-only)*' : '*(user-only)*'}
|
2020-05-21 12:47:58 +02:00
|
|
|
${message.format(setting.description)}`;
|
|
|
|
|
|
|
|
if(setting.examples.length > 0) {
|
2020-05-24 10:54:33 +02:00
|
|
|
description += `\n\n__${message.format('GENERAL_EXAMPLES')}__`;
|
2020-05-21 12:47:58 +02:00
|
|
|
for(let example of setting.examples) {
|
2020-05-22 22:13:47 +02:00
|
|
|
description += `\n\`${prefix}settings ${example}\``;
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 01:13:24 +02:00
|
|
|
let fields = setting.fields(message.guild);
|
|
|
|
if(fields instanceof Promise) fields = await fields;
|
|
|
|
|
|
|
|
const embed = {
|
|
|
|
author: {
|
|
|
|
name: `${setting.name} (${setting.module.resolveable})`,
|
|
|
|
icon_url: this.client.user.displayAvatarURL()
|
|
|
|
},
|
2020-05-21 12:47:58 +02:00
|
|
|
description,
|
2020-05-18 01:13:24 +02:00
|
|
|
fields
|
|
|
|
}
|
|
|
|
|
|
|
|
return await message.embed(embed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
async _handleReset(prompt, message, type) {
|
|
|
|
|
|
|
|
if(!prompt) return;
|
|
|
|
const response = prompt.content.toLowerCase();
|
|
|
|
const bool = this.client.resolver.resolveBoolean(response);
|
|
|
|
if(bool === null) return message.respond(message.format('C_SETTINGS_RESETERROR'), { emoji: 'failure' });
|
|
|
|
if(!bool) return message.respond(message.format('C_SETTINGS_RESETABORT'), { emoji: 'success' });
|
|
|
|
|
|
|
|
type === 'USER'
|
2020-05-17 15:27:35 +02:00
|
|
|
? await message.author._deleteSettings()
|
|
|
|
: await message.guild._deleteSettings();
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
return message.respond(message.format('C_SETTINGS_RESETSUCCESS', { type: type.toLowerCase() }), { emoji: 'success' })
|
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 10:54:33 +02:00
|
|
|
async _showRaw(message, type) {
|
|
|
|
|
|
|
|
const settings = type === 'USER'
|
|
|
|
? message.author._settings
|
|
|
|
: message.guild._settings;
|
|
|
|
|
|
|
|
const string = JSON.stringify(settings);
|
|
|
|
const attachment = new MessageAttachment(Buffer.from(string), "settings.json");
|
|
|
|
|
|
|
|
return await message.respond(message.format('C_SETTINGS_JSON'), { emoji: 'success', attachments: [ attachment ] })
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
_checkAdministrator(message) {
|
|
|
|
const missing = message.channel.permissionsFor(message.member).missing(['ADMINISTRATOR']);
|
|
|
|
if(missing > 0) {
|
|
|
|
message.respond(message.format('C_SETTINGS_ADMINISTRATORERROR'), { emoji: 'failure' })
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 15:27:35 +02:00
|
|
|
module.exports = SettingsCommand;
|