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

118 lines
3.7 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
class ProtectionSetting extends Setting {
constructor(client) {
super(client, {
name: 'protection',
module: 'moderation',
usage: "<'type'|'add'|'remove'> <'role'|'position'|role..>",
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 {
error: true,
msg: message.format('MISSING_ARGS')
};
const value = params[1].toLowerCase();
if (!['role', 'position'].includes(value)) return { error: true, msg: 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: resolver.resolveRoles.bind(resolver), guild });
if (!response) return { error: true, msg: message.format('ERR_INVALID_METHOD', { method }) };
({ method } = response);
if (method !== 'list' && !response.resolved.length) return { error: true, msg: message.format('ERR_ROLERESOLVE') };
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}\``,
inline: true
}, {
name: '》 Roles',
value: roles?.map((r) => `<@&${r.id}>`).join(' ') || '`N/A`',
inline: true
}
];
}
}
module.exports = ProtectionSetting;