diff --git a/src/Discord.Net.Webhook/DiscordWebhookClient.cs b/src/Discord.Net.Webhook/DiscordWebhookClient.cs
index 143f4f687..1253de0a3 100644
--- a/src/Discord.Net.Webhook/DiscordWebhookClient.cs
+++ b/src/Discord.Net.Webhook/DiscordWebhookClient.cs
@@ -59,30 +59,10 @@ namespace Discord.Webhook
/// Thrown if the is null or whitespace.
public DiscordWebhookClient(string webhookUrl, DiscordRestConfig config) : this(config)
{
- if (string.IsNullOrWhiteSpace(webhookUrl))
- throw new ArgumentNullException(paramName: nameof(webhookUrl), message:
- "The given webhook Url cannot be null or whitespace.");
-
- // thrown when groups are not populated/valid, or when there is no match
- ArgumentException ex(string reason = null)
- => new ArgumentException(paramName: nameof(webhookUrl), message:
- $"The given webhook Url was not in a valid format. {reason}");
- var match = WebhookUrlRegex.Match(webhookUrl);
- if (match != null)
- {
- // ensure that the first group is a ulong, set the _webhookId
- // 0th group is always the entire match, so start at index 1
- if (!(match.Groups[1].Success && ulong.TryParse(match.Groups[1].Value, out _webhookId)))
- throw ex("The webhook Id could not be parsed.");
-
- if (!match.Groups[2].Success)
- throw ex("The webhook token could not be parsed.");
-
- ApiClient.LoginAsync(TokenType.Webhook, match.Groups[2].Value).GetAwaiter().GetResult();
- Webhook = WebhookClientHelper.GetWebhookAsync(this, _webhookId).GetAwaiter().GetResult();
- }
- else
- throw ex();
+ string token;
+ ParseWebhookUrl(webhookUrl, out _webhookId, out token);
+ ApiClient.LoginAsync(TokenType.Webhook, token).GetAwaiter().GetResult();
+ Webhook = WebhookClientHelper.GetWebhookAsync(this, _webhookId).GetAwaiter().GetResult();
}
private DiscordWebhookClient(DiscordRestConfig config)
@@ -136,5 +116,31 @@ namespace Discord.Webhook
{
ApiClient?.Dispose();
}
+
+ internal static void ParseWebhookUrl(string webhookUrl, out ulong webhookId, out string webhookToken)
+ {
+ if (string.IsNullOrWhiteSpace(webhookUrl))
+ throw new ArgumentNullException(paramName: nameof(webhookUrl), message:
+ "The given webhook Url cannot be null or whitespace.");
+
+ // thrown when groups are not populated/valid, or when there is no match
+ ArgumentException ex(string reason = null)
+ => new ArgumentException(paramName: nameof(webhookUrl), message:
+ $"The given webhook Url was not in a valid format. {reason}");
+ var match = WebhookUrlRegex.Match(webhookUrl);
+ if (match != null)
+ {
+ // ensure that the first group is a ulong, set the _webhookId
+ // 0th group is always the entire match, so start at index 1
+ if (!(match.Groups[1].Success && ulong.TryParse(match.Groups[1].Value, out webhookId)))
+ throw ex("The webhook Id could not be parsed.");
+
+ if (!match.Groups[2].Success)
+ throw ex("The webhook token could not be parsed.");
+ webhookToken = match.Groups[2].Value;
+ }
+ else
+ throw ex();
+ }
}
}
diff --git a/test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs b/test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs
index caa9cef8d..039525afc 100644
--- a/test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs
+++ b/test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs
@@ -7,32 +7,30 @@ using Xunit;
namespace Discord
{
///
- /// Tests that the
+ /// Tests the function.
///
public class DiscordWebhookClientTests
{
[Theory]
- [InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
+ [InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
+ 123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// ptb, canary, etc will have slightly different urls
- [InlineData("https://ptb.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
- [InlineData("https://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
+ [InlineData("https://ptb.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
+ 123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
+ [InlineData("https://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
+ 123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// don't care about https
- [InlineData("http://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
+ [InlineData("http://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
+ 123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// this is the minimum that the regex cares about
- [InlineData("discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
- public void TestWebhook_Valid(string webhookurl)
+ [InlineData("discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
+ 123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
+ public void TestWebhook_Valid(string webhookurl, ulong expectedId, string expectedToken)
{
- try
- {
- _ = new DiscordWebhookClient(webhookurl);
- }
- catch (InvalidOperationException)
- {
- // ignore, thrown because webhook urls are invalid
- }
-
- // pass if no exception thrown
- Assert.True(true);
+ DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
+
+ Assert.Equal(expectedId, id);
+ Assert.Equal(expectedToken, token);
}
[Theory]
@@ -43,7 +41,7 @@ namespace Discord
{
Assert.Throws(() =>
{
- _ = new DiscordWebhookClient(webhookurl);
+ DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
});
}
@@ -55,7 +53,7 @@ namespace Discord
{
Assert.Throws(() =>
{
- _ = new DiscordWebhookClient(webhookurl);
+ DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
});
}
}
diff --git a/test/Discord.Net.Tests/Tests.cs b/test/Discord.Net.Tests/Tests.cs
index df156d254..e77f71daf 100644
--- a/test/Discord.Net.Tests/Tests.cs
+++ b/test/Discord.Net.Tests/Tests.cs
@@ -50,4 +50,4 @@ namespace Discord
_guild = fixture._guild;
}
}
-}
\ No newline at end of file
+}