| @@ -30,7 +30,7 @@ namespace Discord | |||||
| public IGuild Guild { get; set; } | public IGuild Guild { get; set; } | ||||
| // IMentionable | // IMentionable | ||||
| public string Mention => throw new System.NotImplementedException(); | |||||
| public string Mention => EmoteUtilities.FormatGuildEmote(Id, Name); | |||||
| public Task DeleteAsync() | public Task DeleteAsync() | ||||
| => Discord.Rest.DeleteGuildEmojiAsync(Guild.Id, Id); | => Discord.Rest.DeleteGuildEmojiAsync(Guild.Id, Id); | ||||
| @@ -13,6 +13,6 @@ namespace Discord | |||||
| public ulong Id { get; set; } | public ulong Id { get; set; } | ||||
| public string Name { get; set; } | public string Name { get; set; } | ||||
| public string Mention => throw new System.NotImplementedException(); // TODO: EmojiUtils | |||||
| public string Mention => EmoteUtilities.FormatGuildEmote(Id, Name); | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,27 @@ | |||||
| using System; | |||||
| namespace Discord | |||||
| { | |||||
| public static class EmoteUtilities | |||||
| { | |||||
| public static string FormatGuildEmote(ulong id, string name) | |||||
| => $"<:{name}:{id}>"; | |||||
| public static (ulong, string) ParseGuildEmote(string formatted) | |||||
| { | |||||
| if (formatted.IndexOf('<') != 0 || formatted.IndexOf(':') != 1 || formatted.IndexOf('>') != formatted.Length-1) | |||||
| throw new ArgumentException("passed string does not match a guild emote format", nameof(formatted)); // TODO: grammar | |||||
| int closingIndex = formatted.IndexOf(':', 2); | |||||
| if (closingIndex < 0) | |||||
| throw new ArgumentException("passed string does not match a guild emote format", nameof(formatted)); | |||||
| string name = formatted.Substring(2, closingIndex-2); | |||||
| string idStr = formatted.Substring(closingIndex + 1); | |||||
| idStr = idStr.Substring(0, idStr.Length - 1); // ignore closing > | |||||
| ulong id = ulong.Parse(idStr); // TODO: TryParse here? | |||||
| return (id, name); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,31 @@ | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Text; | |||||
| using Xunit; | |||||
| namespace Discord.Tests.Unit | |||||
| { | |||||
| public class EmoteTests | |||||
| { | |||||
| [Fact] | |||||
| public void Parse() | |||||
| { | |||||
| string input = "<:gopher:243902586946715658>"; | |||||
| var (resultId, resultName) = EmoteUtilities.ParseGuildEmote(input); | |||||
| Assert.Equal(243902586946715658UL, resultId); | |||||
| Assert.Equal("gopher", resultName); | |||||
| Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("foo")); | |||||
| Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<foo")); | |||||
| Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<:foo")); | |||||
| Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<:foo>")); | |||||
| } | |||||
| [Fact] | |||||
| public void Format() | |||||
| { | |||||
| string result = EmoteUtilities.FormatGuildEmote(243902586946715658, "gopher"); | |||||
| Assert.Equal("<:gopher:243902586946715658>", result); | |||||
| } | |||||
| } | |||||
| } | |||||