diff --git a/src/Discord.Net.Core/Extensions/ObjectExtensions.cs b/src/Discord.Net.Core/Extensions/ObjectExtensions.cs index 240fb47a..8c679bce 100644 --- a/src/Discord.Net.Core/Extensions/ObjectExtensions.cs +++ b/src/Discord.Net.Core/Extensions/ObjectExtensions.cs @@ -8,6 +8,21 @@ namespace Discord { internal static class ObjectExtensions { + private const long Int53Max = (1L << 53) - 1; + private const long Int53Min = -Int53Max; + + public static (double Min, double Max) GetSupportedNumericalRange(this object o) + => Type.GetTypeCode(o.GetType()) switch + { + TypeCode.Byte => (byte.MinValue, byte.MaxValue), + TypeCode.SByte => (sbyte.MinValue, sbyte.MaxValue), + TypeCode.Int16 => (short.MinValue, short.MaxValue), + TypeCode.UInt16 => (ushort.MinValue, ushort.MaxValue), + TypeCode.Int32 => (int.MinValue, int.MaxValue), + TypeCode.UInt32 => (uint.MinValue, uint.MaxValue), + _ => (Int53Min, Int53Max) + }; + public static bool IsNumericType(this object o) { switch (Type.GetTypeCode(o.GetType())) diff --git a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs index f0ddc795..e16f23cc 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs @@ -466,6 +466,10 @@ namespace Discord.Interactions.Builders builder.IsRequired = !paramInfo.IsOptional; builder.DefaultValue = paramInfo.DefaultValue; + var supportedNumericalRange = paramInfo.GetSupportedNumericalRange(); + builder.MinValue = supportedNumericalRange.Min; + builder.MaxValue = supportedNumericalRange.Max; + foreach (var attribute in attributes) { switch (attribute) @@ -497,9 +501,15 @@ namespace Discord.Interactions.Builders builder.WithAutocompleteHandler(autocomplete.AutocompleteHandlerType, services); break; case MaxValueAttribute maxValue: + if (maxValue.Value > supportedNumericalRange.Max) + throw new ArgumentOutOfRangeException($"{nameof(maxValue)} cannot be greater than {supportedNumericalRange.Max}."); + builder.MaxValue = maxValue.Value; break; case MinValueAttribute minValue: + if (minValue.Value < supportedNumericalRange.Min) + throw new ArgumentOutOfRangeException($"{nameof(minValue)} cannot be less than {supportedNumericalRange.Min}."); + builder.MinValue = minValue.Value; break; case MinLengthAttribute minLength: