From 3e10829bf2eb2c27b3b50f235ebfd3384f2a470b Mon Sep 17 00:00:00 2001 From: Oleksii Zotov Date: Thu, 16 Jul 2026 02:51:18 +0200 Subject: [PATCH 1/2] Add SlashCommandIgnoreAttribute for hiding enum values from slash commands Closes #330 --- .../SlashCommandIgnoreAttribute.cs | 10 ++++++ .../SlashCommandEnumTypeReader.cs | 5 +-- .../ChoicesAndAutocompleteTests.cs | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 NetCord.Services/ApplicationCommands/SlashCommandIgnoreAttribute.cs diff --git a/NetCord.Services/ApplicationCommands/SlashCommandIgnoreAttribute.cs b/NetCord.Services/ApplicationCommands/SlashCommandIgnoreAttribute.cs new file mode 100644 index 000000000..4339b9dcd --- /dev/null +++ b/NetCord.Services/ApplicationCommands/SlashCommandIgnoreAttribute.cs @@ -0,0 +1,10 @@ +namespace NetCord.Services.ApplicationCommands; + +/// +/// Specifies that an enum value should be ignored and not shown as an option in slash commands. +/// Apply this attribute to enum fields that should not be presented to users as choices. +/// +[AttributeUsage(AttributeTargets.Field)] +public class SlashCommandIgnoreAttribute : Attribute +{ +} diff --git a/NetCord.Services/EnumTypeReaders/SlashCommandEnumTypeReader.cs b/NetCord.Services/EnumTypeReaders/SlashCommandEnumTypeReader.cs index 082c14cbc..3880428ad 100644 --- a/NetCord.Services/EnumTypeReaders/SlashCommandEnumTypeReader.cs +++ b/NetCord.Services/EnumTypeReaders/SlashCommandEnumTypeReader.cs @@ -24,10 +24,11 @@ public bool TryRead(ReadOnlyMemory input, [MaybeNullWhen(false)] out objec { var localizationsProvider = parameter.LocalizationsProvider; - var count = fields.Length; + var filteredFields = fields.Where(f => f.GetCustomAttribute() is null).ToArray(); + var count = filteredFields.Length; var choices = new ApplicationCommandOptionChoiceProperties[count]; for (var i = 0; i < count; i++) - choices[i] = await CreateChoiceAsync(fields[i]).ConfigureAwait(false); + choices[i] = await CreateChoiceAsync(filteredFields[i]).ConfigureAwait(false); return choices; diff --git a/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs b/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs index 2ffdb0df0..11eccd6da 100644 --- a/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs +++ b/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs @@ -274,4 +274,39 @@ public void TestAutocompleteNotSupported() ([SlashCommandParameter(AutocompleteProviderType = typeof(TestAutocompleteProvider))] int i) => { })); }); } + + private enum TestEnumWithIgnoredValues + { + Value1, + [SlashCommandIgnore] + Value2, + Value3, + } + + [TestMethod] + public async Task TestEnumIgnoreAttribute() + { + var service = CreateService(); + + service.AddSlashCommand(new SlashCommandBuilder( + "test", + "Test", + (TestEnumWithIgnoredValues e) => { })); + + var command = (SlashCommandInfo)service.GetCommands().Single(); + + var parameter = command.Parameters.Single(); + + Assert.IsNotNull(parameter.ChoicesProvider); + + var choices = await parameter.ChoicesProvider.GetChoicesAsync(parameter); + + Assert.IsNotNull(choices); + + var choicesList = choices.ToList(); + + Assert.AreEqual(2, choicesList.Count); + Assert.AreEqual("Value1", choicesList[0].Name); + Assert.AreEqual("Value3", choicesList[1].Name); + } } From b7503f54fb64ef3957a6db2d933a2733a3c9233d Mon Sep 17 00:00:00 2001 From: Oleksii Zotov Date: Thu, 16 Jul 2026 16:07:43 +0200 Subject: [PATCH 2/2] Fix CA2007: Add ConfigureAwait(false) --- Tests/ServicesTest/ChoicesAndAutocompleteTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs b/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs index 11eccd6da..b911e04aa 100644 --- a/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs +++ b/Tests/ServicesTest/ChoicesAndAutocompleteTests.cs @@ -299,7 +299,7 @@ public async Task TestEnumIgnoreAttribute() Assert.IsNotNull(parameter.ChoicesProvider); - var choices = await parameter.ChoicesProvider.GetChoicesAsync(parameter); + var choices = await parameter.ChoicesProvider.GetChoicesAsync(parameter).ConfigureAwait(false); Assert.IsNotNull(choices);