1- use futures:: { Stream , StreamExt } ;
21use std:: fmt:: Write as _;
32
43use poise:: serenity_prelude as serenity;
@@ -11,40 +10,34 @@ use crate::{Context, Error};
1110// The first parameter of that function is ApplicationContext or Context, and the second parameter
1211// is a &str of the partial input which the user has typed so far.
1312//
14- // As the return value of autocomplete functions, you can return a Stream, an Iterator, or an
15- // IntoIterator like Vec<T> and [T; N].
16- //
17- // The returned collection type must be a &str/String (or number, if you're implementing
18- // autocomplete on a number type). Wrap the type in serenity::AutocompleteChoice to set a custom label
19- // for each choice which will be displayed in the Discord UI.
20- //
21- // Example function return types (assuming non-number parameter -> autocomplete choices are string):
22- // - `-> impl Stream<String>`
23- // - `-> Vec<String>`
24- // - `-> impl Iterator<String>`
25- // - `-> impl Iterator<&str>`
26- // - `-> impl Iterator<serenity::AutocompleteChoice>
13+ // As the return value of autocomplete functions, you must return `serenity::CreateAutocompleteResponse`.
2714
2815async fn autocomplete_name < ' a > (
2916 _ctx : Context < ' _ > ,
3017 partial : & ' a str ,
31- ) -> impl Stream < Item = String > + ' a {
32- futures:: stream:: iter ( & [ "Amanda" , "Bob" , "Christian" , "Danny" , "Ester" , "Falk" ] )
33- . filter ( move |name| futures:: future:: ready ( name. starts_with ( partial) ) )
34- . map ( |name| name. to_string ( ) )
18+ ) -> serenity:: CreateAutocompleteResponse {
19+ let choices = [ "Amanda" , "Bob" , "Christian" , "Danny" , "Ester" , "Falk" ]
20+ . into_iter ( )
21+ . filter ( move |name| name. starts_with ( partial) )
22+ . map ( serenity:: AutocompleteChoice :: from)
23+ . collect ( ) ;
24+
25+ serenity:: CreateAutocompleteResponse :: new ( ) . set_choices ( choices)
3526}
3627
3728async fn autocomplete_number (
3829 _ctx : Context < ' _ > ,
3930 _partial : & str ,
40- ) -> impl Iterator < Item = serenity:: AutocompleteChoice > {
31+ ) -> serenity:: CreateAutocompleteResponse {
4132 // Dummy choices
42- [ 1_u32 , 2 , 3 , 4 , 5 ] . iter ( ) . map ( |& n| {
33+ let choices = [ 1_u32 , 2 , 3 , 4 , 5 ] . iter ( ) . map ( |& n| {
4334 serenity:: AutocompleteChoice :: new (
4435 format ! ( "{n} (why did discord even give autocomplete choices separate labels)" ) ,
4536 n,
4637 )
47- } )
38+ } ) ;
39+
40+ serenity:: CreateAutocompleteResponse :: new ( ) . set_choices ( choices. collect ( ) )
4841}
4942
5043/// Greet a user. Showcasing autocomplete!
0 commit comments