1- using System . Text ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Globalization ;
4+ using System . Linq ;
5+ using System . Text ;
26using System . Text . RegularExpressions ;
37using DSharpPlus . Entities ;
48using Kattbot . Common . Models . Emotes ;
@@ -7,11 +11,12 @@ namespace Kattbot.Helpers;
711
812public static class EmoteHelper
913{
10- public static readonly Regex EmoteRegex = new ( @"<a{0,1}:\w+:\d+>" ) ;
11- public static readonly Regex EmoteRegexGrouped = new ( @"<(a{0,1}):(\w+):(\d+)>" ) ;
12- public static readonly string EmoteCodeFormat = "<:{0}:{1}>" ;
13- public static readonly string EmoteAnimatedCodeFormat = "<a:{0}:{1}>" ;
14- public static readonly string EmoteNamePlaceholder = "x" ;
14+ private const string EmoteCodeFormat = "<:{0}:{1}>" ;
15+ private const string EmoteAnimatedCodeFormat = "<a:{0}:{1}>" ;
16+ private const string EmoteNamePlaceholder = "x" ;
17+
18+ private static readonly Regex EmoteRegex = new ( @"<a{0,1}:\w+:\d+>" ) ;
19+ private static readonly Regex EmoteRegexGrouped = new ( @"<(a{0,1}):(\w+):(\d+)>" ) ;
1520
1621 public static TempEmote ? Parse ( string emoteString )
1722 {
@@ -32,6 +37,7 @@ public static class EmoteHelper
3237 Id = id ,
3338 Name = name ,
3439 Animated = isAnimated ,
40+ ImageUrl = BuildDiscordEmojiUrl ( id , isAnimated ) ,
3541 } ;
3642
3743 return parsed ;
@@ -74,7 +80,7 @@ public static string GetExternalEmojiImageUrl(string code)
7480
7581 // flag = 0001F1E6 0001F1E9
7682 // https://emoji.aranja.com/static/emoji-data/img-twitter-72/1f1e6-1f1e9.png
77- var utf32Encoding = new UTF32Encoding ( true , false ) ;
83+ var utf32Encoding = new UTF32Encoding ( bigEndian : true , byteOrderMark : false ) ;
7884
7985 byte [ ] bytes = utf32Encoding . GetBytes ( code ) ;
8086
@@ -90,7 +96,7 @@ public static string GetExternalEmojiImageUrl(string code)
9096
9197 for ( var i = 0 ; i < bytesAsString . Length ; i += 8 )
9298 {
93- string unicodePart = bytesAsString . Substring ( i , 8 )
99+ string unicodePart = bytesAsString . Substring ( i , length : 8 )
94100 . TrimStart ( '0' )
95101 . ToLower ( ) ;
96102
@@ -101,4 +107,22 @@ public static string GetExternalEmojiImageUrl(string code)
101107
102108 return $ "https://emoji.aranja.com/static/emoji-data/img-twitter-72/{ fileName } .png";
103109 }
110+
111+ public static List < string > ExtractEmotesFromMessage ( string messageText )
112+ {
113+ MatchCollection result = EmoteRegex . Matches ( messageText ) ;
114+
115+ List < string > emojiStrings = result . Select ( m => m . Value ) . ToList ( ) ;
116+
117+ return emojiStrings ;
118+ }
119+
120+ private static string BuildDiscordEmojiUrl ( ulong id , bool isAnimated )
121+ {
122+ return id == 0
123+ ? throw new InvalidOperationException ( "Cannot get URL of unicode emojis." )
124+ : isAnimated
125+ ? $ "https://cdn.discordapp.com/emojis/{ id . ToString ( CultureInfo . InvariantCulture ) } .gif"
126+ : $ "https://cdn.discordapp.com/emojis/{ id . ToString ( CultureInfo . InvariantCulture ) } .png";
127+ }
104128}
0 commit comments