diff --git a/src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs b/src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs index e4931a9d..999fb357 100644 --- a/src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs +++ b/src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs @@ -1,5 +1,4 @@ using System; -using System.Xml.Linq; namespace Discord; diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOption.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOption.cs index 9ef18028..29986ade 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOption.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOption.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -147,29 +146,20 @@ namespace Discord private static void EnsureValidOptionName(string name) { - if (name == null) - throw new ArgumentNullException(nameof(name), $"{nameof(Name)} cannot be null."); - - if (name.Length > 32) - throw new ArgumentOutOfRangeException(nameof(name), "Name length must be less than or equal to 32."); + Preconditions.NotNull(name, nameof(Name)); + Preconditions.AtMost(name.Length, 32, nameof(Name)); if (!Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); if (name.Any(char.IsUpper)) - throw new FormatException("Name cannot contain any uppercase characters."); + throw new FormatException($"Name cannot contain any uppercase characters. Value: \"{name}\""); } private static void EnsureValidOptionDescription(string description) { - switch (description.Length) - { - case > 100: - throw new ArgumentOutOfRangeException(nameof(description), - "Description length must be less than or equal to 100."); - case 0: - throw new ArgumentOutOfRangeException(nameof(description), "Description length must at least 1."); - } + Preconditions.AtLeast(description.Length, 1, nameof(Description)); + Preconditions.AtMost(description.Length, 100, nameof(Description)); } } } diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOptionChoice.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOptionChoice.cs index 2289b412..36e5bf39 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOptionChoice.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandOptionChoice.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; using System.Text.RegularExpressions; namespace Discord @@ -21,12 +19,12 @@ namespace Discord public string Name { get => _name; - set => _name = value?.Length switch + set { - > 100 => throw new ArgumentOutOfRangeException(nameof(value), "Name length must be less than or equal to 100."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Name length must at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Name)); + Preconditions.AtMost(value.Length, 100, nameof(Name)); + _name = value; + } } /// @@ -41,7 +39,7 @@ namespace Discord set { if (value != null && value is not string && !value.IsNumericType()) - throw new ArgumentException("The value of a choice must be a string or a numeric type!"); + throw new ArgumentException($"The value of a choice must be a string or a numeric type! Value: \"{value}\""); _value = value; } } @@ -60,16 +58,10 @@ namespace Discord foreach (var (locale, name) in value) { if (!Regex.IsMatch(locale, @"^\w{2}(?:-\w{2})?$")) - throw new ArgumentException("Key values of the dictionary must be valid language codes."); + throw new ArgumentException($"Key values of the dictionary must be valid language codes. Locale: \"{locale}\""); - switch (name.Length) - { - case > 100: - throw new ArgumentOutOfRangeException(nameof(value), - "Name length must be less than or equal to 100."); - case 0: - throw new ArgumentOutOfRangeException(nameof(value), "Name length must at least 1."); - } + Preconditions.AtLeast(name.Length, 1, nameof(name), msg: $"Name value of locale {locale} cannot be empty."); + Preconditions.AtMost(name.Length, 100, nameof(name), msg: $"Name value of locale {locale} have to contains 100 chars at most."); } } diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandProperties.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandProperties.cs index 91661697..65e291f7 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandProperties.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommands/ApplicationCommandProperties.cs @@ -46,7 +46,7 @@ namespace Discord Preconditions.AtMost(name.Length, SlashCommandBuilder.MaxNameLength, nameof(name)); if (Type == ApplicationCommandType.Slash && !Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); } } diff --git a/src/Discord.Net.Core/Entities/Interactions/Autocomplete/AutocompleteResult.cs b/src/Discord.Net.Core/Entities/Interactions/Autocomplete/AutocompleteResult.cs index 6b28a84f..6eb0493e 100644 --- a/src/Discord.Net.Core/Entities/Interactions/Autocomplete/AutocompleteResult.cs +++ b/src/Discord.Net.Core/Entities/Interactions/Autocomplete/AutocompleteResult.cs @@ -23,14 +23,10 @@ namespace Discord get => _name; set { - if (value == null) - throw new ArgumentNullException(nameof(value), $"{nameof(Name)} cannot be null."); - _name = value.Length switch - { - > 100 => throw new ArgumentOutOfRangeException(nameof(value), "Name length must be less than or equal to 100."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Name length must be at least 1."), - _ => value - }; + Preconditions.NotNull(value, nameof(Name)); + Preconditions.AtLeast(value.Length, 1, nameof(Name)); + Preconditions.AtMost(value.Length, 100, nameof(Name)); + _name = value; } } @@ -48,7 +44,7 @@ namespace Discord set { if (value is not string && !value.IsNumericType()) - throw new ArgumentException($"{nameof(value)} must be a numeric type or a string!"); + throw new ArgumentException($"{nameof(value)} must be a numeric type or a string! Value: \"{value}\""); _value = value; } diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ButtonBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ButtonBuilder.cs index 4522eb06..f72a3a5b 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ButtonBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ButtonBuilder.cs @@ -22,12 +22,12 @@ public class ButtonBuilder public string Label { get => _label; - set => _label = value?.Length switch + set { - > MaxButtonLabelLength => throw new ArgumentOutOfRangeException(nameof(value), $"Label length must be less or equal to {MaxButtonLabelLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Label length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Label)); + Preconditions.AtMost(value.Length, MaxButtonLabelLength, nameof(Label)); + _label = value; + } } /// @@ -38,12 +38,12 @@ public class ButtonBuilder public string CustomId { get => _customId; - set => _customId = value?.Length switch + set { - > ComponentBuilder.MaxCustomIdLength => throw new ArgumentOutOfRangeException(nameof(value), $"Custom Id length must be less or equal to {ComponentBuilder.MaxCustomIdLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Custom Id length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(CustomId)); + Preconditions.AtMost(value.Length, ComponentBuilder.MaxCustomIdLength, nameof(CustomId)); + _customId = value; + } } /// @@ -294,7 +294,6 @@ public class ButtonBuilder throw new InvalidOperationException("A button must have an Emote or a label!"); if (string.IsNullOrWhiteSpace(CustomId)) throw new InvalidOperationException("Non-link and non-premium buttons must have a custom id associated with them"); - } break; diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuBuilder.cs index 000a1c62..95a937d7 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuBuilder.cs @@ -33,12 +33,12 @@ public class SelectMenuBuilder public string CustomId { get => _customId; - set => _customId = value?.Length switch + set { - > ComponentBuilder.MaxCustomIdLength => throw new ArgumentOutOfRangeException(nameof(value), $"Custom Id length must be less or equal to {ComponentBuilder.MaxCustomIdLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Custom Id length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(CustomId)); + Preconditions.AtMost(value.Length, ComponentBuilder.MaxCustomIdLength, nameof(CustomId)); + _customId = value; + } } /// @@ -61,12 +61,12 @@ public class SelectMenuBuilder public string Placeholder { get => _placeholder; - set => _placeholder = value?.Length switch + set { - > MaxPlaceholderLength => throw new ArgumentOutOfRangeException(nameof(value), $"Placeholder length must be less or equal to {MaxPlaceholderLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Placeholder length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Placeholder)); + Preconditions.AtMost(value.Length, MaxPlaceholderLength, nameof(Placeholder)); + _placeholder = value; + } } /// @@ -109,7 +109,6 @@ public class SelectMenuBuilder { if (value != null) Preconditions.AtMost(value.Count, MaxOptionCount, nameof(Options)); - _options = value; } } diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuOptionBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuOptionBuilder.cs index 6e5b87bf..cd62cea9 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuOptionBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/SelectMenuOptionBuilder.cs @@ -30,12 +30,12 @@ public class SelectMenuOptionBuilder public string Label { get => _label; - set => _label = value?.Length switch + set { - > MaxSelectLabelLength => throw new ArgumentOutOfRangeException(nameof(value), $"Label length must be less or equal to {MaxSelectLabelLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Label length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Label)); + Preconditions.AtMost(value.Length, MaxSelectLabelLength, nameof(Label)); + _label = value; + } } /// @@ -46,12 +46,12 @@ public class SelectMenuOptionBuilder public string Value { get => _value; - set => _value = value?.Length switch + set { - > MaxSelectValueLength => throw new ArgumentOutOfRangeException(nameof(value), $"Value length must be less or equal to {MaxSelectValueLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Value length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Value)); + Preconditions.AtMost(value.Length, MaxSelectValueLength, nameof(Value)); + _value = value; + } } /// @@ -62,12 +62,12 @@ public class SelectMenuOptionBuilder public string Description { get => _description; - set => _description = value?.Length switch + set { - > MaxDescriptionLength => throw new ArgumentOutOfRangeException(nameof(value), $"Description length must be less or equal to {MaxDescriptionLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Description length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(Description)); + Preconditions.AtMost(value.Length, MaxDescriptionLength, nameof(Description)); + _description = value; + } } /// diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/TextInputBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/TextInputBuilder.cs index b675980b..c37c39cb 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/TextInputBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/TextInputBuilder.cs @@ -22,12 +22,12 @@ public class TextInputBuilder public string CustomId { get => _customId; - set => _customId = value?.Length switch + set { - > ComponentBuilder.MaxCustomIdLength => throw new ArgumentOutOfRangeException(nameof(value), $"Custom Id length must be less or equal to {ComponentBuilder.MaxCustomIdLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Custom Id length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(CustomId)); + Preconditions.AtMost(value.Length, ComponentBuilder.MaxCustomIdLength, nameof(CustomId)); + _customId = value; + } } /// @@ -49,7 +49,7 @@ public class TextInputBuilder get => _placeholder; set => _placeholder = (value?.Length ?? 0) <= MaxPlaceholderLength ? value - : throw new ArgumentException($"Placeholder cannot have more than {MaxPlaceholderLength} characters."); + : throw new ArgumentException($"Placeholder cannot have more than {MaxPlaceholderLength} characters. Value: \"{value}\""); } /// @@ -115,9 +115,9 @@ public class TextInputBuilder set { if (value?.Length > (MaxLength ?? LargestMaxLength)) - throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be longer than {MaxLength ?? LargestMaxLength}."); + throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be longer than {MaxLength ?? LargestMaxLength}. Value: \"{value}\""); if (value?.Length < (MinLength ?? 0)) - throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}"); + throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}. Value: \"{value}\""); _value = value; } diff --git a/src/Discord.Net.Core/Entities/Interactions/Modals/ModalBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/Modals/ModalBuilder.cs index c534e9f3..242c316a 100644 --- a/src/Discord.Net.Core/Entities/Interactions/Modals/ModalBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/Modals/ModalBuilder.cs @@ -38,12 +38,12 @@ namespace Discord public string CustomId { get => _customId; - set => _customId = value?.Length switch + set { - > ComponentBuilder.MaxCustomIdLength => throw new ArgumentOutOfRangeException(nameof(value), $"Custom ID length must be less or equal to {ComponentBuilder.MaxCustomIdLength}."), - 0 => throw new ArgumentOutOfRangeException(nameof(value), "Custom ID length must be at least 1."), - _ => value - }; + Preconditions.AtLeast(value.Length, 1, nameof(CustomId)); + Preconditions.AtMost(value.Length, ComponentBuilder.MaxCustomIdLength, nameof(CustomId)); + _customId = value; + } } /// diff --git a/src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs index c6fa663c..c65f7238 100644 --- a/src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs @@ -272,7 +272,7 @@ namespace Discord // https://discord.com/developers/docs/interactions/application-commands if (!Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); // make sure theres only one option with default set to true if (isDefault == true && Options?.Any(x => x.IsDefault == true) == true) @@ -440,10 +440,10 @@ namespace Discord // https://discord.com/developers/docs/interactions/application-commands if (!Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L]}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); if (name.Any(char.IsUpper)) - throw new FormatException("Name cannot contain any uppercase characters."); + throw new FormatException($"Name cannot contain any uppercase characters. Value: \"{name}\""); } internal static void EnsureValidCommandDescription(string description) @@ -650,7 +650,7 @@ namespace Discord // https://discord.com/developers/docs/interactions/application-commands if (!Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); // make sure theres only one option with default set to true if (isDefault && Options?.Any(x => x.IsDefault == true) == true) @@ -1031,7 +1031,7 @@ namespace Discord // https://discord.com/developers/docs/interactions/application-commands if (!Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$")) - throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name)); + throw new ArgumentException(@$"Name must match the regex ^[-_\p{{L}}\p{{N}}\p{{IsDevanagari}}\p{{IsThai}}]{{1,32}}$. Value: ""{name}""", nameof(name)); } private static void EnsureValidCommandOptionDescription(string description)