Browse Source

Fix NullReferenceException caused by Options being undefined. (#2549)

pull/2572/head
Gediminas Milašius GitHub 2 years ago
parent
commit
02d3ce6e01
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions
  1. +3
    -1
      src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs
  2. +19
    -1
      test/Discord.Net.Tests.Unit/CommandBuilderTests.cs

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

@@ -664,7 +664,9 @@ namespace Discord
if (options == null)
throw new ArgumentNullException(nameof(options), "Options cannot be null!");

if ((Options?.Count ?? 0) + options.Length > SlashCommandBuilder.MaxOptionsCount)
Options ??= new List<SlashCommandOptionBuilder>();

if (Options.Count + options.Length > SlashCommandBuilder.MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(options), $"There can only be {SlashCommandBuilder.MaxOptionsCount} options per sub command group!");

foreach (var option in options)


+ 19
- 1
test/Discord.Net.Tests.Unit/CommandBuilderTests.cs View File

@@ -17,7 +17,7 @@ public class CommandBuilderTests
ApplicationCommandOptionType.String,
"option1 description",
isRequired: true,
choices: new []
choices: new []
{
new ApplicationCommandOptionChoiceProperties()
{
@@ -34,4 +34,22 @@ public class CommandBuilderTests
.AddChoice("choice2", "2"));
command.Build();
}

[Fact]
public void BuildSubSlashCommand()
{
var command = new SlashCommandBuilder()
.WithName("command").WithDescription("Command desc.")
.AddOptions(new SlashCommandOptionBuilder()
.WithType(ApplicationCommandOptionType.SubCommand)
.WithName("subcommand").WithDescription("Subcommand desc.")
.AddOptions(
new SlashCommandOptionBuilder()
.WithType(ApplicationCommandOptionType.String)
.WithName("name1").WithDescription("desc1"),
new SlashCommandOptionBuilder()
.WithType(ApplicationCommandOptionType.String)
.WithName("name2").WithDescription("desc2")));
command.Build();
}
}

Loading…
Cancel
Save