Browse Source

Support the discord:// protocol in buttons (#207)

* Update UrlValidation.cs

* Update ComponentBuilder.cs

* Add docs and better error messages.

* Fix wonky intentation
pull/1923/head
CottageDwellingCat GitHub 3 years ago
parent
commit
7de13bb272
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions
  1. +1
    -1
      src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs
  2. +20
    -1
      src/Discord.Net.Core/Utils/UrlValidation.cs

+ 1
- 1
src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs View File

@@ -596,7 +596,7 @@ namespace Discord
if (string.IsNullOrEmpty(Url)) if (string.IsNullOrEmpty(Url))
throw new InvalidOperationException("Link buttons must have a link associated with them"); throw new InvalidOperationException("Link buttons must have a link associated with them");
else else
UrlValidation.Validate(Url);
UrlValidation.ValidateButton(Url);
} }
else if (string.IsNullOrEmpty(CustomId)) else if (string.IsNullOrEmpty(CustomId))
throw new InvalidOperationException("Non-link buttons must have a custom id associated with them"); throw new InvalidOperationException("Non-link buttons must have a custom id associated with them");


+ 20
- 1
src/Discord.Net.Core/Utils/UrlValidation.cs View File

@@ -6,6 +6,7 @@ namespace Discord.Utils
{ {
/// <summary> /// <summary>
/// Not full URL validation right now. Just ensures protocol is present and that it's either http or https /// Not full URL validation right now. Just ensures protocol is present and that it's either http or https
/// <see cref="ValidateButton(string)"/> should be used for url buttons
/// </summary> /// </summary>
/// <param name="url">url to validate before sending to Discord.</param> /// <param name="url">url to validate before sending to Discord.</param>
/// <exception cref="InvalidOperationException">A URL must include a protocol (http or https).</exception> /// <exception cref="InvalidOperationException">A URL must include a protocol (http or https).</exception>
@@ -15,7 +16,25 @@ namespace Discord.Utils
if (string.IsNullOrEmpty(url)) if (string.IsNullOrEmpty(url))
return false; return false;
if(!(url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))) if(!(url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))))
throw new InvalidOperationException($"Url {url} must be include its protocol (either HTTP or HTTPS)");
throw new InvalidOperationException($"The url {url} must include a protocol (either HTTP or HTTPS)");
return true;
}

/// <summary>
/// Not full URL validation right now. Just Ensures the protocol is either http, https, or discord
/// <see cref="Validate(string)"/> should be used everything other than url buttons
/// </summary>
/// <param name="url">the url to validate before sending to discord</param>
/// <exception cref="InvalidOperationException">A URL must include a protocol (either http, https, or discord).</exception>
/// <returns>true if the url is valid by our standard, false if null, throws an error upon invalid</returns>
public static bool ValidateButton(string url)
{
if (string.IsNullOrEmpty(url))
return false;
if(!((url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) ||
(url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) ||
(url.StartsWith("discord://", StringComparison.OrdinalIgnoreCase))))
throw new InvalidOperationException($"The url {url} must include a protocol (either HTTP, HTTPS, or DISCORD)");
return true; return true;
} }
} }


Loading…
Cancel
Save