Browse Source

feature: add emote formatting/parsing, +tests

pull/1253/head
Christopher Felegy 6 years ago
parent
commit
a1cbd5c688
4 changed files with 60 additions and 2 deletions
  1. +1
    -1
      src/Discord.Net/Models/Emotes/AttachedGuildEmote.cs
  2. +1
    -1
      src/Discord.Net/Models/Emotes/Emote.cs
  3. +27
    -0
      src/Discord.Net/Utilities/EmoteUtilities.cs
  4. +31
    -0
      test/Discord.Tests.Unit/Utilities/EmoteTests.cs

+ 1
- 1
src/Discord.Net/Models/Emotes/AttachedGuildEmote.cs View File

@@ -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);


+ 1
- 1
src/Discord.Net/Models/Emotes/Emote.cs View File

@@ -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);
} }
} }

+ 27
- 0
src/Discord.Net/Utilities/EmoteUtilities.cs View File

@@ -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);
}
}
}

+ 31
- 0
test/Discord.Tests.Unit/Utilities/EmoteTests.cs View File

@@ -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);
}
}
}

Loading…
Cancel
Save