From 59d6886c884e7cdd5f9410f9a69119d5cfa2476f Mon Sep 17 00:00:00 2001 From: moiph Date: Wed, 5 Jul 2017 16:48:32 -0700 Subject: [PATCH] Adding IsNullOrUri extension --- src/Discord.Net.Core/Extensions/StringExtensions.cs | 10 ++++++++++ .../Entities/Messages/EmbedBuilder.cs | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/Discord.Net.Core/Extensions/StringExtensions.cs diff --git a/src/Discord.Net.Core/Extensions/StringExtensions.cs b/src/Discord.Net.Core/Extensions/StringExtensions.cs new file mode 100644 index 000000000..2b5891942 --- /dev/null +++ b/src/Discord.Net.Core/Extensions/StringExtensions.cs @@ -0,0 +1,10 @@ +using System; + +namespace Discord +{ + public static class StringExtensions + { + public static bool IsNullOrUri(this string url) => + string.IsNullOrEmpty(url) || Uri.IsWellFormedUriString(url, UriKind.Absolute); + } +} diff --git a/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs index 8cf6ee8fc..7b0285891 100644 --- a/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs @@ -44,7 +44,7 @@ namespace Discord get => _embed.Url; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(Url)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(Url)); _embed.Url = value; } } @@ -53,7 +53,7 @@ namespace Discord get => _embed.Thumbnail?.Url; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(ThumbnailUrl)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(ThumbnailUrl)); _embed.Thumbnail = new EmbedThumbnail(value, null, null, null); } } @@ -62,7 +62,7 @@ namespace Discord get => _embed.Image?.Url; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(ImageUrl)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(ImageUrl)); _embed.Image = new EmbedImage(value, null, null, null); } } @@ -313,7 +313,7 @@ namespace Discord get => _author.Url; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(Url)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(Url)); _author.Url = value; } } @@ -322,7 +322,7 @@ namespace Discord get => _author.IconUrl; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl)); _author.IconUrl = value; } } @@ -372,7 +372,7 @@ namespace Discord get => _footer.IconUrl; set { - if (!string.IsNullOrEmpty(value) && !Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl)); + if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl)); _footer.IconUrl = value; } }