Skip to content

Commit

Permalink
refactor: Remove Util.removeMentions() (#6530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Jan 7, 2022
1 parent 75616a3 commit 0c24cc8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
35 changes: 3 additions & 32 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict';

const { parse } = require('node:path');
const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const fetch = require('node-fetch');
const { Colors, Endpoints } = require('./Constants');
const Options = require('./Options');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const isObject = d => typeof d === 'object' && d !== null;

let deprecationEmittedForRemoveMentions = false;

/**
* Contains various general-purpose utility methods.
*/
Expand Down Expand Up @@ -521,34 +518,8 @@ class Util extends null {
const res = parse(path);
return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0];
}

/**
* 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}
* @deprecated Use {@link BaseMessageOptions#allowedMentions} instead.
*/
static removeMentions(str) {
if (!deprecationEmittedForRemoveMentions) {
process.emitWarning(
'The Util.removeMentions method is deprecated. Use MessageOptions#allowedMentions instead.',
'DeprecationWarning',
);

deprecationEmittedForRemoveMentions = true;
}

return Util._removeMentions(str);
}

static _removeMentions(str) {
return str.replaceAll('@', '@\u200b');
}

/**
* The content to have all mentions replaced by the equivalent text.
* <warn>When {@link Util.removeMentions} is removed, this method will no longer sanitize mentions.
* Use {@link BaseMessageOptions#allowedMentions} instead to prevent mentions when sending a message.</warn>
* @param {string} str The string to be converted
* @param {TextBasedChannels} channel The channel the string was sent in
* @returns {string}
Expand All @@ -559,15 +530,15 @@ class Util extends null {
const id = input.replace(/<|!|>|@/g, '');
if (channel.type === 'DM') {
const user = channel.client.users.cache.get(id);
return user ? Util._removeMentions(`@${user.username}`) : input;
return user ? `@${user.username}` : input;
}

const member = channel.guild.members.cache.get(id);
if (member) {
return Util._removeMentions(`@${member.displayName}`);
return `@${member.displayName}`;
} else {
const user = channel.client.users.cache.get(id);
return user ? Util._removeMentions(`@${user.username}`) : input;
return user ? `@${user.username}` : input;
}
})
.replace(/<#[0-9]+>/g, input => {
Expand Down
3 changes: 0 additions & 3 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2416,9 +2416,6 @@ export class Util extends null {
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
public static basename(path: string, ext?: string): string;
public static cleanContent(str: string, channel: TextBasedChannel): string;
/** @deprecated Use {@link MessageOptions.allowedMentions} to control mentions in a message instead. */
public static removeMentions(str: string): string;
private static _removeMentions(str: string): string;
public static cloneObject(obj: unknown): unknown;
public static discordSort<K, V extends { rawPosition: number; id: Snowflake }>(
collection: Collection<K, V>,
Expand Down

0 comments on commit 0c24cc8

Please sign in to comment.