[Docs] Autocomplete examples (#2288)
* Improved example in int.framework intro * Added example to `autocompletion` * modified example to utilise user's input * added case insensetive matching; mentioned that 25 suggestions is an API limit
This commit is contained in:
@@ -18,6 +18,8 @@ AutocompleteHandlers raise the `AutocompleteHandlerExecuted` event on execution.
|
||||
|
||||
A valid AutocompleteHandlers must inherit [AutocompleteHandler] base type and implement all of its abstract methods.
|
||||
|
||||
[!code-csharp[Autocomplete Command Example](samples/autocompletion/autocomplete-example.cs)]
|
||||
|
||||
### GenerateSuggestionsAsync()
|
||||
|
||||
The Interactions Service uses this method to generate a response of an Autocomplete Interaction.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// you need to add `Autocomplete` attribute before parameter to add autocompletion to it
|
||||
[SlashCommand("command_name", "command_description")]
|
||||
public async Task ExampleCommand([Summary("parameter_name"), Autocomplete(typeof(ExampleAutocompleteHandler))] string parameterWithAutocompletion)
|
||||
=> await RespondAsync($"Your choice: {parameterWithAutocompletion}");
|
||||
|
||||
public class ExampleAutocompleteHandler : AutocompleteHandler
|
||||
{
|
||||
public override async Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
|
||||
{
|
||||
// Create a collection with suggestions for autocomplete
|
||||
IEnumerable<AutocompleteResult> results = new[]
|
||||
{
|
||||
new AutocompleteResult("Name1", "value111"),
|
||||
new AutocompleteResult("Name2", "value2")
|
||||
};
|
||||
|
||||
// max - 25 suggestions at a time (API limit)
|
||||
return AutocompletionResult.FromSuccess(results.Take(25));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,21 @@
|
||||
[AutocompleteCommand("parameter_name", "command_name")]
|
||||
public async Task Autocomplete()
|
||||
{
|
||||
IEnumerable<AutocompleteResult> results;
|
||||
string userInput = (Context.Interaction as SocketAutocompleteInteraction).Data.Current.Value.ToString();
|
||||
|
||||
...
|
||||
IEnumerable<AutocompleteResult> results = new[]
|
||||
{
|
||||
new AutocompleteResult("foo", "foo_value"),
|
||||
new AutocompleteResult("bar", "bar_value"),
|
||||
new AutocompleteResult("baz", "baz_value"),
|
||||
}.Where(x => x.Name.StartsWith(userInput, StringComparison.InvariantCultureIgnoreCase)); // only send suggestions that starts with user's input; use case insensitive matching
|
||||
|
||||
await (Context.Interaction as SocketAutocompleteInteraction).RespondAsync(results);
|
||||
|
||||
// max - 25 suggestions at a time
|
||||
await (Context.Interaction as SocketAutocompleteInteraction).RespondAsync(results.Take(25));
|
||||
}
|
||||
|
||||
// you need to add `Autocomplete` attribute before parameter to add autocompletion to it
|
||||
[SlashCommand("command_name", "command_description")]
|
||||
public async Task ExampleCommand([Summary("parameter_name"), Autocomplete] string parameterWithAutocompletion)
|
||||
=> await RespondAsync($"Your choice: {parameterWithAutocompletion}");
|
||||
Reference in New Issue
Block a user