Browse Source

Fix bugs regarding component builder and slash commands. Also fixed mismatched param names in the slash command builder

pull/1923/head
quin lynch 4 years ago
parent
commit
14ba3caa6e
6 changed files with 157 additions and 127 deletions
  1. +34
    -19
      src/Discord.Net.Core/Discord.Net.Core.xml
  2. +7
    -4
      src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs
  3. +103
    -91
      src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs
  4. +2
    -2
      src/Discord.Net.Rest/Discord.Net.Rest.csproj
  5. +1
    -1
      src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs
  6. +10
    -10
      src/Discord.Net/Discord.Net.nuspec

+ 34
- 19
src/Discord.Net.Core/Discord.Net.Core.xml View File

@@ -4886,59 +4886,74 @@
Gets or sets the options for this command.
</summary>
</member>
<member name="M:Discord.SlashCommandBuilder.Build">
<summary>
Build the current builder into a <see cref="T:Discord.SlashCommandCreationProperties"/> class.
</summary>
<returns>A <see cref="T:Discord.SlashCommandCreationProperties"/> that can be used to create slash commands over rest.</returns>
</member>
<member name="M:Discord.SlashCommandBuilder.WithName(System.String)">
<summary>
Sets the field name.
</summary>
<param name="name">The value to set the field name to.</param>
<returns>
The current builder.
</returns>
</member>
<member name="M:Discord.SlashCommandBuilder.WithDescription(System.String)">
<summary>
Sets the description of the current command.
</summary>
<param name="Description">The description of this command.</param>
<param name="description">The description of this command.</param>
<returns>The current builder.</returns>
</member>
<member name="M:Discord.SlashCommandBuilder.AddOption(System.String,Discord.ApplicationCommandOptionType,System.String,System.Boolean,System.Boolean,System.Collections.Generic.List{Discord.SlashCommandOptionBuilder},Discord.ApplicationCommandOptionChoiceProperties[])">
<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="Default">If this option is the default option.</param>
<param name="Options">The options of the option to add.</param>
<param name="Choices">The choices of this option.</param>
<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="options">The options of the option to add.</param>
<param name="choices">The choices of this option.</param>
<returns>The current builder.</returns>
</member>
<member name="M:Discord.SlashCommandBuilder.AddOption(System.String,Discord.ApplicationCommandOptionType,System.String,System.Boolean,System.Boolean,Discord.ApplicationCommandOptionChoiceProperties[])">
<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="Default">If this option is the default option.</param>
<param name="Choices">The choices of this option.</param>
<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>
</member>
<member name="M:Discord.SlashCommandBuilder.AddOption(System.String,Discord.ApplicationCommandOptionType,System.String)">
<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>
<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>
</member>
<member name="M:Discord.SlashCommandBuilder.AddOption(Discord.SlashCommandOptionBuilder)">
<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>
</member>
<member name="M:Discord.SlashCommandBuilder.AddOptions(Discord.SlashCommandOptionBuilder[])">
<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>
</member>
<member name="T:Discord.SlashCommandOptionBuilder">


+ 7
- 4
src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs View File

@@ -102,7 +102,7 @@ namespace Discord
else
{
ActionRowBuilder actionRow = null;
if (_actionRows.Count < row)
if (_actionRows.Count > row)
actionRow = _actionRows.ElementAt(row);
else
{
@@ -167,7 +167,7 @@ namespace Discord
/// <param name="button">The button to add.</param>
/// <param name="row">The row to add the button.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(ButtonBuilder button, int row)
public ComponentBuilder WithButton(ButtonBuilder button, int row = 0)
{
Preconditions.LessThan(row, 5, nameof(row));

@@ -185,7 +185,7 @@ namespace Discord
else
{
ActionRowBuilder actionRow = null;
if(_actionRows.Count < row)
if(_actionRows.Count > row)
actionRow = _actionRows.ElementAt(row);
else
{
@@ -295,7 +295,10 @@ namespace Discord
case ComponentType.ActionRow:
return false;
case ComponentType.Button:
return this.Components.Count < 5;
if (this.Components.Any(x => x.Type == ComponentType.SelectMenu))
return false;
else
return this.Components.Count < 5;
case ComponentType.SelectMenu:
return this.Components.Count == 0;
default:


+ 103
- 91
src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs View File

@@ -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 = name;
return this;
}

/// <summary>
/// Sets the description of the current command.
/// </summary>
/// <param name="Description">The description of this command.</param>
/// <param name="description">The description of this command.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder WithDescription(string Description)
public SlashCommandBuilder WithDescription(string description)
{
this.Description = Description;
this.Description = description;
return this;
}

/// <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="Default">If this option is the default option.</param>
/// <param name="Options">The options of the option to add.</param>
/// <param name="Choices">The choices of this option.</param>
/// <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="options">The options of the option to add.</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 Default = false, List<SlashCommandOptionBuilder> Options = null, params ApplicationCommandOptionChoiceProperties[] Choices)
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type,
string description, bool required = true, bool isDefault = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
{
// Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(Name, nameof(Name));
Preconditions.AtLeast(Name.Length, 3, nameof(Name));
Preconditions.AtMost(Name.Length, MaxNameLength, nameof(Name));
Preconditions.NotNullOrEmpty(name, nameof(name));
Preconditions.AtLeast(name.Length, 3, nameof(name));
Preconditions.AtMost(name.Length, MaxNameLength, nameof(name));

// 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(Name, @"^[\w-]{3,32}$"))
throw new ArgumentException("Command name cannot contian any special characters or whitespaces!", nameof(Name));
if (!Regex.IsMatch(name, @"^[\w-]{3,32}$"))
throw new ArgumentException("Command name cannot contian any special characters or whitespaces!", nameof(name));

// same with description
Preconditions.NotNullOrEmpty(Description, nameof(Description));
Preconditions.AtLeast(Description.Length, 3, nameof(Description));
Preconditions.AtMost(Description.Length, MaxDescriptionLength, nameof(Description));
Preconditions.NotNullOrEmpty(description, nameof(description));
Preconditions.AtLeast(description.Length, 3, nameof(description));
Preconditions.AtMost(description.Length, MaxDescriptionLength, nameof(description));

// make sure theres only one option with default set to true
if (Default)
if (isDefault)
{
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(isDefault));
}

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="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="Default">If this option is the default option.</param>
/// <param name="Choices">The choices of this option.</param>
/// <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 Default = false, params ApplicationCommandOptionChoiceProperties[] Choices)
=> AddOption(Name, Type, Description, Required, Default, null, Choices);
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>
/// <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);
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type, string description)
=> AddOption(name, type, description, options: null, choices: 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 Option)
public SlashCommandBuilder AddOption(SlashCommandOptionBuilder option)
{
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 (Option == null)
throw new ArgumentNullException(nameof(Option), "Option cannot be null");
if (option == null)
throw new ArgumentNullException(nameof(option), "Option cannot be null");

this.Options.Add(Option);
this.Options.Add(option);
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[] Options)
public SlashCommandBuilder AddOptions(params SlashCommandOptionBuilder[] options)
{
if (Options == null)
throw new ArgumentNullException(nameof(Options), "Options cannot be null!");
if (options == null)
throw new ArgumentNullException(nameof(options), "Options cannot be null!");

if (Options.Length == 0)
throw new ArgumentException(nameof(Options), "Options cannot be empty!");
if (options.Length == 0)
throw new ArgumentException(nameof(options), "Options cannot be empty!");

if (this.Options == null)
this.Options = new List<SlashCommandOptionBuilder>();

if (this.Options.Count + Options.Length > MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(Options), $"Cannot have more than {MaxOptionsCount} options!");
if (this.Options.Count + options.Length > MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(options), $"Cannot have more than {MaxOptionsCount} options!");

this.Options.AddRange(Options);
this.Options.AddRange(options);
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="Name">The name of the choice.</param>
/// <param name="Value">The value of the choice.</param>
/// <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, int Value)
public SlashCommandOptionBuilder AddChoice(string name, int value)
{
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 (Name == null)
throw new ArgumentNullException($"{nameof(Name)} cannot be null!");
if (name == null)
throw new ArgumentNullException($"{nameof(name)} cannot be null!");

Preconditions.AtLeast(Name.Length, 1, nameof(Name));
Preconditions.AtMost(Name.Length, 100, nameof(Name));
Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, 100, nameof(name));

Choices.Add(new ApplicationCommandOptionChoiceProperties()
{
Name = Name,
Value = Value
Name = name,
Value = value
});

return this;
@@ -398,10 +410,10 @@ namespace Discord
/// <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>
/// <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)
public SlashCommandOptionBuilder AddChoice(string name, string value)
{
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 (Name == null)
throw new ArgumentNullException($"{nameof(Name)} cannot be null!");
if (name == null)
throw new ArgumentNullException($"{nameof(name)} cannot be null!");

if (Value == null)
throw new ArgumentNullException($"{nameof(Value)} cannot be null!");
if (value == null)
throw new ArgumentNullException($"{nameof(value)} cannot be null!");

Preconditions.AtLeast(Name.Length, 1, nameof(Name));
Preconditions.AtMost(Name.Length, 100, nameof(Name));
Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, 100, nameof(name));

Preconditions.AtLeast(Value.Length, 1, nameof(Value));
Preconditions.AtMost(Value.Length, 100, nameof(Value));
Preconditions.AtLeast(value.Length, 1, nameof(value));
Preconditions.AtMost(value.Length, 100, nameof(value));

Choices.Add(new ApplicationCommandOptionChoiceProperties()
{
Name = Name,
Value = Value
Name = name,
Value = value
});

return this;
@@ -433,11 +445,11 @@ namespace Discord
/// <summary>
/// Sets the current builders name.
/// </summary>
/// <param name="Name">The name to set the current option builder.</param>
/// <param name="name">The name to set the current option builder.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithName(string Name)
public SlashCommandOptionBuilder WithName(string name)
{
this.Name = Name;
this.Name = name;

return this;
}
@@ -445,11 +457,11 @@ namespace Discord
/// <summary>
/// Sets the current builders description.
/// </summary>
/// <param name="Description">The description to set.</param>
/// <param name="description">The description to set.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithDescription(string Description)
public SlashCommandOptionBuilder WithDescription(string description)
{
this.Description = Description;
this.Description = description;
return this;
}

@@ -478,11 +490,11 @@ namespace Discord
/// <summary>
/// Sets the current type of this builder.
/// </summary>
/// <param name="Type">The type to set.</param>
/// <param name="type">The type to set.</param>
/// <returns>The current builder.</returns>
public SlashCommandOptionBuilder WithType(ApplicationCommandOptionType Type)
public SlashCommandOptionBuilder WithType(ApplicationCommandOptionType type)
{
this.Type = Type;
this.Type = type;
return this;
}
}


+ 2
- 2
src/Discord.Net.Rest/Discord.Net.Rest.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../Discord.Net.targets" />
<Import Project="../../StyleAnalyzer.targets" />
<PropertyGroup>
@@ -9,7 +9,7 @@
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks>
<PackageIcon>Temporary.png</PackageIcon>
<PackageProjectUrl>https://github.com/Discord-Net-Labs/Discord.Net-Labs</PackageProjectUrl>
<Version>2.3.9-pre</Version>
<Version>2.3.9-dev</Version>
<PackageId>Discord.Net.Labs.Rest</PackageId>
<RepositoryUrl>https://github.com/Discord-Net-Labs/Discord.Net-Labs</RepositoryUrl>
<AssemblyVersion>2.3.4</AssemblyVersion>


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs View File

@@ -42,7 +42,7 @@ namespace Discord.WebSocket
internal override void Update(Model model)
{
var data = model.Data.IsSpecified ?
(model.Data.Value as JToken).ToObject<DataModel>(Discord._serializer)
(DataModel)model.Data.Value
: null;

this.Data.Update(data);


+ 10
- 10
src/Discord.Net/Discord.Net.nuspec View File

@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Discord.Net.Labs</id>
<version>2.3.7$suffix$</version>
<version>2.3.8-dev$suffix$</version>
<title>Discord.Net Labs</title>
<authors>Discord.Net Contributors</authors>
<owners>quinchs</owners>
@@ -14,23 +14,23 @@
<iconUrl>https://avatars.githubusercontent.com/u/84047264</iconUrl>
<dependencies>
<group targetFramework="net461">
<dependency id="Discord.Net.Labs.Core" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Core" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Commands" version="2.3.4$suffix$" />
<dependency id="Discord.Net.Labs.Webhook" version="2.3.3$suffix$" />
</group>
<group targetFramework="netstandard2.0">
<dependency id="Discord.Net.Labs.Core" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Core" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Commands" version="2.3.4$suffix$" />
<dependency id="Discord.Net.Labs.Webhook" version="2.3.3$suffix$" />
</group>
<group targetFramework="netstandard2.1">
<dependency id="Discord.Net.Labs.Core" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.8$suffix$" />
<dependency id="Discord.Net.Labs.Core" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Rest" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.WebSocket" version="2.3.9-dev$suffix$" />
<dependency id="Discord.Net.Labs.Commands" version="2.3.4$suffix$" />
<dependency id="Discord.Net.Labs.Webhook" version="2.3.3$suffix$" />
</group>


Loading…
Cancel
Save