Skip to content

Commit

Permalink
MessageEmote: fixes emote replaced string with a hack until this can …
Browse files Browse the repository at this point in the history
…be re-written to something more sane, adds support for 7tv emotes (#244)

* MessageEmote: fixes emote replaced string with a hack until this can be re-written to something more sane, adds support for 7tv emotes
  • Loading branch information
swiftyspiffy authored Aug 7, 2023
1 parent b2bd5c7 commit 29157f1
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions TwitchLib.Client.Models/MessageEmote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ public class MessageEmote
"//cdn.betterttv.net/emote/{0}/3x"
}
);

/// <summary>
/// Collection of Composite Format Strings which will substitute
/// an emote ID to get a URL for an image from the 7tv CDN
/// </summary>
/// <remarks>
/// These are sorted such that the <see cref="EmoteSize"/> enum can be used as an index,
/// eg SevenTvEmoteUrls[<see cref="EmoteSize.Small"/>]
/// </remarks>
public static readonly ReadOnlyCollection<string> SevenTvEmoteUrls = new ReadOnlyCollection<string>(
new[]
{
"//cdn.7tv.app/emote/{0}/1x.avif",
"//cdn.7tv.app/emote/{0}/2x.avif",
"//cdn.7tv.app/emote/{0}/5x.avif"
}
);
#endregion Third-Party Emote URLs

/// <summary>
Expand All @@ -87,6 +104,7 @@ public static string SourceMatchingReplacementText(MessageEmote caller)
var sizeIndex = (int)caller.Size;
return caller.Source switch
{
EmoteSource.SevenTv => string.Format(SevenTvEmoteUrls[sizeIndex], caller.Id),
EmoteSource.BetterTwitchTv => string.Format(BetterTwitchTvEmoteUrls[sizeIndex], caller.Id),
EmoteSource.FrankerFaceZ => string.Format(FrankerFaceZEmoteUrls[sizeIndex], caller.Id),
EmoteSource.Twitch => string.Format(TwitchEmoteUrls[sizeIndex], caller.Id),
Expand All @@ -104,7 +122,10 @@ public enum EmoteSource
FrankerFaceZ,

/// <summary>Emotes hosted by BetterTTV.net</summary>
BetterTwitchTv
BetterTwitchTv,

/// <summary>Emotes hosted by 7tv</summary>
SevenTv
}

/// <summary> Enum denoting the emote sizes</summary>
Expand Down Expand Up @@ -214,7 +235,7 @@ public MessageEmote(
public class MessageEmoteCollection
{
private readonly Dictionary<string, MessageEmote> _emotes;
private const string BasePattern = @"(\b{0}\b)";
private const string BasePattern = @"(\b {0}\b)|(\b{0} \b)|(?<=\W){0}(?=$)|(?<=\s){0}(?=\s)|(^{0}$)";

/// <summary> Do not access directly! Backing field for <see cref="CurrentPattern"/> </summary>
private string _currentPattern;
Expand Down Expand Up @@ -389,17 +410,25 @@ public string ReplaceEmotes(string originalMessage, EmoteFilterDelegate del = nu

var newMessage = CurrentRegex.Replace(originalMessage, match =>
{
if (!_emotes.ContainsKey(match.Value))
// the match includes possible white space on either side, so we need to preserve that
var emoteCode = match.Value.Trim();
if (match.Value[0] == ' ')
prefix += " ";
if (match.Value[match.Value.Length - 1] == ' ')
suffix = " " + suffix;
if (!_emotes.ContainsKey(emoteCode))
{
return match.Value;
}
var emote = _emotes[match.Value];
var emote = _emotes[emoteCode];
return CurrentEmoteFilter(emote) ? prefix + emote.ReplacementString + suffix : match.Value;
});
CurrentEmoteFilter = _preferredFilter;

return newMessage;
// the hacky replacement logic above will leave 2 spaces between emote and non-emote
// twitch doesn't allow this anyways, so this fix should be fine
return newMessage.Replace(" ", " ");
}

/// <summary>
Expand Down

0 comments on commit 29157f1

Please sign in to comment.