From f3d7f7c3bd662d84cd9fb1780ebebeafa91a2b64 Mon Sep 17 00:00:00 2001 From: Isabella Date: Thu, 9 Aug 2018 07:27:05 -0500 Subject: [PATCH] refactor: move Message#cleanContent function to Util (#2703) * refactor: move Message#cleanContent function to Util * suggested changes --- src/structures/Message.js | 30 +----------------------------- src/util/Util.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 4307d2244..7137b6266 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -257,35 +257,7 @@ class Message extends Base { * @readonly */ get cleanContent() { - return this.content - .replace(/@(everyone|here)/g, '@\u200b$1') - .replace(/<@!?[0-9]+>/g, input => { - const id = input.replace(/<|!|>|@/g, ''); - if (this.channel.type === 'dm' || this.channel.type === 'group') { - return this.client.users.has(id) ? `@${this.client.users.get(id).username}` : input; - } - - const member = this.channel.guild.members.get(id); - if (member) { - if (member.nickname) return `@${member.nickname}`; - return `@${member.user.username}`; - } else { - const user = this.client.users.get(id); - if (user) return `@${user.username}`; - return input; - } - }) - .replace(/<#[0-9]+>/g, input => { - const channel = this.client.channels.get(input.replace(/<|#|>/g, '')); - if (channel) return `#${channel.name}`; - return input; - }) - .replace(/<@&[0-9]+>/g, input => { - if (this.channel.type === 'dm' || this.channel.type === 'group') return input; - const role = this.guild.roles.get(input.replace(/<|@|>|&/g, '')); - if (role) return `@${role.name}`; - return input; - }); + return Util.cleanContent(this.content, this); } /** diff --git a/src/util/Util.js b/src/util/Util.js index ea99f5328..154903486 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -382,6 +382,41 @@ class Util { return dec; } + /** + * 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 + * @returns {string} + */ + static cleanContent(str, message) { + return str + .replace(/@(everyone|here)/g, '@\u200b$1') + .replace(/<@!?[0-9]+>/g, input => { + const id = input.replace(/<|!|>|@/g, ''); + if (message.channel.type === 'dm' || message.channel.type === 'group') { + const user = message.client.users.get(id); + return user ? `@${user.username}` : input; + } + + const member = message.channel.guild.members.get(id); + if (member) { + return member.displayName; + } else { + const user = message.client.users.get(id); + return user ? `@${user.username}` : input; + } + }) + .replace(/<#[0-9]+>/g, input => { + const channel = message.client.channels.get(input.replace(/<|#|>/g, '')); + return channel ? `#${channel.name}` : input; + }) + .replace(/<@&[0-9]+>/g, input => { + if (message.channel.type === 'dm' || message.channel.type === 'group') return input; + const role = message.guild.roles.get(input.replace(/<|@|>|&/g, '')); + return role ? `@${role.name}` : input; + }); + } + /** * Creates a Promise that resolves after a specified duration. * @param {number} ms How long to wait before resolving (in milliseconds)