Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Parameters in Slash Commands do not pass attributes in Autocomplete Handlers #3045

Open
3 tasks done
Andrei-Marcu opened this issue Jan 4, 2025 · 1 comment
Open
3 tasks done
Labels

Comments

@Andrei-Marcu
Copy link

Check The Docs

  • I double checked the docs and couldn't find any useful information.

Verify Issue Source

  • I verified the issue was caused by Discord.Net.

Check your intents

  • I double checked that I have the required intents.

Description

When defining a slash command, attributes applied to the parameter are not passed to the Autocomplete Handler.

Steps to Reproduce:

  1. Define a slash command with and apply an attribute to it.
[SlashCommand("test", "Desc")]
public Task Test([CustomAutocomplete("option1", "option2")] string parameter)
  1. Attempt to access the attribute of the parameter within an AutocompleteHandler.
public class CustomAutocompleteAttribute : AutocompleteAttribute
{
    internal readonly string[] _options;

    public CustomAutocompleteAttribute(params string[] options) : base(typeof(CustomAutocompleteHandler))
    {
        _options = options;
    }
}

public class CustomAutocompleteHandler : AutocompleteHandler
{
    public override async Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
    {
        var attribute = (CustomAutocompleteAttribute)parameter.Attributes.Single(a => a is CustomAutocompleteAttribute); //Throws, and Actually parameter.Attributes is empty

        var options = attribute._options;

        //code

        return AutocompletionResult.FromSuccess(/*results*/);
    }
}

Based on what I gathered, I've seen only the Optional Attribute to get passed, either when it is explicitly put or when the parameter is defined with a default like string parameter = ""

Version

3.17.0

Working Version

No response

Logs

nothing extra in logs besides the single assertion throwing

Sample

No response

Packages

not applicable

Environment

  • Windows 10 22H2 x64
  • .NET 8.0
@Andrei-Marcu
Copy link
Author

Based on further testing I found out that certain attributes and their subtypes don't get passed down to AutocompleteHandler, AutocompleteAttribute being one of them.

To get around this, defining a separate attribute to contain the data works, but I find it inelegant:

[SlashCommand("test", "Desc")]
public Task Test([Autocomplete<CustomAutocompleteHandler>,  Custom("option1", "option2")] string parameter)
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public class CustomAttribute : Attribute
{
    internal readonly string[] _options;

    public CustomAttribute(params string[] options)
    {
        _options = options;
    }
}
public class CustomAutocompleteHandler : AutocompleteHandler
{
    public override async Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
    {
        var attribute = (CustomAttribute)parameter.Attributes.Single(a => a is CustomAttribute);
        var options = attribute._options;

        //code

        return AutocompletionResult.FromSuccess(/*results*/);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant