Browse Source

Max/Min length fields for ApplicationCommandOption (#2379)

* implement max/min length fields for ApplicationCommandOption

* fix badly formed xml comments
tags/3.8.0
Cenk Ergen GitHub 2 years ago
parent
commit
e551431d72
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 210 additions and 5 deletions
  1. +10
    -0
      src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs
  2. +10
    -0
      src/Discord.Net.Core/Entities/Interactions/IApplicationCommandOption.cs
  3. +48
    -3
      src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs
  4. +25
    -0
      src/Discord.Net.Interactions/Attributes/MaxLengthAttribute.cs
  5. +25
    -0
      src/Discord.Net.Interactions/Attributes/MinLengthAttribute.cs
  6. +6
    -0
      src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs
  7. +36
    -0
      src/Discord.Net.Interactions/Builders/Parameters/SlashCommandParameterBuilder.cs
  8. +12
    -0
      src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs
  9. +10
    -2
      src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs
  10. +10
    -0
      src/Discord.Net.Rest/API/Common/ApplicationCommandOption.cs
  11. +9
    -0
      src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommandOption.cs
  12. +9
    -0
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommandOption.cs

+ 10
- 0
src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs View File

@@ -81,6 +81,16 @@ namespace Discord
/// </summary>
public double? MaxValue { get; set; }

/// <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.
/// </summary>


+ 10
- 0
src/Discord.Net.Core/Entities/Interactions/IApplicationCommandOption.cs View File

@@ -47,6 +47,16 @@ namespace Discord
/// </summary>
double? MaxValue { get; }

/// <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.
/// </summary>


+ 48
- 3
src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs View File

@@ -196,7 +196,7 @@ namespace Discord
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type,
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);

@@ -222,6 +222,8 @@ namespace Discord
ChannelTypes = channelTypes,
MinValue = minValue,
MaxValue = maxValue,
MinLength = minLength,
MaxLength = maxLength,
};

return AddOption(option);
@@ -354,6 +356,16 @@ namespace Discord
/// </summary>
public double? MaxValue { get; set; }

/// <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.
/// </summary>
@@ -377,6 +389,7 @@ namespace Discord
{
bool isSubType = Type == ApplicationCommandOptionType.SubCommandGroup;
bool isIntType = Type == ApplicationCommandOptionType.Integer;
bool isStrType = Type == ApplicationCommandOptionType.String;

if (isSubType && (Options == null || !Options.Any()))
throw new InvalidOperationException("SubCommands/SubCommandGroups must have at least one option");
@@ -390,6 +403,12 @@ namespace Discord
if (isIntType && MaxValue != null && MaxValue % 1 != 0)
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
{
Name = Name,
@@ -404,7 +423,9 @@ namespace Discord
IsAutocomplete = IsAutocomplete,
ChannelTypes = ChannelTypes,
MinValue = MinValue,
MaxValue = MaxValue
MaxValue = MaxValue,
MinLength = MinLength,
MaxLength = MaxLength,
};
}

@@ -425,7 +446,7 @@ namespace Discord
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddOption(string name, ApplicationCommandOptionType type,
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);

@@ -447,6 +468,8 @@ namespace Discord
IsAutocomplete = isAutocomplete,
MinValue = minValue,
MaxValue = maxValue,
MinLength = minLength,
MaxLength = maxLength,
Options = options,
Type = type,
Choices = (choices ?? Array.Empty<ApplicationCommandOptionChoiceProperties>()).ToList(),
@@ -669,6 +692,28 @@ namespace Discord
return this;
}

/// <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.
/// </summary>


+ 25
- 0
src/Discord.Net.Interactions/Attributes/MaxLengthAttribute.cs 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;
}
}
}

+ 25
- 0
src/Discord.Net.Interactions/Attributes/MinLengthAttribute.cs 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;
}
}
}

+ 6
- 0
src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs 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;


+ 36
- 0
src/Discord.Net.Interactions/Builders/Parameters/SlashCommandParameterBuilder.cs 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>


+ 12
- 0
src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs 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();


+ 10
- 2
src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs 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)


+ 10
- 0
src/Discord.Net.Rest/API/Common/ApplicationCommandOption.cs View File

@@ -38,6 +38,12 @@ namespace Discord.API
[JsonProperty("channel_types")]
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(IApplicationCommandOption cmd)
@@ -56,6 +62,8 @@ namespace Discord.API
Default = cmd.IsDefault ?? Optional<bool>.Unspecified;
MinValue = cmd.MinValue ?? 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;

Name = cmd.Name;
@@ -77,6 +85,8 @@ namespace Discord.API
Default = option.IsDefault ?? Optional<bool>.Unspecified;
MinValue = option.MinValue ?? 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;



+ 9
- 0
src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommandOption.cs View File

@@ -35,6 +35,12 @@ namespace Discord.Rest
/// <inheritdoc/>
public double? MaxValue { get; private set; }

/// <inheritdoc/>
public int? MinLength { get; private set; }

/// <inheritdoc/>
public int? MaxLength { get; private set; }

/// <summary>
/// Gets a collection of <see cref="RestApplicationCommandChoice"/>s for this command.
/// </summary>
@@ -78,6 +84,9 @@ namespace Discord.Rest
if (model.Autocomplete.IsSpecified)
IsAutocomplete = model.Autocomplete.Value;

MinLength = model.MinLength.ToNullable();
MaxLength = model.MaxLength.ToNullable();

Options = model.Options.IsSpecified
? model.Options.Value.Select(Create).ToImmutableArray()
: ImmutableArray.Create<RestApplicationCommandOption>();


+ 9
- 0
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommandOption.cs View File

@@ -33,6 +33,12 @@ namespace Discord.WebSocket
/// <inheritdoc/>
public double? MaxValue { get; private set; }

/// <inheritdoc/>
public int? MinLength { get; private set; }

/// <inheritdoc/>
public int? MaxLength { get; private set; }

/// <summary>
/// Gets a collection of choices for the user to pick from.
/// </summary>
@@ -72,6 +78,9 @@ namespace Discord.WebSocket

IsAutocomplete = model.Autocomplete.ToNullable();

MinLength = model.MinLength.ToNullable();
MaxLength = model.MaxLength.ToNullable();

Choices = model.Choices.IsSpecified
? model.Choices.Value.Select(SocketApplicationCommandChoice.Create).ToImmutableArray()
: ImmutableArray.Create<SocketApplicationCommandChoice>();


Loading…
Cancel
Save