mirror of
https://github.com/danbulant/discord.js
synced 2026-06-05 15:51:31 +00:00
Handle DM messages in cleanContent getter. (#726)
* Handle DM messages in cleanContent getter. Closes #725. * Fix build error, improve handling for user IDs. * Update docblock to be more specific about behaviour. * Handle group DMs in cleanContent. * Regen docs.
This commit is contained in:
parent
063be5cee2
commit
fa18b0c6c9
2 changed files with 9 additions and 3 deletions
File diff suppressed because one or more lines are too long
|
|
@ -209,15 +209,20 @@ class Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The message contents with all mentions replaced by the equivalent text.
|
* The message contents with all mentions replaced by the equivalent text. If mentions cannot be resolved to a name,
|
||||||
|
* the relevant mention in the message content will not be converted.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
get cleanContent() {
|
get cleanContent() {
|
||||||
return this.content
|
return this.content
|
||||||
.replace(/@everyone/g, '@\u200Beveryone')
|
.replace(/@everyone/g, '@\u200Beveryone')
|
||||||
.replace(/@here/g, '@\u200Bhere')
|
.replace(/@here/g, '@\u200Bhere')
|
||||||
.replace(/<@!?[0-9]+>/g, input => {
|
.replace(/<@!?[0-9]+>/g, (input) => {
|
||||||
const id = input.replace(/<|!|>|@/g, '');
|
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);
|
const member = this.channel.guild.members.get(id);
|
||||||
if (member) {
|
if (member) {
|
||||||
if (member.nickname) return `@${member.nickname}`;
|
if (member.nickname) return `@${member.nickname}`;
|
||||||
|
|
@ -234,6 +239,7 @@ class Message {
|
||||||
return input;
|
return input;
|
||||||
})
|
})
|
||||||
.replace(/<@&[0-9]+>/g, (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, ''));
|
const role = this.guild.roles.get(input.replace(/<|@|>|&/g, ''));
|
||||||
if (role) return `@${role.name}`;
|
if (role) return `@${role.name}`;
|
||||||
return input;
|
return input;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue