galactic-bot/structure/client/components/settings/administration/IgnoreChannels.js
2021-05-08 12:33:41 +03:00

151 lines
5.6 KiB
JavaScript

const { Setting } = require('../../../../interfaces');
class IgnoreSetting extends Setting {
constructor(client) {
super(client, {
name: 'ignoreChannels',
module: 'administration',
aliases: [
'ignoreChannel',
'channelIgnore',
'channelIgnores',
'ignore'
],
usage: '<method> [value..]',
examples: [
'ignore add #channel',
'ignore remove #channel',
'ignore bypass add role',
'ignore bypass remove role'
],
guarded: false,
resolve: 'GUILD',
default: {
ignoreChannels: {
channels: [],
enabled: false,
roleBypass: []
}
},
custom: true
});
}
async handle(message, params) {
let index = null,
method = null;
const langParams = {};
const setting = message.guild._settings[this.index];
const { guild } = message;
const { resolver } = this.client;
const response = await resolver.resolveMethod(params, { existing: setting.channels, resolver: resolver.resolveChannels.bind(resolver), guild });
if (!response) method = params.shift();
else ({ method } = response);
if (method === 'add') {
const channels = response.resolved;
if (!channels) return { error: true, msg: message.format('ERR_CHRESOLVE') };
setting.channels = response.result;
setting.enabled = true;
index = 'S_IGNORE_ADD';
const changed = channels.filter((c) => response.changed.includes(c.id)).map((c) => c.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else if (method === 'remove') {
const channels = response.resolved;
if (!channels) return { error: true, msg: message.format('ERR_CHRESOLVE') };
setting.channels = response.result;
setting.enabled = setting.channels.length && true;
index = 'S_IGNORE_REMOVE';
const changed = channels.filter((c) => response.changed.includes(c.id)).map((c) => c.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else if (method === 'list') {
const roles = await resolver.resolveRoles(setting.roleBypass, false, guild);
index = 'S_IGNORE_LIST';
const ch = response.resolved ? response.resolved?.map((c) => c.name).join('`, `') : '';
const r = roles.length ? roles.map((r) => r.name).join('`, `') : '';
langParams.channels = ch.length ? ch : 'N/A';
langParams.roles = r.length ? r : 'N/A';
} else if (method === 'set') {
const channels = response.resolved;
if (!channels) return { error: true, msg: message.format('ERR_CHRESOLVE') };
setting.channels = channels.map((c) => c.id);
setting.enabled = channels.length && true;
index = 'S_IGNORE_SET';
const ch = channels.map((c) => c.name).join('`, `');
langParams.changes = ch.length ? ch : 'N/A';
} else if (method === 'bypass') {
const response = await resolver.resolveMethod(params, { existing: setting.roleBypass, resolver: resolver.resolveRoles.bind(resolver), guild });
if (response.method === 'add') {
const roles = response.resolved;
if (!roles) return { error: true, msg: message.format('ERR_ROLERESOLVE') };
setting.roleBypass = response.result;
index = 'S_IGNORE_BYPASS_ADD';
const changed = roles.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else if (response.method === 'remove') {
const roles = response.resolved;
if (!roles) return { error: true, msg: message.format('ERR_ROLERESOLVE') };
setting.roleBypass = response.result;
index = 'S_IGNORE_BYPASS_REMOVE';
const changed = roles.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else return { error: true, msg: message.format('ERR_INVALID_SUBMETHOD', { submethod: params[0] }) };
} else return { error: true, msg: message.format('ERR_INVALID_METHOD', { method }) };
await message.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?.roleBypass ? await Promise.all(setting.roleBypass.map((role) => guild.resolveRole(role))) : undefined;
const channels = setting?.channels ? await Promise.all(setting.channels.map((ch) => guild.resolveChannel(ch))) : undefined;
return [
{
name: '》 Status',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.enabled) }, true)
},
{
name: '》 Channels',
value: channels?.map((c) => `<#${c.id}>`).join(', ') || '`N/A`'
},
{
name: '》 Roles',
value: roles?.map((r) => `<@&${r.id}>`).join(', ') || '`N/A`'
}
];
}
}
module.exports = IgnoreSetting;