Refactor /highlight (#1702)

* Refactor /highlight

* oops

* v

* apply suggestions
This commit is contained in:
Kris Johnson 2021-01-05 15:14:24 -07:00 committed by GitHub
parent af44ff326a
commit e1ab227783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -792,37 +792,39 @@
case 'highlight':
if (this.checkBroadcast(cmd, text)) return false;
var highlights = Dex.prefs('highlights') || {};
if (target.indexOf(',') > -1) {
var targets = target.match(/([^,]+?({\d*,\d*})?)+/g);
if (target.includes(' ')) {
var targets = target.split(' ');
var subCmd = targets[0];
targets = targets.slice(1).join(' ').match(/([^,]+?({\d*,\d*})?)+/g);
// trim the targets to be safe
for (var i = 0, len = targets.length; i < len; i++) {
targets[i] = targets[i].replace(/\n/g, '').trim();
}
switch (targets[0]) {
switch (subCmd) {
case 'add': case 'roomadd':
var key = targets[0] === 'roomadd' ? (Config.server.id + '#' + this.id) : 'global';
var key = subCmd === 'roomadd' ? (Config.server.id + '#' + this.id) : 'global';
var highlightList = highlights[key] || [];
for (var i = 1, len = targets.length; i < len; i++) {
for (var i = 0, len = targets.length; i < len; i++) {
if (!targets[i]) continue;
if (/[\\^$*+?()|{}[\]]/.test(targets[i])) {
// Catch any errors thrown by newly added regular expressions so they don't break the entire highlight list
try {
new RegExp(targets[i]);
} catch (e) {
return this.add(e.message.substr(0, 28) === 'Invalid regular expression: ' ? e.message : 'Invalid regular expression: /' + targets[i] + '/: ' + e.message);
return this.add('|error|' + (e.message.substr(0, 28) === 'Invalid regular expression: ' ? e.message : 'Invalid regular expression: /' + targets[i] + '/: ' + e.message));
}
}
if (highlightList.indexOf(targets[i]) > -1) {
return this.add(targets[i] + ' is already on your highlights list.');
if (highlightList.includes(targets[i])) {
return this.add('|error|' + targets[i] + ' is already on your highlights list.');
}
}
highlights[key] = highlightList.concat(targets.slice(1));
highlights[key] = highlightList.concat(targets);
this.add("Now highlighting on " + (key === 'global' ? "(everywhere): " : "(in " + key + "): ") + highlights[key].join(', '));
// We update the regex
this.updateHighlightRegExp(highlights);
break;
case 'delete': case 'roomdelete':
var key = targets[0] === 'roomdelete' ? (Config.server.id + '#' + this.id) : 'global';
var key = subCmd === 'roomdelete' ? (Config.server.id + '#' + this.id) : 'global';
var highlightList = highlights[key] || [];
var newHls = [];
for (var i = 0, len = highlightList.length; i < len; i++) {
@ -838,28 +840,35 @@
default:
if (this.checkBroadcast(cmd, text)) return false;
// Wrong command
this.add('Error: Invalid /highlight command.');
this.add('|error|Invalid /highlight command.');
this.parseCommand('/help highlight'); // show help
return false;
}
Storage.prefs('highlights', highlights);
} else {
if (this.checkBroadcast(cmd, text)) return false;
if (target === 'delete') {
Storage.prefs('highlights', false);
this.updateHighlightRegExp({});
this.add("All highlights cleared");
if (['clear', 'roomclear', 'clearall'].includes(target)) {
var key = (target === 'roomclear' ? (Config.server.id + '#' + this.id) : (target === 'clearall' ? '' : 'global'));
if (key) {
highlights[key] = [];
this.add("All highlights (" + (key === 'global' ? "everywhere" : "in " + key) + ") cleared.");
this.updateHighlightRegExp(highlightList);
} else {
Storage.prefs('highlights', false);
this.add("All highlights (in all rooms and globally) cleared.");
this.updateHighlightRegExp({});
}
} else if (['show', 'list', 'roomshow', 'roomlist'].includes(target)) {
// Shows a list of the current highlighting words
var key = target.startsWith('room') ? (Config.server.id + '#' + this.id) : 'global';
if (highlights[key] && highlights[key].length > 0) {
this.add("Current highlight list " + (key === 'global' ? "(everywhere): " : "(in " + key + "): ") + highlights[key].join(", "));
} else {
this.add('Your highlight list' + (key === 'global' ? '' : ' in ' + this.id) + ' is empty.');
this.add('Your highlight list' + (key === 'global' ? '' : ' in ' + key) + ' is empty.');
}
} else {
// Wrong command
this.add('Error: Invalid /highlight command.');
this.add('|error|Invalid /highlight command.');
this.parseCommand('/help highlight'); // show help
return false;
}
@ -1118,13 +1127,15 @@
case 'highlight':
case 'hl':
this.add('Set up highlights:');
this.add('/highlight add, [word] - Add the word [word] to the highlight list.');
this.add('/highlight roomadd, [word] - Add the word [word] to the highlight list of whichever room you used the command in.');
this.add('/highlight add [word 1], [word 2], [...] - Add the provided list of words to your highlight list.');
this.add('/highlight roomadd [word 1], [word 2], [...] - Add the provided list of words to the highlight list of whichever room you used the command in.');
this.add('/highlight list - List all words that currently highlight you.');
this.add('/highlight roomlist - List all words that currently highlight you in whichever room you used the command in.');
this.add('/highlight delete, [word] - Delete the word [word] from your entire highlight list.');
this.add('/highlight roomdelete, [word] - Delete the word [word] from the highlight list of whichever room you used the command in.');
this.add('/highlight delete - Clear the highlight list.');
this.add('/highlight delete [word 1], [word 2], [...] - Delete the provided list of words from your entire highlight list.');
this.add('/highlight roomdelete [word 1], [word 2], [...] - Delete the provided list of words from the highlight list of whichever room you used the command in.');
this.add('/highlight clear - Clear your global highlight list.');
this.add('/highlight roomclear - Clear the highlight list of whichever room you used the command in.');
this.add('/highlight clearall - Clear your entire highlight list (all rooms and globally).');
return false;
case 'rank':
case 'ranking':