Skip to content

Commit ceb549b

Browse files
committed
Fix autocomplete responses with too many items
1 parent 292c887 commit ceb549b

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

tts_commands/src/help.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::{borrow::Cow, fmt::Write};
1+
use std::{borrow::Cow, fmt::Write, ops::ControlFlow};
22

3+
use arrayvec::ArrayVec;
34
use indexmap::IndexMap;
45

56
use self::serenity::CreateEmbed;
@@ -67,35 +68,41 @@ pub async fn autocomplete<'a>(
6768
searching: &'a str,
6869
) -> serenity::CreateAutocompleteResponse<'a> {
6970
fn flatten_commands<'a>(
70-
result: &mut Vec<serenity::AutocompleteChoice<'a>>,
71+
result: &mut ArrayVec<serenity::AutocompleteChoice<'a>, 25>,
7172
commands: &'a [Command],
7273
searching: &str,
73-
) {
74+
) -> ControlFlow<()> {
7475
for command in commands {
7576
if command.owners_only || command.hide_in_help {
7677
continue;
7778
}
7879

7980
if command.subcommands.is_empty() {
8081
if command.qualified_name.starts_with(searching) {
81-
result.push(serenity::AutocompleteChoice::new(
82+
let choice = serenity::AutocompleteChoice::new(
8283
command.qualified_name.as_ref(),
8384
command.qualified_name.as_ref(),
84-
));
85+
);
86+
87+
if result.try_push(choice).is_err() {
88+
return ControlFlow::Break(());
89+
};
8590
}
8691
} else {
87-
flatten_commands(result, &command.subcommands, searching);
92+
flatten_commands(result, &command.subcommands, searching)?;
8893
}
8994
}
95+
96+
ControlFlow::Continue(())
9097
}
9198

9299
let commands = &ctx.framework.options().commands;
93-
let mut result = Vec::with_capacity(commands.len());
100+
let mut result = ArrayVec::<_, 25>::new();
94101

95102
flatten_commands(&mut result, commands, searching);
96103

97104
result.sort_by_cached_key(|a| strsim::levenshtein(&a.name, searching));
98-
serenity::CreateAutocompleteResponse::new().set_choices(result)
105+
serenity::CreateAutocompleteResponse::new().set_choices(result.into_iter().collect::<Vec<_>>())
99106
}
100107

101108
/// Shows TTS Bot's commands and descriptions of them

tts_commands/src/settings/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ async fn voice_autocomplete<'a>(
251251
serenity::CreateAutocompleteResponse::new().set_choices(
252252
voices
253253
.into_iter()
254+
.take(25)
254255
.map(|(_, label, value)| serenity::AutocompleteChoice::new(label, value))
255256
.collect::<Vec<_>>(),
256257
)
@@ -271,6 +272,7 @@ async fn translation_languages_autocomplete<'a>(
271272
serenity::CreateAutocompleteResponse::new().set_choices(
272273
filtered_languages
273274
.into_iter()
275+
.take(25)
274276
.map(|(value, name)| serenity::AutocompleteChoice::new(name, value.as_str()))
275277
.collect::<Vec<_>>(),
276278
)

tts_core/src/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ pub async fn remove_premium(data: &Data, guild_id: serenity::GuildId) -> Result<
3636
Ok(())
3737
}
3838

39-
pub async fn dm_generic<'ctx, 'a>(
40-
ctx: &'ctx serenity::Context,
39+
pub async fn dm_generic(
40+
ctx: &serenity::Context,
4141
author: &serenity::User,
4242
target: serenity::UserId,
4343
mut target_tag: String,
44-
attachment_url: Option<&'a str>,
44+
attachment_url: Option<&str>,
4545
message: &str,
4646
) -> Result<(String, serenity::Embed)> {
4747
let mut embed = serenity::CreateEmbed::default();

0 commit comments

Comments
 (0)