Max/Min length fields for ApplicationCommandOption (#2379)

* implement max/min length fields for ApplicationCommandOption

* fix badly formed xml comments
This commit is contained in:
Cenk Ergen
2022-08-03 16:44:30 +03:00
committed by GitHub
parent 1eb42c6128
commit e551431d72
12 changed files with 210 additions and 5 deletions

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -463,6 +463,12 @@ namespace Discord.Interactions.Builders
case MinValueAttribute minValue:
builder.MinValue = minValue.Value;
break;
case MinLengthAttribute minLength:
builder.MinLength = minLength.Length;
break;
case MaxLengthAttribute maxLength:
builder.MaxLength = maxLength.Length;
break;
case ComplexParameterAttribute complexParameter:
{
builder.IsComplexParameter = true;

View File

@@ -28,6 +28,16 @@ namespace Discord.Interactions.Builders
/// </summary>
public double? MinValue { get; set; }
/// <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.
/// </summary>
@@ -125,6 +135,32 @@ namespace Discord.Interactions.Builders
return this;
}
/// <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"/>.
/// </summary>

View File

@@ -38,6 +38,16 @@ namespace Discord.Interactions
/// </summary>
public double? MaxValue { get; }
/// <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
/// <see cref="CommandParameterInfo.ParameterType"/>.
@@ -86,6 +96,8 @@ namespace Discord.Interactions
Description = builder.Description;
MaxValue = builder.MaxValue;
MinValue = builder.MinValue;
MinLength = builder.MinLength;
MaxLength = builder.MaxLength;
IsComplexParameter = builder.IsComplexParameter;
IsAutocomplete = builder.Autocomplete;
Choices = builder.Choices.ToImmutableArray();

View File

@@ -23,7 +23,9 @@ namespace Discord.Interactions
ChannelTypes = parameterInfo.ChannelTypes?.ToList(),
IsAutocomplete = parameterInfo.IsAutocomplete,
MaxValue = parameterInfo.MaxValue,
MinValue = parameterInfo.MinValue
MinValue = parameterInfo.MinValue,
MinLength = parameterInfo.MinLength,
MaxLength = parameterInfo.MaxLength,
};
parameterInfo.TypeConverter.Write(props, parameterInfo);
@@ -209,7 +211,13 @@ namespace Discord.Interactions
Name = x.Name,
Value = x.Value
}).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)