Update EnumConverter and ApplicationCommandOption to register localizations (#2651)

This commit is contained in:
Florian Pabst
2023-04-02 22:03:05 +02:00
committed by GitHub
parent a8ae1e9b9f
commit 75ae48830e
2 changed files with 6 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
using Discord.WebSocket; using Discord.WebSocket;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -22,6 +23,7 @@ namespace Discord.Interactions
{ {
var names = Enum.GetNames(typeof(T)); var names = Enum.GetNames(typeof(T));
var members = names.SelectMany(x => typeof(T).GetMember(x)).Where(x => !x.IsDefined(typeof(HideAttribute), true)); var members = names.SelectMany(x => typeof(T).GetMember(x)).Where(x => !x.IsDefined(typeof(HideAttribute), true));
var localizationManager = parameterInfo.Command.Module.CommandService.LocalizationManager;
if (members.Count() <= 25) if (members.Count() <= 25)
{ {
@@ -33,7 +35,8 @@ namespace Discord.Interactions
choices.Add(new ApplicationCommandOptionChoiceProperties choices.Add(new ApplicationCommandOptionChoiceProperties
{ {
Name = displayValue, Name = displayValue,
Value = member.Name Value = member.Name,
NameLocalizations = localizationManager?.GetAllNames(parameterInfo.GetChoicePath(new ParameterChoice(displayValue.ToLower(), member.Name)), LocalizationTarget.Choice) ?? ImmutableDictionary<string, string>.Empty
}); });
} }
properties.Choices = choices; properties.Choices = choices;

View File

@@ -93,7 +93,8 @@ namespace Discord.API
Choices = option.Choices?.Select(x => new ApplicationCommandOptionChoice Choices = option.Choices?.Select(x => new ApplicationCommandOptionChoice
{ {
Name = x.Name, Name = x.Name,
Value = x.Value Value = x.Value,
NameLocalizations = x.NameLocalizations?.ToDictionary() ?? Optional<Dictionary<string, string>>.Unspecified,
}).ToArray() ?? Optional<ApplicationCommandOptionChoice[]>.Unspecified; }).ToArray() ?? Optional<ApplicationCommandOptionChoice[]>.Unspecified;
Options = option.Options?.Select(x => new ApplicationCommandOption(x)).ToArray() ?? Optional<ApplicationCommandOption[]>.Unspecified; Options = option.Options?.Select(x => new ApplicationCommandOption(x)).ToArray() ?? Optional<ApplicationCommandOption[]>.Unspecified;