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

EmoteExtractor breaks on messages with emoji #259

Closed
Hampo opened this issue Dec 4, 2023 · 3 comments
Closed

EmoteExtractor breaks on messages with emoji #259

Hampo opened this issue Dec 4, 2023 · 3 comments

Comments

@Hampo
Copy link

Hampo commented Dec 4, 2023

Sent the message One 😂 Two Kappa Three in Twitch:
Twitch Chat

Set a breakpoint on the first Emote in the EmoteSet:
Emote

You can see the emote.Name is Kapp instead of Kappa. Best guess is EmoteExtractor not respecting multi-byte characters, like emoji.

@Hampo
Copy link
Author

Hampo commented Dec 4, 2023

Version tested on was 4.0.0-preview-bb8afa27cfa09f9d3c221969879b0adb43809afa

@Hampo
Copy link
Author

Hampo commented Dec 4, 2023

Looking into it some, it appears Twitch uses multibyte encoding (UTF-16 maybe) and those are the indexes provided. The issue is the network traffic is decoded as UTF-8. You can use StringInfo's SubstringByTextElements to correctly parse the string. Example working code to remove emotes from a ChatMessage:

static string RemoveEmotes(ChatMessage msg)
{
    StringBuilder parsed = new(msg.Message.Length);
    StringInfo rawInfo = new(msg.Message);

    int startIndex = 0;
    foreach (Emote emote in msg.EmoteSet.Emotes.OrderBy(x => x.StartIndex))
    {
        parsed.Append(rawInfo.SubstringByTextElements(startIndex, emote.StartIndex - startIndex));
        parsed.Replace("  ", " ");

        startIndex = emote.EndIndex + 1;
    }
    if (startIndex < rawInfo.LengthInTextElements)
    {
        parsed.Append(rawInfo.SubstringByTextElements(startIndex));
        parsed.Replace("  ", " ");
    }

    return parsed.ToString();
}

Hampo added a commit to Hampo/TwitchLib.Client that referenced this issue Dec 4, 2023
@Syzuna
Copy link
Member

Syzuna commented Dec 4, 2023

Fixed #260

@Syzuna Syzuna closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants