galactic-bot/structure/client/components/commands/administration/Settings.js

249 lines
8.4 KiB
JavaScript
Raw Normal View History

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');
const { PermissionNames } = require('../../../../../util/Constants.js');
const { stripIndents } = require('common-tags');
2020-05-05 01:35:01 +02:00
class SettingsCommand extends Command {
2020-05-05 01:35:01 +02:00
constructor(client) {
super(client, {
name: 'settings',
2020-07-28 05:00:24 +02:00
module: 'administration',
2020-05-05 01:35:01 +02:00
aliases: [
'setting',
2020-05-05 01:35:01 +02:00
'set'
],
usage: "['list'|'reset'|setting] [value..]",
arguments: [
{
name: 'export',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'user',
type: 'BOOLEAN',
types: ['VERBAL', 'FLAG'],
default: true
2020-05-24 10:54:33 +02:00
},
{
name: 'all',
2020-05-24 10:54:33 +02:00
type: 'BOOLEAN',
types: ['VERBAL', 'FLAG'],
default: true,
archivable: false
}
],
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS'],
2020-09-01 22:02:18 +02:00
keepQuotes: false,
2020-05-22 22:13:47 +02:00
showUsage: true,
examples: [
'prefix !',
'modlogs #channel'
2020-05-24 10:54:33 +02:00
],
throttling: {
usages: 2,
duration: 5
2020-05-24 10:54:33 +02:00
}
2020-05-05 01:35:01 +02:00
});
this.client = client;
}
async execute(message, { params, args }) {
const type = !message.guild || args.user ? 'USER' : 'GUILD';
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
if(args.export) return this._exportSettings(message, type);
2020-05-24 10:54:33 +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') {
return this._listSettings(message, type, Boolean(args.all));
2020-05-05 01:35:01 +02:00
} else if(target === 'reset') {
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
return this._resetSettings(message, type);
}
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'
});
}
const [ setting ] = settings;
message._settingCaller = target;
const parameters = params.splice(1);
if(parameters.length === 0) {
return this._showSetting(message, setting);
}
if(setting.resolve === 'GUILD') {
if(setting.clientPermissions.length > 0) {
const missing = message.guild.me.permissions.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);
}
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
const response = await setting.handle(message, parameters);
if(!response || response.ignore) return undefined;
message.respond(response.msg, {
emoji: response.error ? 'failure' : 'success',
embed: response.embed || null,
disableMentions: 'all'
});
}
async _listSettings(message, type, all = false) {
const prefix = message?.guild?.prefix
|| this.client._options.bot.prefix;
const fields = [];
const sorted = this.client.registry.components
2020-07-24 20:49:32 +02:00
.filter((c) => c.type === 'module')
.sort((a, b) => {
const filter = (c) => c.type === 'setting';
return b.components.filter(filter).size - a.components.filter(filter).size;
});
for(const module of sorted.values()) {
const field = {
name: module.id,
value: '',
inline: true
};
for(const setting of module.components.values()) {
if(setting.type !== 'setting'
|| type !== setting.resolve
|| !setting.archivable && !all) continue;
field.value += `\`${message.author._settings.camelCase ? setting.name : setting.name.toLowerCase()}\`\n`;
}
if(field.value) fields.push(field);
2020-05-05 01:35:01 +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
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.displayAvatarURL() //eslint-disable-line camelcase
},
description: stripIndents`${message.format('C_SETTINGS_LISTSETTINGS', { prefix })}
${type === 'USER' ? '' : message.format('C_SETTINGS_LISTSETTINGSALT', { prefix })}`,
fields
};
return message.embed(embed);
}
async _showSetting(message, setting) {
const embed = setting.usageEmbed(message);
let dataFields = setting.fields(message.guild);
if(dataFields instanceof Promise) dataFields = await dataFields;
embed.fields = embed.fields.concat(dataFields);
embed.author.icon_url = setting.resolve === 'GUILD' ? message.guild.iconURL() : message.author.displayAvatarURL(); //eslint-disable-line camelcase
2020-07-24 20:49:32 +02:00
return message.embed(embed);
}
async _exportSettings(message, type) {
const settings = type === 'USER'
? await message.author.settings()
: await message.guild.settings();
const string = JSON.stringify(settings);
const attachment = new MessageAttachment(Buffer.from(string), 'settings.json');
return message.respond(message.format('C_SETTINGS_EXPORT'), {
emoji: 'success',
files: [ attachment ]
});
2020-05-05 01:35:01 +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
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
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
return message.respond(response, {
emoji: 'success'
});
2020-05-24 10:54:33 +02:00
}
2020-05-05 01:35:01 +02:00
}
module.exports = SettingsCommand;