Browse Source

fix: Guarding against empty descriptions in `SlashCommandBuilder`/`SlashCommandOptionBuilder` (#2260)

* adding null/empty check for option-descriptions

* moving check to Preconditions

* docs
tags/3.6.0
Christoph L GitHub 3 years ago
parent
commit
0554ac2442
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 19 deletions
  1. +8
    -19
      src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs
  2. +17
    -0
      src/Discord.Net.Core/Utils/Preconditions.cs

+ 8
- 19
src/Discord.Net.Core/Entities/Interactions/SlashCommands/SlashCommandBuilder.cs View File

@@ -198,21 +198,13 @@ namespace Discord
string description, bool? isRequired = null, bool? isDefault = null, bool isAutocomplete = false, double? minValue = null, double? maxValue = null,
List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices)
{
// Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(name, nameof(name));
Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, MaxNameLength, nameof(name));
Preconditions.Options(name, description);

// 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-]{1,32}$"))
throw new ArgumentException("Command name cannot contain any special characters or whitespaces!", nameof(name));

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

// make sure theres only one option with default set to true
if (isDefault == true && Options?.Any(x => x.IsDefault == true) == true)
throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault));
@@ -248,6 +240,7 @@ namespace Discord
throw new InvalidOperationException($"Cannot have more than {MaxOptionsCount} options!");

Preconditions.NotNull(option, nameof(option));
Preconditions.Options(option.Name, option.Description); // this is a double-check when this method is called via AddOption(string name... )

Options.Add(option);
return this;
@@ -270,6 +263,9 @@ namespace Discord
if (Options.Count + options.Length > MaxOptionsCount)
throw new ArgumentOutOfRangeException(nameof(options), $"Cannot have more than {MaxOptionsCount} options!");

foreach (var option in options)
Preconditions.Options(option.Name, option.Description);

Options.AddRange(options);
return this;
}
@@ -413,7 +409,7 @@ namespace Discord
MinValue = MinValue,
MaxValue = MaxValue
};
}
}

/// <summary>
/// Adds an option to the current slash command.
@@ -434,21 +430,13 @@ namespace Discord
string description, bool? isRequired = null, bool isDefault = false, bool isAutocomplete = false, double? minValue = null, double? maxValue = null,
List<SlashCommandOptionBuilder> options = null, List<ChannelType> channelTypes = null, params ApplicationCommandOptionChoiceProperties[] choices)
{
// Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(name, nameof(name));
Preconditions.AtLeast(name.Length, 1, nameof(name));
Preconditions.AtMost(name.Length, SlashCommandBuilder.MaxNameLength, nameof(name));
Preconditions.Options(name, description);

// 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-]{1,32}$"))
throw new ArgumentException("Command name cannot contain any special characters or whitespaces!", nameof(name));

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

// make sure theres only one option with default set to true
if (isDefault && Options?.Any(x => x.IsDefault == true) == true)
throw new ArgumentException("There can only be one command option with default set to true!", nameof(isDefault));
@@ -483,6 +471,7 @@ namespace Discord
throw new InvalidOperationException($"There can only be {SlashCommandBuilder.MaxOptionsCount} options per sub command group!");

Preconditions.NotNull(option, nameof(option));
Preconditions.Options(option.Name, option.Description); // double check again

Options.Add(option);
return this;


+ 17
- 0
src/Discord.Net.Core/Utils/Preconditions.cs View File

@@ -297,5 +297,22 @@ namespace Discord
}
}
#endregion

#region SlashCommandOptions

/// <exception cref="ArgumentNullException"><paramref name="description"/> or <paramref name="name"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="description"/> or <paramref name="name"/> are either empty or their length exceed limits.</exception>
public static void Options(string name, string description)
{
// Make sure the name matches the requirements from discord
NotNullOrEmpty(name, nameof(name));
NotNullOrEmpty(description, nameof(description));
AtLeast(name.Length, 1, nameof(name));
AtMost(name.Length, SlashCommandBuilder.MaxNameLength, nameof(name));
AtLeast(description.Length, 1, nameof(description));
AtMost(description.Length, SlashCommandBuilder.MaxDescriptionLength, nameof(description));
}

#endregion
}
}

Loading…
Cancel
Save