Fixed unicode emojis in combination with twitch emotes in same message.

edited November 2017 in Developers
Came to my attention last night when I was reading Discord chat about possible issues using Unicode emojis in combination with Twitch emotes EleGiggle / Kappa / WutFace etc in the same chat message that resulted in NOT showing the Twitch emotes properly.

Twitch chat showed this (properly):

My chat client showed it like this:

My bot showed this:

As you can see both my chat client and my bot failed to show both the emojis + Twitch emote combination but it's fixed now which results in this:
The "quick" fix was to replace the emojis Unicode with white space and then parse the rest as normal and after that insert the twitch emote back in the original chat (with the Unicode emojis) before sending it as an alert to stream like this:

I'm sure there are better and more efficient ways to do this so if you know don't hesitate to reply below so we can all learn from it!

After getting some more test cases, the above does not work for all tests so it's back to the drawing table.

Tests (provided by Randomnizer) -->

[update 2]
Instead of a regex approach I'm now using a method to replace surrogate characters. Found the possible solution on stackoverflow ( while looking for utf8 / utf8mb4 related emoji parsing issues.

Basically using this method to fix it:

public static string RemoveSurrogatePairs(string str, string replacementCharacter = "?") { if (str == null) { return null; } StringBuilder sb = null; for (int i = 0; i < str.Length; i++) { char ch = str[i]; if (char.IsSurrogate(ch)) { if (sb == null) { sb = new StringBuilder(str, 0, i, str.Length); } sb.Append(replacementCharacter); // If there is a high+low surrogate, skip the low surrogate if (i + 1 < str.Length && char.IsHighSurrogate(ch) && char.IsLowSurrogate(str[i + 1])) { i++; } } else if (sb != null) { sb.Append(ch); } } return sb == null ? str : sb.ToString(); }

and then calling the method right before i replace the string


Like my (creative) content? Tell your friends, your pets or anyone you know on social media (?), maybe they like the silly stuff posted here too, thanks for being part of the Blushies community! and related sites are supported by people that tipped / subbed @ Twitch channel and / or are a Patreon sub. ♥ Thank you for making Blushies community site real (really silly and stupid xD) ♥

Sign In or Register to comment.