From 553c84379ed983dc673385a2b7a7753b5673b32b Mon Sep 17 00:00:00 2001 From: Navy Date: Wed, 30 Sep 2020 19:23:36 +0300 Subject: [PATCH] further refactor resolveMethod --- .../settings/administration/IgnoreChannels.js | 4 ++-- .../components/settings/moderation/LinkFilter.js | 12 +++++++----- .../components/settings/moderation/MessageLog.js | 10 +++++----- .../settings/moderation/ModerationPoints.js | 2 +- .../components/settings/moderation/Protection.js | 2 +- .../client/components/settings/utility/Autorole.js | 7 ++++--- .../client/components/settings/utility/StickyRole.js | 3 ++- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/structure/client/components/settings/administration/IgnoreChannels.js b/structure/client/components/settings/administration/IgnoreChannels.js index befaf91..c2e82ce 100644 --- a/structure/client/components/settings/administration/IgnoreChannels.js +++ b/structure/client/components/settings/administration/IgnoreChannels.js @@ -43,7 +43,7 @@ class IgnoreSetting extends Setting { const setting = message.guild._settings[this.index]; const { guild } = message; const { resolver } = this.client; - const response = await resolver.resolveMethod(params, null, setting.channels, { resolver: resolver.resolveChannels.bind(resolver), guild }); + const response = await resolver.resolveMethod(params, { existing: setting.channels, resolver: resolver.resolveChannels.bind(resolver), guild }); if (!response) method = params.shift(); else ({ method } = response); @@ -93,7 +93,7 @@ class IgnoreSetting extends Setting { } else if (method === 'bypass') { - const response = await resolver.resolveMethod(params, null, setting.roleBypass, { resolver: resolver.resolveRoles.bind(resolver), guild }); + const response = await resolver.resolveMethod(params, { existing: setting.roleBypass, resolver: resolver.resolveRoles.bind(resolver), guild }); if (response.method === 'add') { diff --git a/structure/client/components/settings/moderation/LinkFilter.js b/structure/client/components/settings/moderation/LinkFilter.js index 0988afd..82b7dce 100644 --- a/structure/client/components/settings/moderation/LinkFilter.js +++ b/structure/client/components/settings/moderation/LinkFilter.js @@ -65,7 +65,7 @@ module.exports = class LinkFilter extends FilterSetting { } else if (['blacklist', 'whitelist'].includes(method)) { - const result = await resolver.resolveMethod(args, null, null, { + const result = await resolver.resolveMethod(args, { allowedMethods: ['add', 'remove', 'set', 'reset'], resolver: resolver.resolveDomains.bind(resolver) }); @@ -117,7 +117,7 @@ module.exports = class LinkFilter extends FilterSetting { let msg = message.format(index, langParams); if (existsInOpposite.length) msg += '\n\n' + message.format('S_LINKFILTER_OPPOSITE', { opposite: existsInOpposite.join('`, `'), op: _opposite }); if (skipped.length) msg += '\n\n' + message.format('S_LINKFILTER_SKIPPED', { skipped: skipped.join('`, `') }); - if (result.failed.length) msg += '\n\n' + message.format('S_LINKFILTER_FAILED', { failed: result.failed.join('`, `') }); + if (result.failed?.length) msg += '\n\n' + message.format('S_LINKFILTER_FAILED', { failed: result.failed.join('`, `') }); await message.guild._updateSettings({ [this.index]: setting }); return { @@ -127,7 +127,8 @@ module.exports = class LinkFilter extends FilterSetting { } else if (['ignore', 'channelignore', 'ignorechannel'].includes(method)) { - const resolved = await resolver.resolveMethod(args, null, setting.ignore, { + const resolved = await resolver.resolveMethod(args, { + existing: setting.ignore, resolver: resolver.resolveChannels.bind(resolver), guild, allowedMethods: ['add', 'remove', 'set', 'reset'] @@ -142,7 +143,8 @@ module.exports = class LinkFilter extends FilterSetting { } else if (['bypass', 'roleignore', 'ignorerole'].includes(method)) { - const resolved = await resolver.resolveMethod(args, null, setting.bypass, { + const resolved = await resolver.resolveMethod(args, { + existing: setting.bypass, resolver: resolver.resolveRoles.bind(resolver), guild, allowedMethods: ['add', 'remove', 'set', 'reset'] @@ -182,7 +184,7 @@ module.exports = class LinkFilter extends FilterSetting { } else if (['action', 'actions'].includes(method)) { const submethod = args.shift().toLowerCase(); - const resolved = await resolver.resolveMethod([submethod], null, null, { + const resolved = await resolver.resolveMethod([submethod], { allowedMethods: ['add', 'remove', 'edit', 'list', 'reset'] }); if (!resolved) return { error: true, msg: message.format('ERR_INVALID_METHOD', { method: submethod }) }; diff --git a/structure/client/components/settings/moderation/MessageLog.js b/structure/client/components/settings/moderation/MessageLog.js index 37e537d..9036758 100644 --- a/structure/client/components/settings/moderation/MessageLog.js +++ b/structure/client/components/settings/moderation/MessageLog.js @@ -55,7 +55,7 @@ class MessageLogsSetting extends Setting { msg: message.format('MISSING_ARGS'), error: true }; - const response = await this.resolveMethod(args, undefined, setting.ignoredRoles, { resolver: guild.resolveRoles.bind(guild) }); + const response = await this.resolveMethod(args, { existing: setting.ignoredRoles, resolver: guild.resolveRoles.bind(guild) }); if (response) { @@ -102,7 +102,7 @@ class MessageLogsSetting extends Setting { msg: message.format('MISSING_ARGS'), error: true }; - const response = await this.resolveMethod(args, null, setting.ignoredChannels, { resolver: guild.resolveChannels.bind(guild) }); + const response = await this.resolveMethod(args, { existing: setting.ignoredChannels, resolver: guild.resolveChannels.bind(guild) }); if (response) { @@ -143,7 +143,7 @@ class MessageLogsSetting extends Setting { } else if (['attachments', 'images', 'attachment', 'image'].includes(method)) { - if (guild.premium < 2) return { + if (guild.premium < 2 && !message.author.developer) return { msg: message.format('PREMIUM_REQUIRED', { required: 2, tier: guild.premium }), error: true }; @@ -242,8 +242,8 @@ class MessageLogsSetting extends Setting { async fields(guild) { const setting = guild._settings[this.index]; - const roles = setting?.ignoredRoles ? await Promise.all(setting.ignoredRoles.map(async (role) => await guild.resolveRole(role))) : undefined; - const channels = setting?.ignoredChannels ? await Promise.all(setting.ignoredChannels.map(async (c) => await guild.resolveChannel(c))) : undefined; + const roles = setting?.ignoredRoles ? await Promise.all(setting.ignoredRoles.map(async (role) => guild.resolveRole(role))) : undefined; + const channels = setting?.ignoredChannels ? await Promise.all(setting.ignoredChannels.map(async (c) => guild.resolveChannel(c))) : undefined; return [ { name: '》 Status', diff --git a/structure/client/components/settings/moderation/ModerationPoints.js b/structure/client/components/settings/moderation/ModerationPoints.js index abd4550..8e6ba92 100644 --- a/structure/client/components/settings/moderation/ModerationPoints.js +++ b/structure/client/components/settings/moderation/ModerationPoints.js @@ -88,7 +88,7 @@ class ModerationPointsSetting extends Setting { } else if (method === 'associate') { - const { method: submethod, changed } = await resolver.resolveMethod(params, null, Object.keys(setting.associations)); + const { method: submethod, changed } = await resolver.resolveMethod(params, { existing: Object.keys(setting.associations) }); if (submethod === 'reset') { setting.associations = {}; diff --git a/structure/client/components/settings/moderation/Protection.js b/structure/client/components/settings/moderation/Protection.js index 418e473..76236a1 100644 --- a/structure/client/components/settings/moderation/Protection.js +++ b/structure/client/components/settings/moderation/Protection.js @@ -52,7 +52,7 @@ class ProtectionSetting extends Setting { } else { - const response = await resolver.resolveMethod(params, null, setting.roles, { resolver: resolver.resolveRoles.bind(resolver), guild }); + const response = await resolver.resolveMethod(params, { existing: 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') }; diff --git a/structure/client/components/settings/utility/Autorole.js b/structure/client/components/settings/utility/Autorole.js index cf680c5..4b352e3 100644 --- a/structure/client/components/settings/utility/Autorole.js +++ b/structure/client/components/settings/utility/Autorole.js @@ -33,13 +33,14 @@ class AutoroleSetting extends Setting { const setting = message.guild._settings[this.index]; const { guild } = message; const { resolver } = this.client; - const response = await resolver.resolveMethod(params, null, setting.roles, { + const response = await resolver.resolveMethod(params, { + existing: setting.roles, resolver: resolver.resolveRoles.bind(resolver), guild }); - let index = null, - langParams = {}; + let index = null; + const langParams = {}; if (!response) return { error: true, diff --git a/structure/client/components/settings/utility/StickyRole.js b/structure/client/components/settings/utility/StickyRole.js index 7d88ff1..aab6b4f 100644 --- a/structure/client/components/settings/utility/StickyRole.js +++ b/structure/client/components/settings/utility/StickyRole.js @@ -37,7 +37,8 @@ class StickyRoleSetting extends Setting { const langParams = {}; let index = null; - const response = await resolver.resolveMethod(params, null, setting.roles, { + const response = await resolver.resolveMethod(params, { + existing: setting.roles, resolver: resolver.resolveRoles.bind(resolver), guild });