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

225 lines
7.9 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 { 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-05-05 01:35:01 +02:00
module: 'utility',
aliases: [
'setting',
2020-05-05 01:35:01 +02:00
'set'
],
usage: "[list|reset|setting-name] <value..>",
arguments: [
{
name: 'user',
type: 'BOOLEAN',
types: ['VERBAL', 'FLAG'],
default: true
},
{
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-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;
}
async execute(message, { params, args }) {
2020-05-05 01:35:01 +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') {
this._listSettings(message, type, Boolean(args.all));
return undefined;
2020-05-05 01:35:01 +02:00
} else if(target === 'reset') {
if(type === 'GUILD' && !this._checkAdministrator(message)) return undefined; //does not have admin
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-07-11 22:40:25 +02:00
const settings = this.client.resolver.components(target, 'setting', true).sort(c=>c.resolve === type);
const [ setting ] = settings;
2020-05-05 01:35:01 +02:00
if(!setting) {
await message.respond(message.format('C_SETTINGS_NONEXISTANT'), { emoji: 'failure' });
return undefined;
}
//Setting permission handling
if(setting.clientPermissions.length > 0 && setting.resolve === 'GUILD') {
const missing = message.channel.permissionsFor(message.guild.me).missing(setting.clientPermissions);
if(missing.length > 0) {
await message.respond(message.format('C_SETTINGS_CLIENTPERMISSIONERROR', { setting: setting.moduleResolveable, missing: missing.join(', ')}), { emoji: 'failure' });
return undefined;
}
}
message._settingCaller = target;
if(setting.resolve === 'GUILD' && !this._checkAdministrator(message)) return undefined; //does not have admin
const parameters = params.splice(1);
if(!parameters || parameters.length === 0) {
await this._showSetting(message, setting)
return undefined;
}
2020-06-10 16:48:45 +02:00
if(['reset', 'clear'].includes(parameters.join(' ').toLowerCase())) {
const { msg } = await setting._handleReset(message);
return message.respond(msg, { emoji: 'success' });
}
2020-07-11 22:40:25 +02:00
if (setting.premium > message.guild.premium) return message.formattedRespond('PREMIUM_REQUIRED', { emoji: 'failure', params: { tier: message.guild.premium, required: setting.premium } });
const response = await setting.handle(message, parameters);
if(response.ignore) return undefined;
message.respond(response.msg, { emoji: response.error ? 'failure' : 'success', embed: response.embed || null });
}
_listSettings(message, type, all) {
if(!message.guild && type === 'GUILD') type = 'USER';
2020-05-24 05:50:35 +02:00
const prefix = message.guild?.prefix;
let fields = [];
const sorted = this.client.registry.components
.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()) {
let field = {
name: module.id,
value: '',
inline: true
};
for(const setting of module.components.values()) {
if(setting.type !== 'setting'
|| (setting.resolve !== type && !all)
|| (!setting.archivable && !all)) continue;
field.value += `\`${setting.display}\`\n`;
}
if(field.value) fields.push(field);
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.avatarURL()
},
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 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)*'}
${message.format(setting.description)}`;
if(setting.examples.length > 0) {
2020-05-24 10:54:33 +02:00
description += `\n\n__${message.format('GENERAL_EXAMPLES')}__`;
for(let example of setting.examples) {
2020-05-22 22:13:47 +02:00
description += `\n\`${prefix}settings ${example}\``;
}
}
let fields = setting.fields(message.guild);
2020-06-10 16:48:45 +02:00
if (fields instanceof Promise) fields = await fields;
//console.log(fields)
const embed = {
author: {
name: `${setting.name} (${setting.module.resolveable})`,
icon_url: this.client.user.displayAvatarURL()
},
description,
fields
}
return await message.embed(embed);
}
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'
? await message.author._deleteSettings()
: await message.guild._deleteSettings();
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 ] })
}
_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
}
module.exports = SettingsCommand;