Browse Source

Added support for animated emoji

This was such a useful feature Discord, I'm glad you added this instead
of fixing bugs.
pull/913/head
Christopher F 7 years ago
parent
commit
5d40e7daa7
7 changed files with 20 additions and 12 deletions
  1. +2
    -2
      src/Discord.Net.Core/CDN.cs
  2. +11
    -5
      src/Discord.Net.Core/Entities/Emotes/Emote.cs
  3. +2
    -2
      src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs
  4. +2
    -0
      src/Discord.Net.Rest/API/Common/Emoji.cs
  5. +1
    -1
      src/Discord.Net.Rest/Entities/Messages/RestReaction.cs
  6. +1
    -1
      src/Discord.Net.Rest/Extensions/EntityExtensions.cs
  7. +1
    -1
      src/Discord.Net.WebSocket/Entities/Messages/SocketReaction.cs

+ 2
- 2
src/Discord.Net.Core/CDN.cs 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")}";

private static string FormatToExtension(ImageFormat format, string imageId)
{


+ 11
- 5
src/Discord.Net.Core/Entities/Emotes/Emote.cs 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,7 +64,7 @@ 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);
if (splitIndex == -1)
@@ -69,7 +74,8 @@ namespace Discord
return false;

string name = text.Substring(2, splitIndex - 2);
result = new Emote(id, name);
bool animated = text[1] == 'a';
result = new Emote(id, name, animated);
return true;
}
return false;
@@ -77,6 +83,6 @@ namespace Discord
}

private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"<:{Name}:{Id}>";
public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";
}
}

+ 2
- 2
src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs 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}>";
}
}

+ 2
- 0
src/Discord.Net.Rest/API/Common/Emoji.cs View File

@@ -9,6 +9,8 @@ namespace Discord.API
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")]


+ 1
- 1
src/Discord.Net.Rest/Entities/Messages/RestReaction.cs View File

@@ -18,7 +18,7 @@ namespace Discord.Rest
{
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);


+ 1
- 1
src/Discord.Net.Rest/Extensions/EntityExtensions.cs View File

@@ -7,7 +7,7 @@ namespace Discord.Rest
{
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)


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Messages/SocketReaction.cs View File

@@ -24,7 +24,7 @@ namespace Discord.WebSocket
{
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);


Loading…
Cancel
Save