* implement max/min length fields for ApplicationCommandOption * fix badly formed xml commentspull/2418/head
@@ -82,6 +82,16 @@ namespace Discord | |||||
public double? MaxValue { get; set; } | public double? MaxValue { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets or sets the minimum allowed length for a string input. | |||||
/// </summary> | |||||
public int? MinLength { get; set; } | |||||
/// <summary> | |||||
/// Gets or sets the maximum allowed length for a string input. | |||||
/// </summary> | |||||
public int? MaxLength { get; set; } | |||||
/// <summary> | |||||
/// Gets or sets the choices for string and int types for the user to pick from. | /// Gets or sets the choices for string and int types for the user to pick from. | ||||
/// </summary> | /// </summary> | ||||
public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; } | public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; } | ||||
@@ -48,6 +48,16 @@ namespace Discord | |||||
double? MaxValue { get; } | double? MaxValue { get; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets the minimum allowed length for a string input. | |||||
/// </summary> | |||||
int? MinLength { get; } | |||||
/// <summary> | |||||
/// Gets the maximum allowed length for a string input. | |||||
/// </summary> | |||||
int? MaxLength { get; } | |||||
/// <summary> | |||||
/// Gets the choices for string and int types for the user to pick from. | /// Gets the choices for string and int types for the user to pick from. | ||||
/// </summary> | /// </summary> | ||||
IReadOnlyCollection<IApplicationCommandOptionChoice> Choices { get; } | IReadOnlyCollection<IApplicationCommandOptionChoice> Choices { get; } | ||||
@@ -196,7 +196,7 @@ namespace Discord | |||||
/// <returns>The current builder.</returns> | /// <returns>The current builder.</returns> | ||||
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type, | public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type, | ||||
string description, bool? isRequired = null, bool? isDefault = null, bool isAutocomplete = false, double? minValue = null, double? maxValue = null, | string description, bool? isRequired = null, bool? isDefault = null, bool isAutocomplete = false, double? minValue = null, double? maxValue = null, | ||||
List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices) | |||||
int? minLength = null, int? maxLength = null, List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices) | |||||
{ | { | ||||
Preconditions.Options(name, description); | Preconditions.Options(name, description); | ||||
@@ -222,6 +222,8 @@ namespace Discord | |||||
ChannelTypes = channelTypes, | ChannelTypes = channelTypes, | ||||
MinValue = minValue, | MinValue = minValue, | ||||
MaxValue = maxValue, | MaxValue = maxValue, | ||||
MinLength = minLength, | |||||
MaxLength = maxLength, | |||||
}; | }; | ||||
return AddOption(option); | return AddOption(option); | ||||
@@ -355,6 +357,16 @@ namespace Discord | |||||
public double? MaxValue { get; set; } | public double? MaxValue { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets or sets the minimum allowed length for a string input. | |||||
/// </summary> | |||||
public int? MinLength { get; set; } | |||||
/// <summary> | |||||
/// Gets or sets the maximum allowed length for a string input. | |||||
/// </summary> | |||||
public int? MaxLength { get; set; } | |||||
/// <summary> | |||||
/// Gets or sets the choices for string and int types for the user to pick from. | /// Gets or sets the choices for string and int types for the user to pick from. | ||||
/// </summary> | /// </summary> | ||||
public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; } | public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; } | ||||
@@ -377,6 +389,7 @@ namespace Discord | |||||
{ | { | ||||
bool isSubType = Type == ApplicationCommandOptionType.SubCommandGroup; | bool isSubType = Type == ApplicationCommandOptionType.SubCommandGroup; | ||||
bool isIntType = Type == ApplicationCommandOptionType.Integer; | bool isIntType = Type == ApplicationCommandOptionType.Integer; | ||||
bool isStrType = Type == ApplicationCommandOptionType.String; | |||||
if (isSubType && (Options == null || !Options.Any())) | if (isSubType && (Options == null || !Options.Any())) | ||||
throw new InvalidOperationException("SubCommands/SubCommandGroups must have at least one option"); | throw new InvalidOperationException("SubCommands/SubCommandGroups must have at least one option"); | ||||
@@ -390,6 +403,12 @@ namespace Discord | |||||
if (isIntType && MaxValue != null && MaxValue % 1 != 0) | if (isIntType && MaxValue != null && MaxValue % 1 != 0) | ||||
throw new InvalidOperationException("MaxValue cannot have decimals on Integer command options."); | throw new InvalidOperationException("MaxValue cannot have decimals on Integer command options."); | ||||
if(isStrType && MinLength is not null && MinLength < 0) | |||||
throw new InvalidOperationException("MinLength cannot be smaller than 0."); | |||||
if (isStrType && MaxLength is not null && MaxLength < 1) | |||||
throw new InvalidOperationException("MaxLength cannot be smaller than 1."); | |||||
return new ApplicationCommandOptionProperties | return new ApplicationCommandOptionProperties | ||||
{ | { | ||||
Name = Name, | Name = Name, | ||||
@@ -404,7 +423,9 @@ namespace Discord | |||||
IsAutocomplete = IsAutocomplete, | IsAutocomplete = IsAutocomplete, | ||||
ChannelTypes = ChannelTypes, | ChannelTypes = ChannelTypes, | ||||
MinValue = MinValue, | MinValue = MinValue, | ||||
MaxValue = MaxValue | |||||
MaxValue = MaxValue, | |||||
MinLength = MinLength, | |||||
MaxLength = MaxLength, | |||||
}; | }; | ||||
} | } | ||||
@@ -425,7 +446,7 @@ namespace Discord | |||||
/// <returns>The current builder.</returns> | /// <returns>The current builder.</returns> | ||||
public SlashCommandOptionBuilder AddOption(string name, ApplicationCommandOptionType type, | public SlashCommandOptionBuilder AddOption(string name, ApplicationCommandOptionType type, | ||||
string description, bool? isRequired = null, bool isDefault = false, bool isAutocomplete = false, double? minValue = null, double? maxValue = null, | string description, bool? isRequired = null, bool isDefault = false, bool isAutocomplete = false, double? minValue = null, double? maxValue = null, | ||||
List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices) | |||||
int? minLength = null, int? maxLength = null, List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices) | |||||
{ | { | ||||
Preconditions.Options(name, description); | Preconditions.Options(name, description); | ||||
@@ -447,6 +468,8 @@ namespace Discord | |||||
IsAutocomplete = isAutocomplete, | IsAutocomplete = isAutocomplete, | ||||
MinValue = minValue, | MinValue = minValue, | ||||
MaxValue = maxValue, | MaxValue = maxValue, | ||||
MinLength = minLength, | |||||
MaxLength = maxLength, | |||||
Options = options, | Options = options, | ||||
Type = type, | Type = type, | ||||
Choices = (choices ?? Array.Empty<ApplicationCommandOptionChoiceProperties>()).ToList(), | Choices = (choices ?? Array.Empty<ApplicationCommandOptionChoiceProperties>()).ToList(), | ||||
@@ -670,6 +693,28 @@ namespace Discord | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// Sets the current builders min length field. | |||||
/// </summary> | |||||
/// <param name="length">The value to set.</param> | |||||
/// <returns>The current builder.</returns> | |||||
public SlashCommandOptionBuilder WithMinLength(int length) | |||||
{ | |||||
MinLength = length; | |||||
return this; | |||||
} | |||||
/// <summary> | |||||
/// Sets the current builders max length field. | |||||
/// </summary> | |||||
/// <param name="lenght">The value to set.</param> | |||||
/// <returns>The current builder.</returns> | |||||
public SlashCommandOptionBuilder WithMaxLength(int lenght) | |||||
{ | |||||
MaxLength = lenght; | |||||
return this; | |||||
} | |||||
/// <summary> | |||||
/// Sets the current type of this builder. | /// Sets the current type of this builder. | ||||
/// </summary> | /// </summary> | ||||
/// <param name="type">The type to set.</param> | /// <param name="type">The type to set.</param> | ||||
@@ -0,0 +1,25 @@ | |||||
using System; | |||||
namespace Discord.Interactions | |||||
{ | |||||
/// <summary> | |||||
/// Sets the maximum length allowed for a string type parameter. | |||||
/// </summary> | |||||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] | |||||
public class MaxLengthAttribute : Attribute | |||||
{ | |||||
/// <summary> | |||||
/// Gets the maximum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int Length { get; } | |||||
/// <summary> | |||||
/// Sets the maximum length allowed for a string type parameter. | |||||
/// </summary> | |||||
/// <param name="lenght">Maximum string length allowed.</param> | |||||
public MaxLengthAttribute(int lenght) | |||||
{ | |||||
Length = lenght; | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,25 @@ | |||||
using System; | |||||
namespace Discord.Interactions | |||||
{ | |||||
/// <summary> | |||||
/// Sets the minimum length allowed for a string type parameter. | |||||
/// </summary> | |||||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] | |||||
public class MinLengthAttribute : Attribute | |||||
{ | |||||
/// <summary> | |||||
/// Gets the minimum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int Length { get; } | |||||
/// <summary> | |||||
/// Sets the minimum length allowed for a string type parameter. | |||||
/// </summary> | |||||
/// <param name="lenght">Minimum string length allowed.</param> | |||||
public MinLengthAttribute(int lenght) | |||||
{ | |||||
Length = lenght; | |||||
} | |||||
} | |||||
} |
@@ -463,6 +463,12 @@ namespace Discord.Interactions.Builders | |||||
case MinValueAttribute minValue: | case MinValueAttribute minValue: | ||||
builder.MinValue = minValue.Value; | builder.MinValue = minValue.Value; | ||||
break; | break; | ||||
case MinLengthAttribute minLength: | |||||
builder.MinLength = minLength.Length; | |||||
break; | |||||
case MaxLengthAttribute maxLength: | |||||
builder.MaxLength = maxLength.Length; | |||||
break; | |||||
case ComplexParameterAttribute complexParameter: | case ComplexParameterAttribute complexParameter: | ||||
{ | { | ||||
builder.IsComplexParameter = true; | builder.IsComplexParameter = true; | ||||
@@ -29,6 +29,16 @@ namespace Discord.Interactions.Builders | |||||
public double? MinValue { get; set; } | public double? MinValue { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets or sets the minimum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int? MinLength { get; set; } | |||||
/// <summary> | |||||
/// Gets or sets the maximum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int? MaxLength { get; set; } | |||||
/// <summary> | |||||
/// Gets a collection of the choices of this command. | /// Gets a collection of the choices of this command. | ||||
/// </summary> | /// </summary> | ||||
public IReadOnlyCollection<ParameterChoice> Choices => _choices; | public IReadOnlyCollection<ParameterChoice> Choices => _choices; | ||||
@@ -126,6 +136,32 @@ namespace Discord.Interactions.Builders | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// Sets <see cref="MinLength"/>. | |||||
/// </summary> | |||||
/// <param name="length">New value of the <see cref="MinLength"/>.</param> | |||||
/// <returns> | |||||
/// The builder instance. | |||||
/// </returns> | |||||
public SlashCommandParameterBuilder WithMinLength(int length) | |||||
{ | |||||
MinLength = length; | |||||
return this; | |||||
} | |||||
/// <summary> | |||||
/// Sets <see cref="MaxLength"/>. | |||||
/// </summary> | |||||
/// <param name="length">New value of the <see cref="MaxLength"/>.</param> | |||||
/// <returns> | |||||
/// The builder instance. | |||||
/// </returns> | |||||
public SlashCommandParameterBuilder WithMaxLength(int length) | |||||
{ | |||||
MaxLength = length; | |||||
return this; | |||||
} | |||||
/// <summary> | |||||
/// Adds parameter choices to <see cref="Choices"/>. | /// Adds parameter choices to <see cref="Choices"/>. | ||||
/// </summary> | /// </summary> | ||||
/// <param name="options">New choices to be added to <see cref="Choices"/>.</param> | /// <param name="options">New choices to be added to <see cref="Choices"/>.</param> | ||||
@@ -39,6 +39,16 @@ namespace Discord.Interactions | |||||
public double? MaxValue { get; } | public double? MaxValue { get; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets the minimum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int? MinLength { get; } | |||||
/// <summary> | |||||
/// Gets the maximum length allowed for a string type parameter. | |||||
/// </summary> | |||||
public int? MaxLength { get; } | |||||
/// <summary> | |||||
/// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.WebSocket.SocketSlashCommandDataOption"/> into | /// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.WebSocket.SocketSlashCommandDataOption"/> into | ||||
/// <see cref="CommandParameterInfo.ParameterType"/>. | /// <see cref="CommandParameterInfo.ParameterType"/>. | ||||
/// </summary> | /// </summary> | ||||
@@ -86,6 +96,8 @@ namespace Discord.Interactions | |||||
Description = builder.Description; | Description = builder.Description; | ||||
MaxValue = builder.MaxValue; | MaxValue = builder.MaxValue; | ||||
MinValue = builder.MinValue; | MinValue = builder.MinValue; | ||||
MinLength = builder.MinLength; | |||||
MaxLength = builder.MaxLength; | |||||
IsComplexParameter = builder.IsComplexParameter; | IsComplexParameter = builder.IsComplexParameter; | ||||
IsAutocomplete = builder.Autocomplete; | IsAutocomplete = builder.Autocomplete; | ||||
Choices = builder.Choices.ToImmutableArray(); | Choices = builder.Choices.ToImmutableArray(); | ||||
@@ -23,7 +23,9 @@ namespace Discord.Interactions | |||||
ChannelTypes = parameterInfo.ChannelTypes?.ToList(), | ChannelTypes = parameterInfo.ChannelTypes?.ToList(), | ||||
IsAutocomplete = parameterInfo.IsAutocomplete, | IsAutocomplete = parameterInfo.IsAutocomplete, | ||||
MaxValue = parameterInfo.MaxValue, | MaxValue = parameterInfo.MaxValue, | ||||
MinValue = parameterInfo.MinValue | |||||
MinValue = parameterInfo.MinValue, | |||||
MinLength = parameterInfo.MinLength, | |||||
MaxLength = parameterInfo.MaxLength, | |||||
}; | }; | ||||
parameterInfo.TypeConverter.Write(props, parameterInfo); | parameterInfo.TypeConverter.Write(props, parameterInfo); | ||||
@@ -209,7 +211,13 @@ namespace Discord.Interactions | |||||
Name = x.Name, | Name = x.Name, | ||||
Value = x.Value | Value = x.Value | ||||
}).ToList(), | }).ToList(), | ||||
Options = commandOption.Options?.Select(x => x.ToApplicationCommandOptionProps()).ToList() | |||||
Options = commandOption.Options?.Select(x => x.ToApplicationCommandOptionProps()).ToList(), | |||||
MaxLength = commandOption.MaxLength, | |||||
MinLength = commandOption.MinLength, | |||||
MaxValue = commandOption.MaxValue, | |||||
MinValue = commandOption.MinValue, | |||||
IsAutocomplete = commandOption.IsAutocomplete.GetValueOrDefault(), | |||||
ChannelTypes = commandOption.ChannelTypes.ToList(), | |||||
}; | }; | ||||
public static Modal ToModal(this ModalInfo modalInfo, string customId, Action<ModalBuilder> modifyModal = null) | public static Modal ToModal(this ModalInfo modalInfo, string customId, Action<ModalBuilder> modifyModal = null) | ||||
@@ -38,6 +38,12 @@ namespace Discord.API | |||||
[JsonProperty("channel_types")] | [JsonProperty("channel_types")] | ||||
public Optional<ChannelType[]> ChannelTypes { get; set; } | public Optional<ChannelType[]> ChannelTypes { get; set; } | ||||
[JsonProperty("min_length")] | |||||
public Optional<int> MinLength { get; set; } | |||||
[JsonProperty("max_length")] | |||||
public Optional<int> MaxLength { get; set; } | |||||
public ApplicationCommandOption() { } | public ApplicationCommandOption() { } | ||||
public ApplicationCommandOption(IApplicationCommandOption cmd) | public ApplicationCommandOption(IApplicationCommandOption cmd) | ||||
@@ -56,6 +62,8 @@ namespace Discord.API | |||||
Default = cmd.IsDefault ?? Optional<bool>.Unspecified; | Default = cmd.IsDefault ?? Optional<bool>.Unspecified; | ||||
MinValue = cmd.MinValue ?? Optional<double>.Unspecified; | MinValue = cmd.MinValue ?? Optional<double>.Unspecified; | ||||
MaxValue = cmd.MaxValue ?? Optional<double>.Unspecified; | MaxValue = cmd.MaxValue ?? Optional<double>.Unspecified; | ||||
MinLength = cmd.MinLength ?? Optional<int>.Unspecified; | |||||
MaxLength = cmd.MaxLength ?? Optional<int>.Unspecified; | |||||
Autocomplete = cmd.IsAutocomplete ?? Optional<bool>.Unspecified; | Autocomplete = cmd.IsAutocomplete ?? Optional<bool>.Unspecified; | ||||
Name = cmd.Name; | Name = cmd.Name; | ||||
@@ -77,6 +85,8 @@ namespace Discord.API | |||||
Default = option.IsDefault ?? Optional<bool>.Unspecified; | Default = option.IsDefault ?? Optional<bool>.Unspecified; | ||||
MinValue = option.MinValue ?? Optional<double>.Unspecified; | MinValue = option.MinValue ?? Optional<double>.Unspecified; | ||||
MaxValue = option.MaxValue ?? Optional<double>.Unspecified; | MaxValue = option.MaxValue ?? Optional<double>.Unspecified; | ||||
MinLength = option.MinLength ?? Optional<int>.Unspecified; | |||||
MaxLength = option.MaxLength ?? Optional<int>.Unspecified; | |||||
ChannelTypes = option.ChannelTypes?.ToArray() ?? Optional<ChannelType[]>.Unspecified; | ChannelTypes = option.ChannelTypes?.ToArray() ?? Optional<ChannelType[]>.Unspecified; | ||||
@@ -35,6 +35,12 @@ namespace Discord.Rest | |||||
/// <inheritdoc/> | /// <inheritdoc/> | ||||
public double? MaxValue { get; private set; } | public double? MaxValue { get; private set; } | ||||
/// <inheritdoc/> | |||||
public int? MinLength { get; private set; } | |||||
/// <inheritdoc/> | |||||
public int? MaxLength { get; private set; } | |||||
/// <summary> | /// <summary> | ||||
/// Gets a collection of <see cref="RestApplicationCommandChoice"/>s for this command. | /// Gets a collection of <see cref="RestApplicationCommandChoice"/>s for this command. | ||||
/// </summary> | /// </summary> | ||||
@@ -78,6 +84,9 @@ namespace Discord.Rest | |||||
if (model.Autocomplete.IsSpecified) | if (model.Autocomplete.IsSpecified) | ||||
IsAutocomplete = model.Autocomplete.Value; | IsAutocomplete = model.Autocomplete.Value; | ||||
MinLength = model.MinLength.ToNullable(); | |||||
MaxLength = model.MaxLength.ToNullable(); | |||||
Options = model.Options.IsSpecified | Options = model.Options.IsSpecified | ||||
? model.Options.Value.Select(Create).ToImmutableArray() | ? model.Options.Value.Select(Create).ToImmutableArray() | ||||
: ImmutableArray.Create<RestApplicationCommandOption>(); | : ImmutableArray.Create<RestApplicationCommandOption>(); | ||||
@@ -33,6 +33,12 @@ namespace Discord.WebSocket | |||||
/// <inheritdoc/> | /// <inheritdoc/> | ||||
public double? MaxValue { get; private set; } | public double? MaxValue { get; private set; } | ||||
/// <inheritdoc/> | |||||
public int? MinLength { get; private set; } | |||||
/// <inheritdoc/> | |||||
public int? MaxLength { get; private set; } | |||||
/// <summary> | /// <summary> | ||||
/// Gets a collection of choices for the user to pick from. | /// Gets a collection of choices for the user to pick from. | ||||
/// </summary> | /// </summary> | ||||
@@ -72,6 +78,9 @@ namespace Discord.WebSocket | |||||
IsAutocomplete = model.Autocomplete.ToNullable(); | IsAutocomplete = model.Autocomplete.ToNullable(); | ||||
MinLength = model.MinLength.ToNullable(); | |||||
MaxLength = model.MaxLength.ToNullable(); | |||||
Choices = model.Choices.IsSpecified | Choices = model.Choices.IsSpecified | ||||
? model.Choices.Value.Select(SocketApplicationCommandChoice.Create).ToImmutableArray() | ? model.Choices.Value.Select(SocketApplicationCommandChoice.Create).ToImmutableArray() | ||||
: ImmutableArray.Create<SocketApplicationCommandChoice>(); | : ImmutableArray.Create<SocketApplicationCommandChoice>(); | ||||