From f1c0c043b516f4158ab9d473419e3b5e125a4c03 Mon Sep 17 00:00:00 2001 From: Advaith Date: Sun, 18 Apr 2021 09:53:09 -0700 Subject: [PATCH] feat(Util): make `cleanContent` take a channel instead of a message (#5535) --- src/structures/Message.js | 2 +- src/util/Util.js | 20 ++++++++++---------- typings/index.d.ts | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 867233611..954ddab39 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -323,7 +323,7 @@ class Message extends Base { */ get cleanContent() { // 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; } /** diff --git a/src/util/Util.js b/src/util/Util.js index 8d6548668..9ece9356f 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -531,33 +531,33 @@ class Util { /** * The content to have all mentions replaced by the equivalent text. * @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} */ - static cleanContent(str, message) { + static cleanContent(str, channel) { 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); + if (channel.type === 'dm') { + const user = channel.client.users.cache.get(id); 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) { return Util.removeMentions(`@${member.displayName}`); } else { - const user = message.client.users.cache.get(id); + const user = channel.client.users.cache.get(id); return user ? Util.removeMentions(`@${user.username}`) : input; } }) .replace(/<#[0-9]+>/g, input => { - const channel = message.client.channels.cache.get(input.replace(/<|#|>/g, '')); - return channel ? `#${channel.name}` : input; + const mentionedChannel = channel.client.channels.cache.get(input.replace(/<|#|>/g, '')); + return mentionedChannel ? `#${mentionedChannel.name}` : input; }) .replace(/<@&[0-9]+>/g, input => { - if (message.channel.type === 'dm') return input; - const role = message.guild.roles.cache.get(input.replace(/<|@|>|&/g, '')); + if (channel.type === 'dm') return input; + const role = channel.guild.roles.cache.get(input.replace(/<|@|>|&/g, '')); return role ? `@${role.name}` : input; }); return str; diff --git a/typings/index.d.ts b/typings/index.d.ts index e98a8af31..c88c09dbf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1604,7 +1604,7 @@ declare module 'discord.js' { export class Util { public static basename(path: string, ext?: string): string; 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 cloneObject(obj: object): object; public static delayFor(ms: number): Promise;