Browse Source

update SlashCommandBuilder boolean field names, summaries, and choice parameters

pull/1923/head
quin lynch 3 years ago
parent
commit
5ae3f09fa2
2 changed files with 70 additions and 76 deletions
  1. +0
    -5
      src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionChoice.cs
  2. +70
    -71
      src/Discord.Net.Core/Entities/Interactions/Slash Commands/SlashCommandBuilder.cs

+ 0
- 5
src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionChoice.cs View File

@@ -40,11 +40,6 @@ namespace Discord
get => _value; get => _value;
set set
{ {
if(value != null)
{
if(!(value is int) && !(value is string))
throw new ArgumentException("The value of a choice must be a string or int!");
}
_value = value; _value = value;
} }
} }


+ 70
- 71
src/Discord.Net.Core/Entities/Interactions/Slash Commands/SlashCommandBuilder.cs View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Discord namespace Discord
{ {
/// <summary> /// <summary>
/// A class used to build slash commands.
/// Represents a class used to build slash commands.
/// </summary> /// </summary>
public class SlashCommandBuilder public class SlashCommandBuilder
{ {
@@ -26,7 +26,7 @@ namespace Discord
public const int MaxOptionsCount = 25; public const int MaxOptionsCount = 25;


/// <summary> /// <summary>
/// The name of this slash command.
/// Gets or sets the name of this slash command.
/// </summary> /// </summary>
public string Name public string Name
{ {
@@ -50,7 +50,7 @@ namespace Discord
} }


/// <summary> /// <summary>
/// A 1-100 length description of this slash command
/// Gets or sets a 1-100 length description of this slash command
/// </summary> /// </summary>
public string Description public string Description
{ {
@@ -84,9 +84,9 @@ namespace Discord
} }


/// <summary> /// <summary>
/// Whether the command is enabled by default when the app is added to a guild
/// Gets or sets whether the command is enabled by default when the app is added to a guild
/// </summary> /// </summary>
public bool DefaultPermission { get; set; } = true;
public bool IsDefaultPermission { get; set; } = true;


private string _name { get; set; } private string _name { get; set; }
private string _description { get; set; } private string _description { get; set; }
@@ -95,14 +95,14 @@ namespace Discord
/// <summary> /// <summary>
/// Build the current builder into a <see cref="SlashCommandProperties"/> class. /// Build the current builder into a <see cref="SlashCommandProperties"/> class.
/// </summary> /// </summary>
/// <returns>A <see cref="SlashCommandProperties"/> that can be used to create slash commands over rest.</returns>
/// <returns>A <see cref="SlashCommandProperties"/> that can be used to create slash commands.</returns>
public SlashCommandProperties Build() public SlashCommandProperties Build()
{ {
SlashCommandProperties props = new SlashCommandProperties() SlashCommandProperties props = new SlashCommandProperties()
{ {
Name = Name, Name = Name,
Description = Description, Description = Description,
IsDefaultPermission = DefaultPermission,
IsDefaultPermission = IsDefaultPermission,
}; };


if (Options != null && Options.Any()) if (Options != null && Options.Any())
@@ -115,7 +115,6 @@ namespace Discord
} }


return props; return props;

} }


/// <summary> /// <summary>
@@ -149,7 +148,7 @@ namespace Discord
/// <returns>The current builder.</returns> /// <returns>The current builder.</returns>
public SlashCommandBuilder WithDefaultPermission(bool value) public SlashCommandBuilder WithDefaultPermission(bool value)
{ {
DefaultPermission = value;
IsDefaultPermission = value;
return this; return this;
} }


@@ -159,14 +158,14 @@ namespace Discord
/// <param name="name">The name of the option to add.</param> /// <param name="name">The name of the option to add.</param>
/// <param name="type">The type of this option.</param> /// <param name="type">The type of this option.</param>
/// <param name="description">The description of this option.</param> /// <param name="description">The description of this option.</param>
/// <param name="required">If this option is required for this command.</param>
/// <param name="isRequired">If this option is required for this command.</param>
/// <param name="isDefault">If this option is the default option.</param> /// <param name="isDefault">If this option is the default option.</param>
/// <param name="isAutocomplete">If this option is set to autocompleate.</param> /// <param name="isAutocomplete">If this option is set to autocompleate.</param>
/// <param name="options">The options of the option to add.</param> /// <param name="options">The options of the option to add.</param>
/// <param name="choices">The choices of this option.</param> /// <param name="choices">The choices of this option.</param>
/// <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? required = null, bool? isDefault = null, bool isAutocomplete = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
string description, bool? isRequired = null, bool? isDefault = null, bool isAutocomplete = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
{ {
// Make sure the name matches the requirements from discord // Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(name, nameof(name)); Preconditions.NotNullOrEmpty(name, nameof(name));
@@ -187,7 +186,7 @@ namespace Discord
if (isDefault.HasValue && isDefault.Value) if (isDefault.HasValue && isDefault.Value)
{ {
if (Options != null) if (Options != null)
if (Options.Any(x => x.Default.HasValue && x.Default.Value))
if (Options.Any(x => x.IsDefault.HasValue && x.IsDefault.Value))
throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault)); throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault));
} }


@@ -195,41 +194,17 @@ namespace Discord
{ {
Name = name, Name = name,
Description = description, Description = description,
Required = required,
Default = isDefault,
IsRequired = isRequired,
IsDefault = isDefault,
Options = options, Options = options,
Type = type, Type = type,
Autocomplete = isAutocomplete,
IsAutocomplete = isAutocomplete,
Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null
}; };


return AddOption(option); return AddOption(option);
} }


///// <summary>
///// Adds an option to the current slash command.
///// </summary>
///// <param name="name">The name of the option to add.</param>
///// <param name="type">The type of this option.</param>
///// <param name="description">The description of this option.</param>
///// <param name="required">If this option is required for this command.</param>
///// <param name="isDefault">If this option is the default option.</param>
///// <param name="choices">The choices of this option.</param>
///// <returns>The current builder.</returns>
//public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type,
// string description, bool required = true, bool isDefault = false, params ApplicationCommandOptionChoiceProperties[] choices)
// => AddOption(name, type, description, required, isDefault, null, choices);

/// <summary>
/// Adds an option to the current slash command.
/// </summary>
/// <param name="name">The name of the option to add.</param>
/// <param name="type">The type of this option.</param>
/// <param name="description">The sescription of this option.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type, string description)
=> AddOption(name, type, description, options: null, choices: null);

/// <summary> /// <summary>
/// Adds an option to this slash command. /// Adds an option to this slash command.
/// </summary> /// </summary>
@@ -337,17 +312,17 @@ namespace Discord
/// <summary> /// <summary>
/// Gets or sets whether or not this options is the first required option for the user to complete. only one option can be default. /// Gets or sets whether or not this options is the first required option for the user to complete. only one option can be default.
/// </summary> /// </summary>
public bool? Default { get; set; }
public bool? IsDefault { get; set; }


/// <summary> /// <summary>
/// Gets or sets if the option is required. /// Gets or sets if the option is required.
/// </summary> /// </summary>
public bool? Required { get; set; } = null;
public bool? IsRequired { get; set; } = null;


/// <summary> /// <summary>
/// Gets or sets whether or not this option supports autocomplete. /// Gets or sets whether or not this option supports autocomplete.
/// </summary> /// </summary>
public bool Autocomplete { get; set; }
public bool IsAutocomplete { get; set; }


/// <summary> /// <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.
@@ -377,12 +352,12 @@ namespace Discord
{ {
Name = Name, Name = Name,
Description = Description, Description = Description,
IsDefault = Default,
IsRequired = Required,
IsDefault = IsDefault,
IsRequired = IsRequired,
Type = Type, Type = Type,
Options = Options?.Count > 0 ? new List<ApplicationCommandOptionProperties>(Options.Select(x => x.Build())) : null, Options = Options?.Count > 0 ? new List<ApplicationCommandOptionProperties>(Options.Select(x => x.Build())) : null,
Choices = Choices, Choices = Choices,
IsAutocomplete = Autocomplete
IsAutocomplete = IsAutocomplete
}; };
} }


@@ -392,13 +367,13 @@ namespace Discord
/// <param name="name">The name of the option to add.</param> /// <param name="name">The name of the option to add.</param>
/// <param name="type">The type of this option.</param> /// <param name="type">The type of this option.</param>
/// <param name="description">The description of this option.</param> /// <param name="description">The description of this option.</param>
/// <param name="required">If this option is required for this command.</param>
/// <param name="isRequired">If this option is required for this command.</param>
/// <param name="isDefault">If this option is the default option.</param> /// <param name="isDefault">If this option is the default option.</param>
/// <param name="options">The options of the option to add.</param> /// <param name="options">The options of the option to add.</param>
/// <param name="choices">The choices of this option.</param> /// <param name="choices">The choices of this option.</param>
/// <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? required = null, bool isDefault = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
string description, bool? isRequired = null, bool isDefault = false, bool isAutocomplete = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
{ {
// Make sure the name matches the requirements from discord // Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(name, nameof(name)); Preconditions.NotNullOrEmpty(name, nameof(name));
@@ -419,7 +394,7 @@ namespace Discord
if (isDefault) if (isDefault)
{ {
if (Options != null) if (Options != null)
if (Options.Any(x => x.Default.HasValue && x.Default.Value))
if (Options.Any(x => x.IsDefault.HasValue && x.IsDefault.Value))
throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault)); throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault));
} }


@@ -427,8 +402,9 @@ namespace Discord
{ {
Name = name, Name = name,
Description = description, Description = description,
Required = required,
Default = isDefault,
IsRequired = isRequired,
IsAutocomplete = isAutocomplete,
IsDefault = isDefault,
Options = options, Options = options,
Type = type, Type = type,
Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null
@@ -464,25 +440,40 @@ namespace Discord
/// <returns>The current builder.</returns> /// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string name, int value) public SlashCommandOptionBuilder AddChoice(string name, int value)
{ {
if (Choices == null)
Choices = new List<ApplicationCommandOptionChoiceProperties>();

if (Choices.Count >= MaxChoiceCount)
throw new ArgumentOutOfRangeException(nameof(Choices), $"Cannot add more than {MaxChoiceCount} choices!");

if (name == null)
throw new ArgumentNullException($"{nameof(name)} cannot be null!");
return AddChoiceInternal(name, value);
}


Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, 100, nameof(name));
/// <summary>
/// Adds a choice to the current option.
/// </summary>
/// <param name="name">The name of the choice.</param>
/// <param name="value">The value of the choice.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string name, string value)
{
return AddChoiceInternal(name, value);
}


Choices.Add(new ApplicationCommandOptionChoiceProperties()
{
Name = name,
Value = value
});
/// <summary>
/// Adds a choice to the current option.
/// </summary>
/// <param name="name">The name of the choice.</param>
/// <param name="value">The value of the choice.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string name, double value)
{
return AddChoiceInternal(name, value);
}


return this;
/// <summary>
/// Adds a choice to the current option.
/// </summary>
/// <param name="name">The name of the choice.</param>
/// <param name="value">The value of the choice.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string name, float value)
{
return AddChoiceInternal(name, value);
} }


/// <summary> /// <summary>
@@ -491,7 +482,12 @@ namespace Discord
/// <param name="name">The name of the choice.</param> /// <param name="name">The name of the choice.</param>
/// <param name="value">The value of the choice.</param> /// <param name="value">The value of the choice.</param>
/// <returns>The current builder.</returns> /// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string name, string value)
public SlashCommandOptionBuilder AddChoice(string name, long value)
{
return AddChoiceInternal(name, value);
}

private SlashCommandOptionBuilder AddChoiceInternal(string name, object value)
{ {
if (Choices == null) if (Choices == null)
Choices = new List<ApplicationCommandOptionChoiceProperties>(); Choices = new List<ApplicationCommandOptionChoiceProperties>();
@@ -508,8 +504,11 @@ namespace Discord
Preconditions.AtLeast(name.Length, 1, nameof(name)); Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, 100, nameof(name)); Preconditions.AtMost(name.Length, 100, nameof(name));


Preconditions.AtLeast(value.Length, 1, nameof(value));
Preconditions.AtMost(value.Length, 100, nameof(value));
if(value is string str)
{
Preconditions.AtLeast(str.Length, 1, nameof(value));
Preconditions.AtMost(str.Length, 100, nameof(value));
}


Choices.Add(new ApplicationCommandOptionChoiceProperties() Choices.Add(new ApplicationCommandOptionChoiceProperties()
{ {
@@ -550,7 +549,7 @@ namespace Discord
/// <returns>The current builder.</returns> /// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithRequired(bool value) public SlashCommandOptionBuilder WithRequired(bool value)
{ {
Required = value;
IsRequired = value;
return this; return this;
} }


@@ -561,7 +560,7 @@ namespace Discord
/// <returns>The current builder.</returns> /// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithDefault(bool value) public SlashCommandOptionBuilder WithDefault(bool value)
{ {
Default = value;
IsDefault = value;
return this; return this;
} }




Loading…
Cancel
Save