mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Added cleanContent getter (#644)
* Added cleanContent getter which replaces mentions with the equivalent text. (Majority of code implemented by Gus). * Fixed bug where invalid mentions would cause crash * Fixed not cleaning @everyone and @here * Fixed only detecting first @here or @everyone, resolved to test software (and learn regular expressions) in the future
This commit is contained in:
committed by
Schuyler Cebulskie
parent
7fe032c785
commit
9bef99c75c
@@ -155,6 +155,43 @@ class Message {
|
||||
}
|
||||
|
||||
/**
|
||||
* The message contents with all mentions replaced by the equivalent text.
|
||||
* @type {string}
|
||||
*/
|
||||
get cleanContent() {
|
||||
return this.content.replace(/<@!?[0-9]+>/g, input => {
|
||||
let user = this.channel.guild.members.get(input.replace(/<|!|>|@/g, ''));
|
||||
|
||||
if (user) {
|
||||
if (user.nickname) {
|
||||
return `@${user.nickname}`;
|
||||
}
|
||||
return `@${user.user.username}`;
|
||||
}
|
||||
|
||||
return input;
|
||||
}).replace(/<#[0-9]+>/g, (input) => {
|
||||
let channel = this.client.channels.get(input.replace(/<|#|>/g, ''));
|
||||
|
||||
if (channel) {
|
||||
return `#${channel.name}`;
|
||||
}
|
||||
|
||||
return input;
|
||||
}).replace(/<@&[0-9]+>/g, (input) => {
|
||||
let role = this.guild.roles.get(input.replace(/<|@|>|&/g, ''));
|
||||
|
||||
if (role) {
|
||||
return `@${role.name}`;
|
||||
}
|
||||
|
||||
return input;
|
||||
})
|
||||
.replace(/@everyone/g, '@\u200Beveryone')
|
||||
.replace(/@here/g, '@\u200Bhere');
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of cached versions of the message, including the current version.
|
||||
* Sorted from latest (first) to oldest (last).
|
||||
* @type {Message[]}
|
||||
|
||||
Reference in New Issue
Block a user