feat: replace disableEveryone with disableMentions (#3830)

* add ClientOptions#disableMentions and MessageOptions#disableMentions

* provide tests

* don't sanitize controlled mentions

* add @here mentions to tests

* fix indents (6 spaces instead of 8)

* add Util#cleanContent tests

* add typings for removeMentions

* replace @ with @\u200b AFTER cleaning content as suggested instead of using removeMentions

* better explanation of this option

* no newline in Util.removeMentions

* fix long line

* remove double space

* remove comments (change has been reverted)

* Use Util.removeMentions to remove mentions

* use Util.removeMentions in Util.cleanContent
This commit is contained in:
Timo
2020-02-26 12:13:23 +01:00
committed by GitHub
parent c4bda746c8
commit 9cb306c823
8 changed files with 77 additions and 21 deletions

View File

@@ -518,6 +518,15 @@ class Util {
return dec;
}
/**
* Breaks user, role and everyone/here mentions by adding a zero width space after every @ character
* @param {string} str The string to sanitize
* @returns {string}
*/
static removeMentions(str) {
return str.replace(/@/g, '@\u200b');
}
/**
* The content to have all mentions replaced by the equivalent text.
* @param {string} str The string to be converted
@@ -525,8 +534,7 @@ class Util {
* @returns {string}
*/
static cleanContent(str, message) {
return str
.replace(/@(everyone|here)/g, '@\u200b$1')
return Util.removeMentions(str
.replace(/<@!?[0-9]+>/g, input => {
const id = input.replace(/<|!|>|@/g, '');
if (message.channel.type === 'dm') {
@@ -550,7 +558,7 @@ class Util {
if (message.channel.type === 'dm') return input;
const role = message.guild.roles.cache.get(input.replace(/<|@|>|&/g, ''));
return role ? `@${role.name}` : input;
});
}));
}
/**