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

Exclude emojis from escapeMarkdown() #8943

Closed
KevinNovak opened this issue Dec 16, 2022 · 1 comment · Fixed by #8945
Closed

Exclude emojis from escapeMarkdown() #8943

KevinNovak opened this issue Dec 16, 2022 · 1 comment · Fixed by #8945

Comments

@KevinNovak
Copy link
Contributor

KevinNovak commented Dec 16, 2022

Which package is the feature request for?

discord.js

Feature

I want to allow my bot's users to customize part of a message in the bots posts. Per the design, the message can use emojis (including custom emojis), but not markdown (bold, italics, etc).

If I use the escapeMarkdown() method to clean the user input, sometimes an emoji can be escaped as well. This only happens if:

  1. The emoji is a custom emoji
  2. The custom emoji name has a "_" underscore in the name.

Take the following test scenarios:

let testOne =
    'This is a test with emojis <:FrostedWreath:1053399941210443826> and **bold text**.';
let escapedTestOne = escapeMarkdown(testOne);
console.log('Test 1:', escapedTestOne);

let testTwo =
    'This is a test with emojis <:Frosted_Wreath:1053399939654352978> and **bold text**.';
let escapedTestTwo = escapeMarkdown(testTwo);
console.log('Test 2:', escapedTestTwo);

And the resulting output:

Test 1: This is a test with emojis <:FrostedWreath:1053399941210443826> and \*\*bold text\*\*.
Test 2: This is a test with emojis <:Frosted\_Wreath:1053399939654352978> and \*\*bold text\*\*.

In Test 1, the emoji was not escaped. In Test 2 the emoji was escaped because the name contains an underscore.

Side note: Channel mentions, role mentions, user mentions, follow a similar <some_discord_id> syntax as emojis, but these are not a problem with escaping markdown since they don't ever contain markdown characters like "_". Emoji mentions are unique in that they contain a name along with the ID, and that's what causes a problem with the escapeMarkdown() function. Slash command mentions may have a similar issue, I haven't tested that.

Ideal solution or implementation

Ideally escapeMarkdown() would have an option to include/exclude custom emojis from the escaping.

Alternative solutions or implementations

I can workaround this issue by writing a regex to find and remove all escape characters from emoji mentions after they have been escaped with escapeMarkdown().

escapeMarkdown(input).replaceAll(
    /<(a?):(\S+):(\d{17,20})>/g,
    (_match, animatedPrefix, emojiName, emojiId) => {
        let emojiNameUnescaped = emojiName.replaceAll(/\\/g, "");
        return `<${animatedPrefix}:${emojiNameUnescaped}:${emojiId}>`;
    }
);

Other context

Different behaviors after using escapeMarkdown depending on if the emoji name has an underscore in it:
image

@Jiralite
Copy link
Member

This is actually a bug.

@kodiakhq kodiakhq bot closed this as completed in #8945 Dec 25, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants