galactic-bot/structure/client/components/settings/administration/IgnoreChannels.js

151 lines
5.6 KiB
JavaScript
Raw Normal View History

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