Skip to content

Commit

Permalink
Added support for animated emoji (#913)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
foxbot committed Dec 23, 2017
1 parent 34b4e5a commit a19ff18
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Discord.Net.Core/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static string GetGuildSplashUrl(ulong guildId, string splashId)
=> 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)
{
Expand Down
22 changes: 15 additions & 7 deletions src/Discord.Net.Core/Entities/Emotes/Emote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ public class Emote : IEmote, ISnowflakeEntity
/// 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)
Expand Down Expand Up @@ -59,24 +64,27 @@ public static Emote Parse(string text)
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;

}

private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"<:{Name}:{Id}>";
public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";
}
}
4 changes: 2 additions & 2 deletions src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class GuildEmote : Emote
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;
RoleIds = roleIds;
}

private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"<:{Name}:{Id}>";
public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";
}
}
2 changes: 2 additions & 0 deletions src/Discord.Net.Rest/API/Common/Emoji.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class Emoji
public ulong? Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("animated")]
public bool? Animated { get; set; }
[JsonProperty("roles")]
public ulong[] Roles { get; set; }
[JsonProperty("require_colons")]
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Entities/Messages/RestReaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static RestReaction Create(Model model)
{
IEmote emote;
if (model.Emoji.Id.HasValue)
emote = new Emote(model.Emoji.Id.Value, model.Emoji.Name);
emote = new Emote(model.Emoji.Id.Value, model.Emoji.Name, model.Emoji.Animated.GetValueOrDefault());
else
emote = new Emoji(model.Emoji.Name);
return new RestReaction(emote, model.Count, model.Me);
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class EntityExtensions
{
public static GuildEmote ToEntity(this API.Emoji model)
{
return new GuildEmote(model.Id.Value, model.Name, model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles));
return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles));
}

public static Embed ToEntity(this API.Embed model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static SocketReaction Create(Model model, ISocketMessageChannel channel
{
IEmote emote;
if (model.Emoji.Id.HasValue)
emote = new Emote(model.Emoji.Id.Value, model.Emoji.Name);
emote = new Emote(model.Emoji.Id.Value, model.Emoji.Name, model.Emoji.Animated.GetValueOrDefault());
else
emote = new Emoji(model.Emoji.Name);
return new SocketReaction(channel, model.MessageId, message, model.UserId, user, emote);
Expand Down
44 changes: 44 additions & 0 deletions test/Discord.Net.Tests/Tests.Emotes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Xunit;

namespace Discord
{
public class EmoteTests
{
[Fact]
public void Test_Emote_Parse()
{
Assert.True(Emote.TryParse("<:typingstatus:394207658351263745>", out Emote emote));
Assert.NotNull(emote);
Assert.Equal("typingstatus", emote.Name);
Assert.Equal(394207658351263745UL, emote.Id);
Assert.False(emote.Animated);
Assert.Equal(DateTimeOffset.FromUnixTimeMilliseconds(1514056829775), emote.CreatedAt);
Assert.EndsWith("png", emote.Url);
}
[Fact]
public void Test_Invalid_Emote_Parse()
{
Assert.False(Emote.TryParse("invalid", out _));
Assert.False(Emote.TryParse("<:typingstatus:not_a_number>", out _));
Assert.Throws<ArgumentException>(() => Emote.Parse("invalid"));
}
[Fact]
public void Test_Animated_Emote_Parse()
{
Assert.True(Emote.TryParse("<a:typingstatus:394207658351263745>", out Emote emote));
Assert.NotNull(emote);
Assert.Equal("typingstatus", emote.Name);
Assert.Equal(394207658351263745UL, emote.Id);
Assert.True(emote.Animated);
Assert.Equal(DateTimeOffset.FromUnixTimeMilliseconds(1514056829775), emote.CreatedAt);
Assert.EndsWith("gif", emote.Url);
}
public void Test_Invalid_Amimated_Emote_Parse()
{
Assert.False(Emote.TryParse("<x:typingstatus:394207658351263745>", out _));
Assert.False(Emote.TryParse("<a:typingstatus>", out _));
Assert.False(Emote.TryParse("<a:typingstatus:not_a_number>", out _));
}
}
}

0 comments on commit a19ff18

Please sign in to comment.