feat(Util): make cleanContent take a channel instead of a message (#5535)

This commit is contained in:
Advaith
2021-04-18 09:53:09 -07:00
committed by GitHub
parent 596832371c
commit f1c0c043b5
3 changed files with 12 additions and 12 deletions

View File

@@ -323,7 +323,7 @@ class Message extends Base {
*/ */
get cleanContent() { get cleanContent() {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
return this.content != null ? Util.cleanContent(this.content, this) : null; return this.content != null ? Util.cleanContent(this.content, this.channel) : null;
} }
/** /**

View File

@@ -531,33 +531,33 @@ class Util {
/** /**
* The content to have all mentions replaced by the equivalent text. * The content to have all mentions replaced by the equivalent text.
* @param {string} str The string to be converted * @param {string} str The string to be converted
* @param {Message} message The message object to reference * @param {Channel} channel The channel the string was sent in
* @returns {string} * @returns {string}
*/ */
static cleanContent(str, message) { static cleanContent(str, channel) {
str = str str = str
.replace(/<@!?[0-9]+>/g, input => { .replace(/<@!?[0-9]+>/g, input => {
const id = input.replace(/<|!|>|@/g, ''); const id = input.replace(/<|!|>|@/g, '');
if (message.channel.type === 'dm') { if (channel.type === 'dm') {
const user = message.client.users.cache.get(id); const user = channel.client.users.cache.get(id);
return user ? Util.removeMentions(`@${user.username}`) : input; return user ? Util.removeMentions(`@${user.username}`) : input;
} }
const member = message.channel.guild.members.cache.get(id); const member = channel.guild.members.cache.get(id);
if (member) { if (member) {
return Util.removeMentions(`@${member.displayName}`); return Util.removeMentions(`@${member.displayName}`);
} else { } else {
const user = message.client.users.cache.get(id); const user = channel.client.users.cache.get(id);
return user ? Util.removeMentions(`@${user.username}`) : input; return user ? Util.removeMentions(`@${user.username}`) : input;
} }
}) })
.replace(/<#[0-9]+>/g, input => { .replace(/<#[0-9]+>/g, input => {
const channel = message.client.channels.cache.get(input.replace(/<|#|>/g, '')); const mentionedChannel = channel.client.channels.cache.get(input.replace(/<|#|>/g, ''));
return channel ? `#${channel.name}` : input; return mentionedChannel ? `#${mentionedChannel.name}` : input;
}) })
.replace(/<@&[0-9]+>/g, input => { .replace(/<@&[0-9]+>/g, input => {
if (message.channel.type === 'dm') return input; if (channel.type === 'dm') return input;
const role = message.guild.roles.cache.get(input.replace(/<|@|>|&/g, '')); const role = channel.guild.roles.cache.get(input.replace(/<|@|>|&/g, ''));
return role ? `@${role.name}` : input; return role ? `@${role.name}` : input;
}); });
return str; return str;

2
typings/index.d.ts vendored
View File

@@ -1604,7 +1604,7 @@ declare module 'discord.js' {
export class Util { export class Util {
public static basename(path: string, ext?: string): string; public static basename(path: string, ext?: string): string;
public static binaryToID(num: string): Snowflake; public static binaryToID(num: string): Snowflake;
public static cleanContent(str: string, message: Message): string; public static cleanContent(str: string, channel: Channel): string;
public static removeMentions(str: string): string; public static removeMentions(str: string): string;
public static cloneObject(obj: object): object; public static cloneObject(obj: object): object;
public static delayFor(ms: number): Promise<void>; public static delayFor(ms: number): Promise<void>;