From 20bd2e9e2f8383fd101c88865d22dea02004280b Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Wed, 18 May 2022 10:52:38 +0300 Subject: [PATCH] [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 --- docs/guides/int_framework/autocompletion.md | 2 ++ .../autocompletion/autocomplete-example.cs | 20 +++++++++++++++++++ .../samples/intro/autocomplete.cs | 18 ++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 docs/guides/int_framework/samples/autocompletion/autocomplete-example.cs diff --git a/docs/guides/int_framework/autocompletion.md b/docs/guides/int_framework/autocompletion.md index 834db2b4f..27da54e36 100644 --- a/docs/guides/int_framework/autocompletion.md +++ b/docs/guides/int_framework/autocompletion.md @@ -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. diff --git a/docs/guides/int_framework/samples/autocompletion/autocomplete-example.cs b/docs/guides/int_framework/samples/autocompletion/autocomplete-example.cs new file mode 100644 index 000000000..30c0697e1 --- /dev/null +++ b/docs/guides/int_framework/samples/autocompletion/autocomplete-example.cs @@ -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 GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services) + { + // Create a collection with suggestions for autocomplete + IEnumerable results = new[] + { + new AutocompleteResult("Name1", "value111"), + new AutocompleteResult("Name2", "value2") + }; + + // max - 25 suggestions at a time (API limit) + return AutocompletionResult.FromSuccess(results.Take(25)); + } +} \ No newline at end of file diff --git a/docs/guides/int_framework/samples/intro/autocomplete.cs b/docs/guides/int_framework/samples/intro/autocomplete.cs index f93c56eaa..11de489f1 100644 --- a/docs/guides/int_framework/samples/intro/autocomplete.cs +++ b/docs/guides/int_framework/samples/intro/autocomplete.cs @@ -1,9 +1,21 @@ [AutocompleteCommand("parameter_name", "command_name")] public async Task Autocomplete() { - IEnumerable results; + string userInput = (Context.Interaction as SocketAutocompleteInteraction).Data.Current.Value.ToString(); - ... + IEnumerable 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}"); \ No newline at end of file