2020-05-06 01:40:46 +02:00
|
|
|
const { Command, Argument } = require('../../../../interfaces/');
|
|
|
|
|
|
|
|
const { stripIndents } = require('common-tags');
|
2020-05-05 01:35:01 +02:00
|
|
|
|
|
|
|
class SettingCommand extends Command {
|
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
super(client, {
|
|
|
|
name: 'setting',
|
|
|
|
module: 'utility',
|
|
|
|
description: "Sets user or guild settings.",
|
|
|
|
aliases: [
|
|
|
|
'settings',
|
|
|
|
'set'
|
|
|
|
],
|
2020-05-06 01:40:46 +02:00
|
|
|
arguments: [
|
|
|
|
new Argument(client, {
|
|
|
|
name: 'user',
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
|
|
|
}),
|
|
|
|
new Argument(client, {
|
|
|
|
name: 'all',
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
types: ['VERBAL', 'FLAG'],
|
|
|
|
default: true
|
|
|
|
})
|
|
|
|
],
|
2020-05-07 01:26:16 +02:00
|
|
|
showUsage: true
|
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
|
|
|
|
|
|
|
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-06 09:11:00 +02:00
|
|
|
if(message.channel.permissionsFor(message.member).missing('ADMINISTRATOR').length > 0) {
|
|
|
|
await message.respond(message.format('C_SETTINGS_ADMINISTRATORERROR', { type: type.toLowerCase() }), { emoji: 'failure' });
|
|
|
|
return undefined;
|
|
|
|
}
|
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
|
|
|
} else if(target === 'walkthrough') {
|
2020-05-06 01:40:46 +02:00
|
|
|
//TODO
|
|
|
|
return undefined;
|
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
|
|
|
|
if(setting.clientPermissions.length > 0) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if(message.channel.permissionsFor(message.member).missing('ADMINISTRATOR').length > 0 && setting.resolve === 'GUILD') {
|
|
|
|
await message.respond(message.format('C_SETTINGS_ADMINISTRATORERROR', { type: type.toLowerCase() }), { emoji: 'failure' });
|
|
|
|
return undefined;
|
2020-05-06 09:11:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
const response = await setting.handle(message, params.splice(1));
|
|
|
|
message.respond(response.msg, { emoji: response.error ? 'failure' : 'success' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_listSettings(message, type, all) {
|
|
|
|
if(!message.guild && type === 'GUILD') type = 'USER';
|
|
|
|
|
2020-05-06 09:11:00 +02:00
|
|
|
const prefix = message.guild?.prefix || this.client._options.bot.prefix;
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
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) - a.components.filter(filter);
|
|
|
|
});
|
|
|
|
|
|
|
|
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.restricted && !all)) continue;
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 01:26:16 +02:00
|
|
|
? await message.author._delete()
|
|
|
|
: await message.guild._delete('guilds');
|
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-06 01:40:46 +02:00
|
|
|
module.exports = SettingCommand;
|