From ceb549bcd318cf7e37df454b72c33ee33dbcdffd Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Sat, 16 Nov 2024 15:01:54 +0000 Subject: [PATCH] Fix autocomplete responses with too many items --- tts_commands/src/help.rs | 23 +++++++++++++++-------- tts_commands/src/settings/mod.rs | 2 ++ tts_core/src/common.rs | 6 +++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tts_commands/src/help.rs b/tts_commands/src/help.rs index c0d5866..57edaa4 100644 --- a/tts_commands/src/help.rs +++ b/tts_commands/src/help.rs @@ -1,5 +1,6 @@ -use std::{borrow::Cow, fmt::Write}; +use std::{borrow::Cow, fmt::Write, ops::ControlFlow}; +use arrayvec::ArrayVec; use indexmap::IndexMap; use self::serenity::CreateEmbed; @@ -67,10 +68,10 @@ pub async fn autocomplete<'a>( searching: &'a str, ) -> serenity::CreateAutocompleteResponse<'a> { fn flatten_commands<'a>( - result: &mut Vec>, + result: &mut ArrayVec, 25>, commands: &'a [Command], searching: &str, - ) { + ) -> ControlFlow<()> { for command in commands { if command.owners_only || command.hide_in_help { continue; @@ -78,24 +79,30 @@ pub async fn autocomplete<'a>( if command.subcommands.is_empty() { if command.qualified_name.starts_with(searching) { - result.push(serenity::AutocompleteChoice::new( + let choice = serenity::AutocompleteChoice::new( command.qualified_name.as_ref(), command.qualified_name.as_ref(), - )); + ); + + if result.try_push(choice).is_err() { + return ControlFlow::Break(()); + }; } } else { - flatten_commands(result, &command.subcommands, searching); + flatten_commands(result, &command.subcommands, searching)?; } } + + ControlFlow::Continue(()) } let commands = &ctx.framework.options().commands; - let mut result = Vec::with_capacity(commands.len()); + let mut result = ArrayVec::<_, 25>::new(); flatten_commands(&mut result, commands, searching); result.sort_by_cached_key(|a| strsim::levenshtein(&a.name, searching)); - serenity::CreateAutocompleteResponse::new().set_choices(result) + serenity::CreateAutocompleteResponse::new().set_choices(result.into_iter().collect::>()) } /// Shows TTS Bot's commands and descriptions of them diff --git a/tts_commands/src/settings/mod.rs b/tts_commands/src/settings/mod.rs index bbe5772..30f4a73 100644 --- a/tts_commands/src/settings/mod.rs +++ b/tts_commands/src/settings/mod.rs @@ -251,6 +251,7 @@ async fn voice_autocomplete<'a>( serenity::CreateAutocompleteResponse::new().set_choices( voices .into_iter() + .take(25) .map(|(_, label, value)| serenity::AutocompleteChoice::new(label, value)) .collect::>(), ) @@ -271,6 +272,7 @@ async fn translation_languages_autocomplete<'a>( serenity::CreateAutocompleteResponse::new().set_choices( filtered_languages .into_iter() + .take(25) .map(|(value, name)| serenity::AutocompleteChoice::new(name, value.as_str())) .collect::>(), ) diff --git a/tts_core/src/common.rs b/tts_core/src/common.rs index 8a80b2a..74c7cc6 100644 --- a/tts_core/src/common.rs +++ b/tts_core/src/common.rs @@ -36,12 +36,12 @@ pub async fn remove_premium(data: &Data, guild_id: serenity::GuildId) -> Result< Ok(()) } -pub async fn dm_generic<'ctx, 'a>( - ctx: &'ctx serenity::Context, +pub async fn dm_generic( + ctx: &serenity::Context, author: &serenity::User, target: serenity::UserId, mut target_tag: String, - attachment_url: Option<&'a str>, + attachment_url: Option<&str>, message: &str, ) -> Result<(String, serenity::Embed)> { let mut embed = serenity::CreateEmbed::default();