Added support for animated emoji (#913)

* Added support for animated emoji

This was such a useful feature Discord, I'm glad you added this instead
of fixing bugs.

* Fix bugs in emote parser

* Added unit tests for emotes
This commit is contained in:
Christopher F
2017-12-23 15:09:24 -05:00
committed by GitHub
parent 34b4e5a6d2
commit a19ff188e9
8 changed files with 68 additions and 14 deletions

View File

@@ -19,8 +19,8 @@ namespace Discord
=> splashId != null ? $"{DiscordConfig.CDNUrl}splashes/{guildId}/{splashId}.jpg" : null;
public static string GetChannelIconUrl(ulong channelId, string iconId)
=> iconId != null ? $"{DiscordConfig.CDNUrl}channel-icons/{channelId}/{iconId}.jpg" : null;
public static string GetEmojiUrl(ulong emojiId)
=> $"{DiscordConfig.CDNUrl}emojis/{emojiId}.png";
public static string GetEmojiUrl(ulong emojiId, bool animated)
=> $"{DiscordConfig.CDNUrl}emojis/{emojiId}.{(animated ? "gif" : "png")}";
public static string GetRichAssetUrl(ulong appId, string assetId, ushort size, ImageFormat format)
{

View File

@@ -16,13 +16,18 @@ namespace Discord
/// The ID of this emote
/// </summary>
public ulong Id { get; }
/// <summary>
/// Is this emote animated?
/// </summary>
public bool Animated { get; }
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
public string Url => CDN.GetEmojiUrl(Id);
public string Url => CDN.GetEmojiUrl(Id, Animated);
internal Emote(ulong id, string name)
internal Emote(ulong id, string name, bool animated)
{
Id = id;
Name = name;
Animated = animated;
}
public override bool Equals(object other)
@@ -59,17 +64,20 @@ namespace Discord
public static bool TryParse(string text, out Emote result)
{
result = null;
if (text.Length >= 4 && text[0] == '<' && text[1] == ':' && text[text.Length - 1] == '>')
if (text.Length >= 4 && text[0] == '<' && (text[1] == ':' || (text[1] == 'a' && text[2] == ':')) && text[text.Length - 1] == '>')
{
int splitIndex = text.IndexOf(':', 2);
bool animated = text[1] == 'a';
int startIndex = animated ? 3 : 2;
int splitIndex = text.IndexOf(':', startIndex);
if (splitIndex == -1)
return false;
if (!ulong.TryParse(text.Substring(splitIndex + 1, text.Length - splitIndex - 2), NumberStyles.None, CultureInfo.InvariantCulture, out ulong id))
return false;
string name = text.Substring(2, splitIndex - 2);
result = new Emote(id, name);
string name = text.Substring(startIndex, splitIndex - startIndex);
result = new Emote(id, name, animated);
return true;
}
return false;
@@ -77,6 +85,6 @@ namespace Discord
}
private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"<:{Name}:{Id}>";
public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";
}
}

View File

@@ -13,7 +13,7 @@ namespace Discord
public bool RequireColons { get; }
public IReadOnlyList<ulong> RoleIds { get; }
internal GuildEmote(ulong id, string name, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds) : base(id, name)
internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds) : base(id, name, animated)
{
IsManaged = isManaged;
RequireColons = requireColons;
@@ -21,6 +21,6 @@ namespace Discord
}
private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"<:{Name}:{Id}>";
public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";
}
}