feat: reimplement disableEveryone into disableMentions

* User input sanitation: reimplement disableEveryone into disableMentions

* Change default value of ClientOptions#disableMentions to 'none'

* Update type declarations of disableMentions to include default

* update for compliance with ESLint

* Overlooked these files. Updated for complete compliance with ESLint
This commit is contained in:
Papa
2020-02-29 06:20:39 -07:00
committed by GitHub
parent bbe169deac
commit 9c8aaf1bbc
8 changed files with 51 additions and 29 deletions

View File

@@ -21,7 +21,8 @@ const browser = exports.browser = typeof window !== 'undefined';
* the message cache lifetime (in seconds, 0 for never)
* @property {boolean} [fetchAllMembers=false] Whether to cache all guild members and users upon startup, as well as
* upon joining a guild (should be avoided whenever possible)
* @property {boolean} [disableMentions=false] Default value for {@link MessageOptions#disableMentions}
* @property {'none' | 'all' | 'everyone'} [disableMentions='none'] Default value
* for {@link MessageOptions#disableMentions}
* @property {PartialType[]} [partials] Structures allowed to be partial. This means events can be emitted even when
* they're missing all the data for a particular structure. See the "Partials" topic listed in the sidebar for some
* important usage information, as partials require you to put checks in place when handling data.
@@ -47,7 +48,7 @@ exports.DefaultOptions = {
messageCacheLifetime: 0,
messageSweepInterval: 0,
fetchAllMembers: false,
disableMentions: false,
disableMentions: 'none',
partials: [],
restWsBridgeTimeout: 5000,
disabledEvents: [],

View File

@@ -534,22 +534,30 @@ class Util {
* @returns {string}
*/
static cleanContent(str, message) {
return Util.removeMentions(str
.replace(/<@!?[0-9]+>/g, input => {
const id = input.replace(/<|!|>|@/g, '');
if (message.channel.type === 'dm') {
const user = message.client.users.cache.get(id);
return user ? `@${user.username}` : input;
}
const member = message.channel.guild.members.cache.get(id);
if (member) {
return `@${member.displayName}`;
if (message.client.options.disableMentions === 'everyone') {
str = str.replace(/@([^<>@ ]*)/gsmu, (match, target) => {
if (target.match(/^[&!]?\d+$/)) {
return `@${target}`;
} else {
const user = message.client.users.cache.get(id);
return user ? `@${user.username}` : input;
return `@\u200b${target}`;
}
})
});
}
str = str.replace(/<@!?[0-9]+>/g, input => {
const id = input.replace(/<|!|>|@/g, '');
if (message.channel.type === 'dm') {
const user = message.client.users.cache.get(id);
return user ? `@${user.username}` : input;
}
const member = message.channel.guild.members.cache.get(id);
if (member) {
return `@${member.displayName}`;
} else {
const user = message.client.users.cache.get(id);
return user ? `@${user.username}` : input;
}
})
.replace(/<#[0-9]+>/g, input => {
const channel = message.client.channels.cache.get(input.replace(/<|#|>/g, ''));
return channel ? `#${channel.name}` : input;
@@ -558,7 +566,12 @@ class Util {
if (message.channel.type === 'dm') return input;
const role = message.guild.roles.cache.get(input.replace(/<|@|>|&/g, ''));
return role ? `@${role.name}` : input;
}));
});
if (message.client.options.disableMentions === 'all') {
return Util.removeMentions(str);
} else {
return str;
}
}
/**