From b71de65afe11f1f9346a6f35211142b331c27ba1 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 28 Apr 2022 18:28:57 +0300 Subject: [PATCH] wordwatcher and filter response tweaks & validation --- .../settings/moderation/WordFilter.js | 41 ++++++++++++----- .../settings/moderation/WordWatcher.js | 44 ++++++++++++++----- 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/src/structure/components/settings/moderation/WordFilter.js b/src/structure/components/settings/moderation/WordFilter.js index c42ce6a..31e104c 100644 --- a/src/structure/components/settings/moderation/WordFilter.js +++ b/src/structure/components/settings/moderation/WordFilter.js @@ -119,9 +119,22 @@ class WordFilterSetting extends FilterSetting { const words = Util.parseQuotes(content).map(([word]) => word), params = [], invalid = []; + let index = 'SETTING_SUCCESS'; - if (list === 'regex') for (const word of words) params.push(Util.sanitiseRegex(word)); - else if (list === 'presets') for (const word of words) { + if (list === 'regex') { + for (const word of words) { + const sanitised = Util.sanitiseRegex(word); + try { + // eslint-disable-next-line require-unicode-regexp, no-new + new RegExp(sanitised); + params.push(sanitised); + } catch (_) { + invalid.push(word); + index = 'SETTING_FILTER_REGEX_FAIL'; + } + } + if (params.length && invalid.length) index += '_SOME'; + } else if (list === 'presets') for (const word of words) { if (FilterPresets.regex[word]) params.push(FilterPresets.regex[word]); else invalid.push(word); } else if (list === 'bypass') params.push(...await guild.resolveRoles(words) @@ -130,12 +143,16 @@ class WordFilterSetting extends FilterSetting { .then((channels) => channels.map((channel) => channel.id))); else params.push(...words); - if (!params.length) return { error: true, index: 'RESOLVE_FAIL', params: { type: list === 'bypass' ? 'roles' : 'channels' } }; + if (!params.length) { + if (['roles', 'channels'].includes(list.value)) + return { error: true, index: 'RESOLVE_FAIL', params: { type: list === 'bypass' ? 'roles' : 'channels' } }; + return { error: true, index }; + } this[method](setting[list], params); - + return { index, params: { updated: list } }; } - return { index: 'SETTING_SUCCESS', params: { updated: list } }; + return { index: 'SETTING_SUCCESS_ALT' }; } @@ -213,32 +230,32 @@ class WordFilterSetting extends FilterSetting { { name: '\u200b', value: '\u200b', inline: true }, { name: 'SETTING_WORDFILTER_EXPLICIT', - value: setting.explicit.length ? '||' + setting.explicit.join(', ') + '||' : '`N/A`', + value: setting.explicit.length ? '||' + setting.explicit.join(', ') + '||' : '**N/A**', inline: true }, { name: 'SETTING_WORDFILTER_FUZZY', - value: setting.fuzzy?.length ? '||' + setting.fuzzy.join(', ') + '||' : '`N/A`', + value: setting.fuzzy?.length ? '||' + setting.fuzzy.join(', ') + '||' : '**N/A**', inline: true }, { name: 'SETTING_WORDFILTER_REGEX', - value: setting.regex?.length ? '`' + setting.regex.join('`\n`') + '`' : '`N/A`', + value: setting.regex?.length ? '`' + setting.regex.join('`\n`') + '`' : '**N/A**', inline: true }, { name: 'SETTING_FILTER_WHITELIST', - value: setting.whitelist.join(', ') || '`N/A`', + value: setting.whitelist.join(', ') || '**N/A**', inline: true }, { name: 'GENERAL_IGNORED', - value: setting.ignore.map((channel) => `<#${channel}>`).join(', ') || '`N/A`', + value: setting.ignore.map((channel) => `<#${channel}>`).join(', ') || '**N/A**', inline: true }, { name: 'GENERAL_BYPASS', - value: setting.bypass.map((role) => `<@&${role}>`).join(', ') || '`N/A`', + value: setting.bypass.map((role) => `<@&${role}>`).join(', ') || '**N/A**', inline: true }, { @@ -254,7 +271,7 @@ class WordFilterSetting extends FilterSetting { : '`' + val.trigger + '`'}`; //result.trigger instanceof Array ? result.trigger.join(', ') : result.trigger acc.push(str); return acc; - }, []).join('\n') || '`N/A`' + }, []).join('\n') || '**N/A**' } ]; } diff --git a/src/structure/components/settings/moderation/WordWatcher.js b/src/structure/components/settings/moderation/WordWatcher.js index 2e773ee..0e5282a 100644 --- a/src/structure/components/settings/moderation/WordWatcher.js +++ b/src/structure/components/settings/moderation/WordWatcher.js @@ -62,7 +62,7 @@ class WordWatcher extends FilterSetting { if (method && list) { if (list.value === 'actions') return this._action(interaction, method.value, setting[list.value], { _wordWatcher: true }); - else if (method.value === 'list') return { error: false, message: setting[list.value].join(', ') }; + else if (method.value === 'list') return { error: false, content: setting[list.value].join(', ') }; const { guild } = interaction; const time = 120; @@ -75,19 +75,35 @@ class WordWatcher extends FilterSetting { const words = Util.parseQuotes(content).map(([word]) => word), params = [], - regex = []; + invalid = []; + let index = 'SETTING_SUCCESS'; - if (list.value === 'regex') for (const word of words) regex.push(Util.sanitiseRegex(word)); - else if (list.value === 'bypass') params.push(...await guild.resolveRoles(words) + if (list.value === 'regex') { + for (const word of words) { + const sanitised = Util.sanitiseRegex(word); + try { // Validating that the regex is valid + // eslint-disable-next-line require-unicode-regexp, no-new + new RegExp(sanitised); + params.push(sanitised); + } catch (_) { + invalid.push(word); + index = 'SETTING_FILTER_REGEX_FAIL'; + } + } + if (params.length && invalid.length) index += '_SOME'; + } else if (list.value === 'bypass') params.push(...await guild.resolveRoles(words) .then((roles) => roles.map((role) => role.id))); else if (list.value === 'ignore') params.push(...await guild.resolveChannels(words) .then((channels) => channels.map((channel) => channel.id))); else params.push(...words); - if (!params.length) - return { error: true, index: 'RESOLVE_FAIL', params: { type: list.value === 'bypass' ? 'roles' : 'channels' } }; - + if (!params.length) { + if (['roles', 'channels'].includes(list.value)) + return { error: true, index: 'RESOLVE_FAIL', params: { type: list.value === 'bypass' ? 'roles' : 'channels' } }; + return { error: true, index }; + } this[method.value](setting[list.value], params); + return { index, params: { updated: list.value, invalid: invalid.join('`, `') } }; } @@ -114,18 +130,18 @@ class WordWatcher extends FilterSetting { }, { name: 'GENERAL_CHANNEL', - value: setting.channel ? `<#${setting.channel}>` : '`N/A`', + value: setting.channel ? `<#${setting.channel}>` : '**N/A**', inline: true }, { name: '\u200b', value: '\u200b', inline: true }, { name: 'GENERAL_IGNORED', - value: setting.ignore.map((channel) => `<#${channel}>`).join(', ') || '`N/A`', + value: setting.ignore.map((channel) => `<#${channel}>`).join(', ') || '**N/A**', inline: true }, { name: 'GENERAL_BYPASS', - value: setting.bypass.map((role) => `<@&${role}>`).join(', ') || '`N/A`', + value: setting.bypass.map((role) => `<@&${role}>`).join(', ') || '**N/A**', inline: true }, { @@ -141,7 +157,13 @@ class WordWatcher extends FilterSetting { : '`' + val.trigger + '`'}`; //result.trigger instanceof Array ? result.trigger.join(', ') : result.trigger acc.push(str); return acc; - }, []).join('\n') || '`N/A`' + }, []).join('\n') || '**N/A**' + }, { + name: 'SETTING_REGEX', + value: setting.regex.length ? `\`${setting.regex.join('`, `')}\`` : '**N/A**' + }, { + name: 'SETTING_WORDS', + value: setting.words.length ? `\`${setting.words.join('`, `')}\`` : '**N/A**' } ]; }