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(cleanContent): add slash commands and emojis #9809

Merged
merged 4 commits into from
Nov 5, 2023
Merged
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
56 changes: 32 additions & 24 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,32 +365,40 @@ function basename(path, ext) {
* @returns {string}
*/
function cleanContent(str, channel) {
return str.replaceAll(/<(@[!&]?|#)(\d{17,19})>/g, (match, type, id) => {
switch (type) {
case '@':
case '@!': {
const member = channel.guild?.members.cache.get(id);
if (member) {
return `@${member.displayName}`;
return str.replaceAll(
/* eslint-disable max-len */
/<(?:(?<type>@[!&]?|#)|(?:\/(?<commandName>[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai} ]+):)|(?:a?:(?<emojiName>[\w]+):))(?<id>\d{17,19})>/gu,
(match, type, commandName, emojiName, id) => {
if (commandName) return `/${commandName}`;

if (emojiName) return `:${emojiName}:`;

switch (type) {
case '@':
case '@!': {
const member = channel.guild?.members.cache.get(id);
if (member) {
return `@${member.displayName}`;
}

const user = channel.client.users.cache.get(id);
return user ? `@${user.displayName}` : match;
}
case '@&': {
if (channel.type === ChannelType.DM) return match;
const role = channel.guild.roles.cache.get(id);
return role ? `@${role.name}` : match;
}
case '#': {
const mentionedChannel = channel.client.channels.cache.get(id);
return mentionedChannel ? `#${mentionedChannel.name}` : match;
}
default: {
return match;
}

const user = channel.client.users.cache.get(id);
return user ? `@${user.username}` : match;
}
case '@&': {
if (channel.type === ChannelType.DM) return match;
const role = channel.guild.roles.cache.get(id);
return role ? `@${role.name}` : match;
}
case '#': {
const mentionedChannel = channel.client.channels.cache.get(id);
return mentionedChannel ? `#${mentionedChannel.name}` : match;
}
default: {
return match;
}
}
});
},
);
}

/**
Expand Down