Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NetCord.Services.ApplicationCommands;

/// <summary>
/// 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.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class SlashCommandIgnoreAttribute : Attribute

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this attribute should be named SlashCommandChoiceIgnoreAttribute. SlashCommandIgnoreAttribute is misleading.

{
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public bool TryRead(ReadOnlyMemory<char> input, [MaybeNullWhen(false)] out objec
{
var localizationsProvider = parameter.LocalizationsProvider;

var count = fields.Length;
var filteredFields = fields.Where(f => f.GetCustomAttribute<SlashCommandIgnoreAttribute>() 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);
Comment on lines +27 to +31

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var filteredFields = fields.Where(f => f.GetCustomAttribute<SlashCommandIgnoreAttribute>() 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);
var ignoreAttributeType = typeof(SlashCommandIgnoreAttribute);
var count = fields.Length;
List<ApplicationCommandOptionChoiceProperties> choices = new(count);
for (var i = 0; i < count; i++)
{
var field = fields[i];
if (!field.IsDefined(ignoreAttributeType))
choices.Add(await CreateChoiceAsync(field).ConfigureAwait(false));
}


return choices;

Expand Down
35 changes: 35 additions & 0 deletions Tests/ServicesTest/ChoicesAndAutocompleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationCommandContext>)service.GetCommands().Single();

var parameter = command.Parameters.Single();

Assert.IsNotNull(parameter.ChoicesProvider);

var choices = await parameter.ChoicesProvider.GetChoicesAsync(parameter).ConfigureAwait(false);

Assert.IsNotNull(choices);

var choicesList = choices.ToList();

Assert.AreEqual(2, choicesList.Count);
Assert.AreEqual("Value1", choicesList[0].Name);
Assert.AreEqual("Value3", choicesList[1].Name);
}
}