diff --git a/structure/client/components/settings/utility/IgnoreChannels.js b/structure/client/components/settings/utility/IgnoreChannels.js new file mode 100644 index 0000000..64ef82b --- /dev/null +++ b/structure/client/components/settings/utility/IgnoreChannels.js @@ -0,0 +1,144 @@ +const { Setting } = require('../../../../interfaces'); + +class IgnoreSetting extends Setting { + + constructor(client) { + + super(client, { + name: 'ignore', + module: 'utility', + aliases: [ + 'channelignore', + 'ignorechannel' + ], + usage: ' [value..]', + examples: [ + 'ignore add #channel', + 'ignore remove #channel', + 'ignore bypass add role', + 'ignore bypass remove role' + ], + guarded: false, + resolve: 'GUILD', + default: { + ignore: { + channels: [], + enabled: false, + roleBypass: [] + } + }, + custom: true + }); + + this.client = client; + + } + + async handle(message, params) { + + let index = null, + langParams = {}, + method = null; + + const setting = message.guild._settings[this.index] || this.default[this.index]; + const { guild } = message; + const { resolver } = this.client; + const response = await resolver.resolveMethod(params, null, setting.channels, resolver.resolveChannels.bind(resolver), guild); + + if (!response) method = params.shift(); + else ({ method } = response); + + if (method === 'add') { + + const channels = response.resolved; + 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; + 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; + 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, null, setting.roleBypass, resolver.resolveRoles.bind(resolver), guild); + + if (response.method === 'add') { + + const roles = response.resolved; + 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; + 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(async (role) => guild.resolveRole(role))) : undefined; + const channels = setting?.channels ? guild.resolveChannels(setting.channels) || undefined : undefined; + + return [ + { + name: '》Enabled', + value: setting?.enabled || 'false' + }, { + name: '》Channels', + value: channels?.map(c => c.name).join(', ') || 'N/A' + }, { + name: '》Roles', + value: roles?.map(r => r.name).join(', ') || 'N/A' + } + ]; + + } + +} + +module.exports = IgnoreSetting; \ No newline at end of file