Skip to content

Commit a1cbd5c

Browse files
committed
feature: add emote formatting/parsing, +tests
1 parent a138f5d commit a1cbd5c

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/Discord.Net/Models/Emotes/AttachedGuildEmote.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public AttachedGuildEmote(Model model, IGuild guild, IDiscordClient discord) : b
3030
public IGuild Guild { get; set; }
3131

3232
// IMentionable
33-
public string Mention => throw new System.NotImplementedException();
33+
public string Mention => EmoteUtilities.FormatGuildEmote(Id, Name);
3434

3535
public Task DeleteAsync()
3636
=> Discord.Rest.DeleteGuildEmojiAsync(Guild.Id, Id);

src/Discord.Net/Models/Emotes/Emote.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public Emote(ulong id, string name)
1313
public ulong Id { get; set; }
1414
public string Name { get; set; }
1515

16-
public string Mention => throw new System.NotImplementedException(); // TODO: EmojiUtils
16+
public string Mention => EmoteUtilities.FormatGuildEmote(Id, Name);
1717
}
1818
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Discord
4+
{
5+
public static class EmoteUtilities
6+
{
7+
public static string FormatGuildEmote(ulong id, string name)
8+
=> $"<:{name}:{id}>";
9+
10+
public static (ulong, string) ParseGuildEmote(string formatted)
11+
{
12+
if (formatted.IndexOf('<') != 0 || formatted.IndexOf(':') != 1 || formatted.IndexOf('>') != formatted.Length-1)
13+
throw new ArgumentException("passed string does not match a guild emote format", nameof(formatted)); // TODO: grammar
14+
15+
int closingIndex = formatted.IndexOf(':', 2);
16+
if (closingIndex < 0)
17+
throw new ArgumentException("passed string does not match a guild emote format", nameof(formatted));
18+
19+
string name = formatted.Substring(2, closingIndex-2);
20+
string idStr = formatted.Substring(closingIndex + 1);
21+
idStr = idStr.Substring(0, idStr.Length - 1); // ignore closing >
22+
ulong id = ulong.Parse(idStr); // TODO: TryParse here?
23+
24+
return (id, name);
25+
}
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Xunit;
5+
6+
namespace Discord.Tests.Unit
7+
{
8+
public class EmoteTests
9+
{
10+
[Fact]
11+
public void Parse()
12+
{
13+
string input = "<:gopher:243902586946715658>";
14+
var (resultId, resultName) = EmoteUtilities.ParseGuildEmote(input);
15+
Assert.Equal(243902586946715658UL, resultId);
16+
Assert.Equal("gopher", resultName);
17+
18+
Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("foo"));
19+
Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<foo"));
20+
Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<:foo"));
21+
Assert.Throws<ArgumentException>(() => EmoteUtilities.ParseGuildEmote("<:foo>"));
22+
}
23+
24+
[Fact]
25+
public void Format()
26+
{
27+
string result = EmoteUtilities.FormatGuildEmote(243902586946715658, "gopher");
28+
Assert.Equal("<:gopher:243902586946715658>", result);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)