galactic-bot/structure/client/components/settings/moderation/Protection.js

113 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-07-11 22:36:40 +02:00
const { Setting } = require('../../../../interfaces/');
class ProtectionSetting extends Setting {
constructor(client) {
super(client, {
name: 'protection',
module: 'moderation',
usage: "<'type'|'add'|'remove'> <'role'|'position'|role..>",
2020-07-11 22:36:40 +02:00
examples: [
'protection type <role|position>',
'protection add <role..>',
'protection remove <role..>'
],
resolve: 'GUILD',
default: {
protection: {
type: 'position',
roles: []
}
},
custom: true
});
}
async handle(message, params) {
const { guild } = message;
const { resolver } = this.client;
const setting = guild._settings[this.index];
let method = params[0].toLowerCase(),
index = null;
const langParams = {};
if (method === 'type') {
if (!params[1]) return message.format('MISSING_ARGS');
const value = params[1].toLowerCase();
if (!['role', 'position'].includes(value)) return message.format('S_PROTECTION_INVALID_ARGS');
setting.type = value;
index = 'S_PROTECTION_TYPE';
langParams.type = value;
} else {
const response = await resolver.resolveMethod(params, null, setting.roles, resolver.resolveRoles.bind(resolver), guild);
if (!response) return message.format('INVALID_METHOD', { method });
({ method } = response);
if (method === 'add') {
setting.roles = response.result;
setting.enabled = true;
index = 'S_PROTECTION_ADD';
langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `');
} else if (method === 'remove') {
setting.roles = response.result;
setting.enabled = setting.roles.length && true;
index = 'S_PROTECTION_REMOVE';
langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `');
} else if (method === 'set') {
setting.roles = response.result;
setting.enabled = true;
index = 'S_PROTECTION_SET';
langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `');
} else if (method === 'list') {
index = 'S_AUTOROLE_LIST';
langParams.list = response.resolved.map((r) => r.name).join(', ');
}
}
await guild._updateSettings({
[this.index]: setting
});
return { error: false, msg: message.format(index, langParams) };
}
async fields(guild) {
const setting = guild._settings[this.index];
const roles = setting?.roles ? await Promise.all(setting.roles.map(async (role) => guild.resolveRole(role))) : undefined;
return [
{
name: '》 Type',
value: `\`${setting.type}\``,
2020-07-11 22:36:40 +02:00
inline: true
}, {
name: '》 Roles',
value: roles?.map((r) => `<@&${r.id}>`).join(' ') || '`N/A`',
inline: true
2020-07-11 22:36:40 +02:00
}
];
}
}
module.exports = ProtectionSetting;