@@ -86,6 +86,10 @@ namespace Discord
private string _description { get; set; }
private List<SlashCommandOptionBuilder> _options { get; set; }
/// <summary>
/// Build the current builder into a <see cref="SlashCommandCreationProperties"/> class.
/// </summary>
/// <returns>A <see cref="SlashCommandCreationProperties"/> that can be used to create slash commands over rest.</returns>
public SlashCommandCreationProperties Build()
{
SlashCommandCreationProperties props = new SlashCommandCreationProperties()
@@ -107,67 +111,75 @@ namespace Discord
}
public SlashCommandBuilder WithName(string Name)
/// <summary>
/// Sets the field name.
/// </summary>
/// <param name="name">The value to set the field name to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SlashCommandBuilder WithName(string name)
{
this.Name = Name;
this.Name = n ame;
return this;
}
/// <summary>
/// Sets the description of the current command.
/// </summary>
/// <param name="D escription">The description of this command.</param>
/// <param name="d escription">The description of this command.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder WithDescription(string D escription)
public SlashCommandBuilder WithDescription(string d escription)
{
this.Description = D escription;
this.Description = d escription;
return this;
}
/// <summary>
/// Adds an option to the current slash command.
/// </summary>
/// <param name="N ame">The name of the option to add.</param>
/// <param name="T ype">The type of this option.</param>
/// <param name="D escription">The description of this option.</param>
/// <param name="R equired">If this option is required for this command.</param>
/// <param name="Default">If this option is the default option.</param>
/// <param name="O ptions">The options of the option to add.</param>
/// <param name="C hoices">The choices of this option.</param>
/// <param name="n ame">The name of the option to add.</param>
/// <param name="t ype">The type of this option.</param>
/// <param name="d escription">The description of this option.</param>
/// <param name="r equired">If this option is required for this command.</param>
/// <param name="is Default">If this option is the default option.</param>
/// <param name="o ptions">The options of the option to add.</param>
/// <param name="c hoices">The choices of this option.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType T ype,
string Description, bool Required = true, bool Default = false, List<SlashCommandOptionBuilder> Options = null, params ApplicationCommandOptionChoiceProperties[] C hoices)
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType t ype,
string description, bool required = true, bool isDefault = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] c hoices)
{
// Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(Name, nameof(N ame));
Preconditions.AtLeast(Name.Length, 3, nameof(N ame));
Preconditions.AtMost(Name.Length, MaxNameLength, nameof(N ame));
Preconditions.NotNullOrEmpty(name, nameof(n ame));
Preconditions.AtLeast(name.Length, 3, nameof(n ame));
Preconditions.AtMost(name.Length, MaxNameLength, nameof(n ame));
// Discord updated the docs, this regex prevents special characters like @!$%( and s p a c e s.. etc,
// https://discord.com/developers/docs/interactions/slash-commands#applicationcommand
if (!Regex.IsMatch(N ame, @"^[\w-]{3,32}$"))
throw new ArgumentException("Command name cannot contian any special characters or whitespaces!", nameof(N ame));
if (!Regex.IsMatch(n ame, @"^[\w-]{3,32}$"))
throw new ArgumentException("Command name cannot contian any special characters or whitespaces!", nameof(n ame));
// same with description
Preconditions.NotNullOrEmpty(Description, nameof(D escription));
Preconditions.AtLeast(Description.Length, 3, nameof(D escription));
Preconditions.AtMost(Description.Length, MaxDescriptionLength, nameof(D escription));
Preconditions.NotNullOrEmpty(description, nameof(d escription));
Preconditions.AtLeast(description.Length, 3, nameof(d escription));
Preconditions.AtMost(description.Length, MaxDescriptionLength, nameof(d escription));
// make sure theres only one option with default set to true
if (Default)
if (is Default)
{
if (this.Options != null)
if (this.Options.Any(x => x.Default))
throw new ArgumentException("There can only be one command option with default set to true!", nameof(Default));
if (this.Options.Any(x => x.Default.HasValue && x.Default.Value ))
throw new ArgumentException("There can only be one command option with default set to true!", nameof(is Default));
}
SlashCommandOptionBuilder option = new SlashCommandOptionBuilder();
option.Name = Name;
option.Description = Description;
option.Required = Required;
option.Default = Default;
option.Options = Options;
option.Choices = Choices != null ? new List<ApplicationCommandOptionChoiceProperties>(Choices) : null;
option.Name = name;
option.Description = description;
option.Required = required;
option.Default = isDefault;
option.Options = options;
option.Type = type;
option.Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null;
return AddOption(option);
}
@@ -175,33 +187,33 @@ namespace Discord
/// <summary>
/// Adds an option to the current slash command.
/// </summary>
/// <param name="N ame">The name of the option to add.</param>
/// <param name="T ype">The type of this option.</param>
/// <param name="D escription">The description of this option.</param>
/// <param name="R equired">If this option is required for this command.</param>
/// <param name="Default">If this option is the default option.</param>
/// <param name="C hoices">The choices of this option.</param>
/// <param name="n ame">The name of the option to add.</param>
/// <param name="t ype">The type of this option.</param>
/// <param name="d escription">The description of this option.</param>
/// <param name="r equired">If this option is required for this command.</param>
/// <param name="is Default">If this option is the default option.</param>
/// <param name="c hoices">The choices of this option.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType T ype,
string Description, bool Required = true, bool Default = false, params ApplicationCommandOptionChoiceProperties[] C hoices)
=> AddOption(Name, Type, Description, Required, Default, null, C hoices);
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType t ype,
string description, bool required = true, bool isDefault = false, params ApplicationCommandOptionChoiceProperties[] c hoices)
=> AddOption(name, type, description, required, isDefault, null, c hoices);
/// <summary>
/// Adds an option to the current slash command.
/// </summary>
/// <param name="N ame">The name of the option to add.</param>
/// <param name="T ype">The type of this option.</param>
/// <param name="D escription">The sescription of this option.</param>
/// <param name="n ame">The name of the option to add.</param>
/// <param name="t ype">The type of this option.</param>
/// <param name="d escription">The sescription of this option.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType Type, string D escription)
=> AddOption(Name, Type, Description, Options: null, C hoices: null);
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type, string d escription)
=> AddOption(name, type, description, options: null, c hoices: null);
/// <summary>
/// Adds an option to this slash command.
/// </summary>
/// <param name="Parameter ">The option to add.</param>
/// <param name="option ">The option to add.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(SlashCommandOptionBuilder O ption)
public SlashCommandBuilder AddOption(SlashCommandOptionBuilder o ption)
{
if (this.Options == null)
this.Options = new List<SlashCommandOptionBuilder>();
@@ -209,32 +221,32 @@ namespace Discord
if (this.Options.Count >= MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(Options), $"Cannot have more than {MaxOptionsCount} options!");
if (O ption == null)
throw new ArgumentNullException(nameof(O ption), "Option cannot be null");
if (o ption == null)
throw new ArgumentNullException(nameof(o ption), "Option cannot be null");
this.Options.Add(O ption);
this.Options.Add(o ption);
return this;
}
/// <summary>
/// Adds a collection of options to the current slash command.
/// </summary>
/// <param name="Parameter ">The collection of options to add.</param>
/// <param name="options ">The collection of options to add.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOptions(params SlashCommandOptionBuilder[] O ptions)
public SlashCommandBuilder AddOptions(params SlashCommandOptionBuilder[] o ptions)
{
if (O ptions == null)
throw new ArgumentNullException(nameof(O ptions), "Options cannot be null!");
if (o ptions == null)
throw new ArgumentNullException(nameof(o ptions), "Options cannot be null!");
if (O ptions.Length == 0)
throw new ArgumentException(nameof(O ptions), "Options cannot be empty!");
if (o ptions.Length == 0)
throw new ArgumentException(nameof(o ptions), "Options cannot be empty!");
if (this.Options == null)
this.Options = new List<SlashCommandOptionBuilder>();
if (this.Options.Count + O ptions.Length > MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(O ptions), $"Cannot have more than {MaxOptionsCount} options!");
if (this.Options.Count + o ptions.Length > MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(o ptions), $"Cannot have more than {MaxOptionsCount} options!");
this.Options.AddRange(O ptions);
this.Options.AddRange(o ptions);
return this;
}
}
@@ -303,7 +315,7 @@ namespace Discord
/// <summary>
/// The first required option for the user to complete. only one option can be default.
/// </summary>
public bool Default { get; set; }
public bool? Default { get; set; }
/// <summary>
/// <see langword="true"/> if this option is required for this command, otherwise <see langword="false"/>.
@@ -369,10 +381,10 @@ namespace Discord
/// <summary>
/// Adds a choice to the current option.
/// </summary>
/// <param name="N ame">The name of the choice.</param>
/// <param name="V alue">The value of the choice.</param>
/// <param name="n ame">The name of the choice.</param>
/// <param name="v alue">The value of the choice.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string Name, int V alue)
public SlashCommandOptionBuilder AddChoice(string name, int v alue)
{
if (Choices == null)
Choices = new List<ApplicationCommandOptionChoiceProperties>();
@@ -380,16 +392,16 @@ namespace Discord
if (Choices.Count >= MaxChoiceCount)
throw new ArgumentOutOfRangeException(nameof(Choices), $"Cannot add more than {MaxChoiceCount} choices!");
if (N ame == null)
throw new ArgumentNullException($"{nameof(N ame)} cannot be null!");
if (n ame == null)
throw new ArgumentNullException($"{nameof(n ame)} cannot be null!");
Preconditions.AtLeast(Name.Length, 1, nameof(N ame));
Preconditions.AtMost(Name.Length, 100, nameof(N ame));
Preconditions.AtLeast(name.Length, 1, nameof(n ame));
Preconditions.AtMost(name.Length, 100, nameof(n ame));
Choices.Add(new ApplicationCommandOptionChoiceProperties()
{
Name = N ame,
Value = V alue
Name = n ame,
Value = v alue
});
return this;
@@ -398,10 +410,10 @@ namespace Discord
/// <summary>
/// Adds a choice to the current option.
/// </summary>
/// <param name="N ame">The name of the choice.</param>
/// <param name="V alue">The value of the choice.</param>
/// <param name="n ame">The name of the choice.</param>
/// <param name="v alue">The value of the choice.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder AddChoice(string Name, string V alue)
public SlashCommandOptionBuilder AddChoice(string name, string v alue)
{
if (Choices == null)
Choices = new List<ApplicationCommandOptionChoiceProperties>();
@@ -409,22 +421,22 @@ namespace Discord
if (Choices.Count >= MaxChoiceCount)
throw new ArgumentOutOfRangeException(nameof(Choices), $"Cannot add more than {MaxChoiceCount} choices!");
if (N ame == null)
throw new ArgumentNullException($"{nameof(N ame)} cannot be null!");
if (n ame == null)
throw new ArgumentNullException($"{nameof(n ame)} cannot be null!");
if (V alue == null)
throw new ArgumentNullException($"{nameof(V alue)} cannot be null!");
if (v alue == null)
throw new ArgumentNullException($"{nameof(v alue)} cannot be null!");
Preconditions.AtLeast(Name.Length, 1, nameof(N ame));
Preconditions.AtMost(Name.Length, 100, nameof(N ame));
Preconditions.AtLeast(name.Length, 1, nameof(n ame));
Preconditions.AtMost(name.Length, 100, nameof(n ame));
Preconditions.AtLeast(Value.Length, 1, nameof(V alue));
Preconditions.AtMost(Value.Length, 100, nameof(V alue));
Preconditions.AtLeast(value.Length, 1, nameof(v alue));
Preconditions.AtMost(value.Length, 100, nameof(v alue));
Choices.Add(new ApplicationCommandOptionChoiceProperties()
{
Name = N ame,
Value = V alue
Name = n ame,
Value = v alue
});
return this;
@@ -433,11 +445,11 @@ namespace Discord
/// <summary>
/// Sets the current builders name.
/// </summary>
/// <param name="N ame">The name to set the current option builder.</param>
/// <param name="n ame">The name to set the current option builder.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithName(string N ame)
public SlashCommandOptionBuilder WithName(string n ame)
{
this.Name = N ame;
this.Name = n ame;
return this;
}
@@ -445,11 +457,11 @@ namespace Discord
/// <summary>
/// Sets the current builders description.
/// </summary>
/// <param name="D escription">The description to set.</param>
/// <param name="d escription">The description to set.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithDescription(string D escription)
public SlashCommandOptionBuilder WithDescription(string d escription)
{
this.Description = D escription;
this.Description = d escription;
return this;
}
@@ -478,11 +490,11 @@ namespace Discord
/// <summary>
/// Sets the current type of this builder.
/// </summary>
/// <param name="T ype">The type to set.</param>
/// <param name="t ype">The type to set.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithType(ApplicationCommandOptionType T ype)
public SlashCommandOptionBuilder WithType(ApplicationCommandOptionType t ype)
{
this.Type = T ype;
this.Type = t ype;
return this;
}
}