Skip to content

Commit

Permalink
Fix autocomplete responses with too many items
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 16, 2024
1 parent 292c887 commit ceb549b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
23 changes: 15 additions & 8 deletions tts_commands/src/help.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,35 +68,41 @@ pub async fn autocomplete<'a>(
searching: &'a str,
) -> serenity::CreateAutocompleteResponse<'a> {
fn flatten_commands<'a>(
result: &mut Vec<serenity::AutocompleteChoice<'a>>,
result: &mut ArrayVec<serenity::AutocompleteChoice<'a>, 25>,
commands: &'a [Command],
searching: &str,
) {
) -> ControlFlow<()> {
for command in commands {
if command.owners_only || command.hide_in_help {
continue;
}

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::<Vec<_>>())
}

/// Shows TTS Bot's commands and descriptions of them
Expand Down
2 changes: 2 additions & 0 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
)
Expand All @@ -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::<Vec<_>>(),
)
Expand Down
6 changes: 3 additions & 3 deletions tts_core/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ceb549b

Please sign in to comment.