Skip to content

Commit

Permalink
Fix NullReferenceException caused by Options being undefined. (#2549)
Browse files Browse the repository at this point in the history
  • Loading branch information
GedasFX committed Jan 21, 2023
1 parent 4aded83 commit 02d3ce6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,9 @@ public SlashCommandOptionBuilder AddOptions(params SlashCommandOptionBuilder[] o
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)
Expand Down
20 changes: 19 additions & 1 deletion test/Discord.Net.Tests.Unit/CommandBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void BuildSimpleSlashCommand()
ApplicationCommandOptionType.String,
"option1 description",
isRequired: true,
choices: new []
choices: new []
{
new ApplicationCommandOptionChoiceProperties()
{
Expand All @@ -34,4 +34,22 @@ public void BuildSimpleSlashCommand()
.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();
}
}

0 comments on commit 02d3ce6

Please sign in to comment.