Skip to content

Commit

Permalink
feat(cleanContent): add slash commands and emojis (#9809)
Browse files Browse the repository at this point in the history
* feat(cleanContent): add missing commands and emojis

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

* fix: check for every possible name

* fix: use non capturing group

---------

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 5, 2023
1 parent 31d914e commit c2349d4
Showing 1 changed file with 32 additions and 24 deletions.
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

0 comments on commit c2349d4

Please sign in to comment.