* Display name support for enum type converter
* allow display attribute on enum type converter
* update docs/examples to include enum Display sample
* Revert "allow display attribute on enum type converter"
This reverts commit a0eec5b755.
* adds ChoiceDisplay for enum type converters
* Update EnumChoiceAttribute.cs
* fix renamed folder issue
* fix namespace
Co-authored-by: Xeno <eliotd@gmail.com>
tags/3.4.0
| @@ -1,16 +1,18 @@ | |||||
| [SlashCommand("blep", "Send a random adorable animal photo")] | [SlashCommand("blep", "Send a random adorable animal photo")] | ||||
| public async Task Blep([Choice("Dog", "dog"), Choice("Cat", "cat"), Choice("Penguin", "penguin")] string animal) | |||||
| public async Task Blep([Choice("Dog", "dog"), Choice("Cat", "cat"), Choice("Guinea pig", "GuineaPig")] string animal) | |||||
| { | { | ||||
| ... | ... | ||||
| } | } | ||||
| // In most cases, you can use an enum to replace the seperate choice attributes in a command. | |||||
| // In most cases, you can use an enum to replace the separate choice attributes in a command. | |||||
| public enum Animal | public enum Animal | ||||
| { | { | ||||
| Cat, | Cat, | ||||
| Dog, | Dog, | ||||
| Penguin | |||||
| // You can also use the ChoiceDisplay attribute to change how they appear in the choice menu. | |||||
| [ChoiceDisplay("Guinea pig")] | |||||
| GuineaPig | |||||
| } | } | ||||
| [SlashCommand("blep", "Send a random adorable animal photo")] | [SlashCommand("blep", "Send a random adorable animal photo")] | ||||
| @@ -1,3 +1,11 @@ | |||||
| using Discord.Interactions; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.ComponentModel.DataAnnotations; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace InteractionFramework | namespace InteractionFramework | ||||
| { | { | ||||
| public enum ExampleEnum | public enum ExampleEnum | ||||
| @@ -5,6 +13,8 @@ namespace InteractionFramework | |||||
| First, | First, | ||||
| Second, | Second, | ||||
| Third, | Third, | ||||
| Fourth | |||||
| Fourth, | |||||
| [ChoiceDisplay("Twenty First")] | |||||
| TwentyFirst | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,25 @@ | |||||
| using System; | |||||
| namespace Discord.Interactions | |||||
| { | |||||
| /// <summary> | |||||
| /// Customize the displayed value of a slash command choice enum. Only works with the default enum type converter. | |||||
| /// </summary> | |||||
| [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] | |||||
| public class ChoiceDisplayAttribute : Attribute | |||||
| { | |||||
| /// <summary> | |||||
| /// Gets the name of the parameter. | |||||
| /// </summary> | |||||
| public string Name { get; } = null; | |||||
| /// <summary> | |||||
| /// Modify the default name and description values of a Slash Command parameter. | |||||
| /// </summary> | |||||
| /// <param name="name">Name of the parameter.</param> | |||||
| public ChoiceDisplayAttribute(string name) | |||||
| { | |||||
| Name = name; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -2,6 +2,7 @@ using Discord.WebSocket; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | using System.Linq; | ||||
| using System.Reflection; | |||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| namespace Discord.Interactions | namespace Discord.Interactions | ||||
| @@ -27,12 +28,14 @@ namespace Discord.Interactions | |||||
| var choices = new List<ApplicationCommandOptionChoiceProperties>(); | var choices = new List<ApplicationCommandOptionChoiceProperties>(); | ||||
| foreach (var member in members) | foreach (var member in members) | ||||
| { | |||||
| var displayValue = member.GetCustomAttribute<ChoiceDisplayAttribute>()?.Name ?? member.Name; | |||||
| choices.Add(new ApplicationCommandOptionChoiceProperties | choices.Add(new ApplicationCommandOptionChoiceProperties | ||||
| { | { | ||||
| Name = member.Name, | |||||
| Name = displayValue, | |||||
| Value = member.Name | Value = member.Name | ||||
| }); | }); | ||||
| } | |||||
| properties.Choices = choices; | properties.Choices = choices; | ||||
| } | } | ||||
| } | } | ||||