You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

autocompletion.md 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ---
  2. uid: Guides.IntFw.AutoCompletion
  3. title: Command Autocompletion
  4. ---
  5. # AutocompleteHandlers
  6. [Autocompleters] provide a similar pattern to TypeConverters.
  7. [Autocompleters] are cached, singleton services and they are used by the
  8. Interaction Service to handle Autocomplete Interations targeted to a specific Slash Command parameter.
  9. To start using AutocompleteHandlers, use the `[AutocompleteAttribute(Type type)]` overload of the [AutocompleteAttribute].
  10. This will dynamically link the parameter to the [AutocompleteHandler] type.
  11. AutocompleteHandlers raise the `AutocompleteHandlerExecuted` event on execution. This event can be also used to create a post-execution logic, just like the `*CommandExecuted` events.
  12. ## Creating AutocompleteHandlers
  13. A valid AutocompleteHandlers must inherit [AutocompleteHandler] base type and implement all of its abstract methods.
  14. ### GenerateSuggestionsAsync()
  15. The Interactions Service uses this method to generate a response of an Autocomplete Interaction.
  16. This method should return `AutocompletionResult.FromSuccess(IEnumerable<AutocompleteResult>)` to
  17. display parameter suggestions to the user. If there are no suggestions to be presented to the user, you have two results:
  18. 1. Returning the parameterless `AutocompletionResult.FromSuccess()` will display a "No options match your search." message to the user.
  19. 2. Returning `AutocompleteResult.FromError()` will make the Interaction Service **not** respond to the interaction,
  20. consequently displaying the user a "Loading options failed." message. `AutocompletionResult.FromError()` is solely used for error handling purposes. Discord currently doesn't allow
  21. you to display custom error messages. This result type will be directly returned to the `AutocompleteHandlerExecuted` method.
  22. ## Resolving AutocompleteHandler Dependencies
  23. AutocompleteHandler dependencies are resolved using the same dependency injection
  24. pattern as the Interaction Modules.
  25. Property injection and constructor injection are both valid ways to get service dependencies.
  26. Because [AutocompleterHandlers] are constructed at service startup,
  27. class dependencies are resolved only once.
  28. > [!NOTE]
  29. > If you need to access per-request dependencies you can use the
  30. > IServiceProvider parameter of the `GenerateSuggestionsAsync()` method.
  31. [AutoCompleteHandlers]: xref:Discord.Interactions.AutocompleteHandler
  32. [AutoCompleteHandler]: xref:Discord.Interactions.AutocompleteHandler
  33. [AutoCompleteAttribute]: