Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,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<void>;
Expand Down