change tokenised to regex in word filter

This commit is contained in:
Erik 2020-11-12 00:59:49 +02:00
parent 42327f6485
commit bb8d5c946c

View File

@ -1,4 +1,5 @@
const { FilterSetting } = require('../../../../interfaces/');
const { Util, FilterPresets } = require('../../../../../util');
// const validInfractions = ['WARN', 'MUTE', 'KICK', 'BAN', 'SOFTBAN'];
module.exports = class WordFilter extends FilterSetting {
@ -16,7 +17,7 @@ module.exports = class WordFilter extends FilterSetting {
'wordfilter reset',
'wordfilter explicit set word "a phrase"',
'wordfilter fuzzy clear',
'wordfilter tokenized add partOfAword',
'wordfilter regex add someth(i|l)ng',
'wordfilter whitelist remove word "a phrase"',
'wordfilter bypass add role "rolename with several words"',
'wordfilter ignore set #channel channel-name',
@ -30,7 +31,7 @@ module.exports = class WordFilter extends FilterSetting {
silent: false,
explicit: [],
fuzzy: [],
tokenized: [],
regex: [],
whitelist: [],
actions: [ // Link certain words to actions with or without points ex fuck: { action: 'MUTE', points: null, force: false, duration: 300 }
// {
@ -39,12 +40,11 @@ module.exports = class WordFilter extends FilterSetting {
// force: true,
// duration: 300,
// prune: true,
// trigger: 'generic' || 'fuzzy' || 'explicit' || 'tokenized' || [words]
// trigger: 'generic' || 'fuzzy' || 'explicit' || 'regex' || [words]
// }
],
ignore: [],
bypass: [],
presets: []
bypass: []
}
}
});
@ -53,8 +53,8 @@ module.exports = class WordFilter extends FilterSetting {
async handle(message, params) {
const [_method, ...args] = params;
let method = _method.toLowerCase();
let [method, ...args] = params;
method = method.toLowerCase();
const { resolver } = this.client;
const { guild } = message;
@ -75,13 +75,13 @@ module.exports = class WordFilter extends FilterSetting {
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: bool }, true);
setting.enabled = bool;
} else if (['explicit', 'fuzzy', 'tokenized', 'token', 'whitelist'].includes(method)) {
} else if (['explicit', 'fuzzy', 'regex', 'whitelist'].includes(method)) {
if (args.length < 2) return {
error: true,
msg: message.format('MISSING_ARGS')
};
if (method === 'token') method = 'tokenized';
if (method === 'regex') args = args.map((arg) => Util.sanitiseRegex(arg));
const resolved = await resolver.resolveMethod(args, {
existing: setting[method],
allowedMethods: ['add', 'remove', 'set', 'reset']
@ -91,7 +91,7 @@ module.exports = class WordFilter extends FilterSetting {
msg: message.format('ERR_INVALID_METHOD', { method: resolved.method || args[0] })
};
setting[method] = resolved.result || [];
index = 'S_WORDFILTER_' + method.toUpperCase() + '_' + resolved.method.toUpperCase();
index = resolved.changed.length ? 'S_WORDFILTER_' + method.toUpperCase() + '_' + resolved.method.toUpperCase() : 'S_NO_CHANGE';
langParams = { changed: resolved.changed.join('`, `') || '' };
} else if (['ignore', 'channelignore', 'ignorechannel'].includes(method)) {
@ -200,7 +200,9 @@ module.exports = class WordFilter extends FilterSetting {
} else if (['preset', 'presets'].includes(method)) {
if (args.length < 2) return {
// TODO: Figure out a system to allow selecting which specific words from the presets
if (args.length < 1) return {
error: true,
msg: message.format('S_WORDFILTER_PRESETMISSING_ARGS')
};
@ -213,10 +215,16 @@ module.exports = class WordFilter extends FilterSetting {
valid: Object.values(valid).reduce((acc, val) => {
acc.push(...val);
return acc;
}, []), allowedMethods: ['add', 'remove', 'set', 'reset']
}, []), allowedMethods: ['add', 'remove', 'reset']
});
const { method: submethod, rest } = resolved;
let { presets } = setting;
if (!resolved) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: resolved.method || args[0] })
};
const { method: submethod, rest = [] } = resolved;
const { regex } = setting;
const _resolved = [];
for (const item of rest) {
@ -225,17 +233,34 @@ module.exports = class WordFilter extends FilterSetting {
else if (valid.obscene.includes(item) && !_resolved.includes('obscene')) _resolved.push('obscene');
}
if (submethod === 'set') presets = _resolved;
else if (submethod === 'reset') presets = [];
else if (submethod === 'add') presets = [
...presets,
..._resolved.filter((item) => !presets.includes(item))
];
else if (submethod === 'remove') presets = presets.filter((item) => !_resolved.includes(item));
if (submethod === 'reset') {
// Remove presets from the regex list
const presets = Object.values(FilterPresets.regex).reduce((acc, val) => {
acc.push(...val);
return acc;
}, []);
presets.forEach((reg) => {
if (regex.includes(reg)) regex.splice(regex.indexOf(reg), 1);
});
} else if (submethod === 'add') {
// Add preset set from the preset list to the regex filter
for (const list of _resolved) {
FilterPresets.regex[list].forEach((reg) => {
if (!regex.includes(reg)) regex.push(reg);
});
}
} else if (submethod === 'remove') {
// Remove a preset set from the regex filter
for (const list of _resolved) {
FilterPresets.regex[list].forEach((reg) => {
if (regex.includes(reg)) regex.splice(regex.indexOf(reg), 1);
});
}
}
index = 'S_FILTER_PRESETS';
langParams.presets = presets.join('`, `') || 'N/A';
setting.presets = presets;
index = `S_FILTER_PRESETS_${submethod.toUpperCase()}`;
langParams.presets = _resolved.join('`, `') || 'N/A';
setting.regex = regex;
}
@ -265,11 +290,11 @@ module.exports = class WordFilter extends FilterSetting {
const { params } = await this._parseArguments(response.content.split(' '), message.guild);
let first = params[0].toLowerCase();
if (['generic', 'gen', 'explicit', 'ex', 'fuzzy', 'fuz', 'tokenized', 'token'].includes(first)) {
if (['generic', 'gen', 'explicit', 'ex', 'fuzzy', 'fuz', 'regex'].includes(first)) {
if (first === 'gen') first = 'generic';
else if (first === 'ex') first = 'explicit';
else if (first === 'fuz') first = 'fuzzy';
else if (first === 'token') first = 'tokenized';
else if (first === 'reg') first = 'regex';
const existing = setting.actions.find((ac) => ac.trigger === first);
if (existing) return {
error: true,
@ -295,6 +320,7 @@ module.exports = class WordFilter extends FilterSetting {
}
async fields(guild) {
const setting = guild._settings[this.index];
const { resolver } = this.client;
@ -310,8 +336,8 @@ module.exports = class WordFilter extends FilterSetting {
inline: true
},
{
name: '》 Preset filters',
value: setting.presets.join(', ') || 'N/A',
name: '\u200b',
value: '\u200b',
inline: true
},
{
@ -325,8 +351,8 @@ module.exports = class WordFilter extends FilterSetting {
inline: true
},
{
name: '》 Tokenized filter',
value: setting.tokenized.length ? '||' + setting.tokenized.join(', ') + '||' : '`N/A`',
name: '》 Regex filter',
value: setting.regex.length ? '`' + setting.regex.join('`\n`') + '`' : '`N/A`',
inline: true
},
{