Null check slash command localizations (#2453)

This commit is contained in:
essoperagma
2022-09-09 10:20:19 +00:00
committed by GitHub
parent 5073afa316
commit 4834b27f90
5 changed files with 87 additions and 31 deletions

View File

@@ -105,6 +105,8 @@ namespace Discord
{
get => _nameLocalizations;
set
{
if (value != null)
{
foreach (var (locale, name) in value)
{
@@ -113,6 +115,8 @@ namespace Discord
EnsureValidOptionName(name);
}
}
_nameLocalizations = value;
}
}
@@ -125,6 +129,8 @@ namespace Discord
{
get => _descriptionLocalizations;
set
{
if (value != null)
{
foreach (var (locale, description) in value)
{
@@ -133,6 +139,8 @@ namespace Discord
EnsureValidOptionDescription(description);
}
}
_descriptionLocalizations = value;
}
}

View File

@@ -54,6 +54,8 @@ namespace Discord
{
get => _nameLocalizations;
set
{
if (value != null)
{
foreach (var (locale, name) in value)
{
@@ -69,6 +71,7 @@ namespace Discord
throw new ArgumentOutOfRangeException(nameof(value), "Name length must at least 1.");
}
}
}
_nameLocalizations = value;
}

View File

@@ -34,6 +34,8 @@ namespace Discord
{
get => _nameLocalizations;
set
{
if (value != null)
{
foreach (var (locale, name) in value)
{
@@ -46,6 +48,8 @@ namespace Discord
if (Type == ApplicationCommandType.Slash && !Regex.IsMatch(name, @"^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$"))
throw new ArgumentException(@"Name must match the regex ^[-_\p{L}\p{N}\p{IsDevanagari}\p{IsThai}]{1,32}$", nameof(name));
}
}
_nameLocalizations = value;
}
}
@@ -57,6 +61,8 @@ namespace Discord
{
get => _descriptionLocalizations;
set
{
if (value != null)
{
foreach (var (locale, description) in value)
{
@@ -66,6 +72,8 @@ namespace Discord
Preconditions.AtLeast(description.Length, 1, nameof(description));
Preconditions.AtMost(description.Length, SlashCommandBuilder.MaxDescriptionLength, nameof(description));
}
}
_descriptionLocalizations = value;
}
}

View File

@@ -907,7 +907,7 @@ namespace Discord
if (descriptionLocalizations is null)
throw new ArgumentNullException(nameof(descriptionLocalizations));
foreach (var (locale, description) in _descriptionLocalizations)
foreach (var (locale, description) in descriptionLocalizations)
{
if(!Regex.IsMatch(locale, @"^\w{2}(?:-\w{2})?$"))
throw new ArgumentException($"Invalid locale: {locale}", nameof(locale));

View File

@@ -0,0 +1,37 @@
using System;
using Discord;
using Xunit;
namespace Discord;
public class CommandBuilderTests
{
[Fact]
public void BuildSimpleSlashCommand()
{
var command = new SlashCommandBuilder()
.WithName("command")
.WithDescription("description")
.AddOption(
"option1",
ApplicationCommandOptionType.String,
"option1 description",
isRequired: true,
choices: new []
{
new ApplicationCommandOptionChoiceProperties()
{
Name = "choice1", Value = "1"
}
})
.AddOptions(new SlashCommandOptionBuilder()
.WithName("option2")
.WithDescription("option2 description")
.WithType(ApplicationCommandOptionType.String)
.WithRequired(true)
.AddChannelType(ChannelType.Text)
.AddChoice("choice1", "1")
.AddChoice("choice2", "2"));
command.Build();
}
}