Fix autocompletes

This commit is contained in:
jude 2023-08-22 18:10:54 +01:00
parent fca080253f
commit e6f5db1842
3 changed files with 60 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use log::warn; use log::warn;
use crate::{cmds::autocomplete_sound, models::sound::SoundCtx, Context, Error}; use crate::{cmds::autocomplete_favorite, models::sound::SoundCtx, Context, Error};
#[poise::command(slash_command, rename = "favorites", guild_only = true)] #[poise::command(slash_command, rename = "favorites", guild_only = true)]
pub async fn favorites(_ctx: Context<'_>) -> Result<(), Error> { pub async fn favorites(_ctx: Context<'_>) -> Result<(), Error> {
@ -16,9 +16,7 @@ pub async fn favorites(_ctx: Context<'_>) -> Result<(), Error> {
)] )]
pub async fn add_favorite( pub async fn add_favorite(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Name or ID of sound to favorite"] #[description = "Name or ID of sound to favorite"] name: String,
#[autocomplete = "autocomplete_sound"]
name: String,
) -> Result<(), Error> { ) -> Result<(), Error> {
let sounds = ctx let sounds = ctx
.data() .data()
@ -61,7 +59,7 @@ pub async fn add_favorite(
pub async fn remove_favorite( pub async fn remove_favorite(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Name or ID of sound to favorite"] #[description = "Name or ID of sound to favorite"]
#[autocomplete = "autocomplete_sound"] #[autocomplete = "autocomplete_favorite"]
name: String, name: String,
) -> Result<(), Error> { ) -> Result<(), Error> {
let sounds = ctx let sounds = ctx

View File

@ -23,3 +23,19 @@ pub async fn autocomplete_sound(
}) })
.collect() .collect()
} }
pub async fn autocomplete_favorite(
ctx: Context<'_>,
partial: &str,
) -> Vec<poise::AutocompleteChoice<String>> {
ctx.data()
.autocomplete_favorite_sounds(&partial, ctx.author().id)
.await
.unwrap_or(vec![])
.iter()
.map(|s| poise::AutocompleteChoice {
name: s.name.clone(),
value: s.id.to_string(),
})
.collect()
}

View File

@ -37,6 +37,11 @@ pub trait SoundCtx {
user_id: U, user_id: U,
guild_id: G, guild_id: G,
) -> Result<Vec<Sound>, sqlx::Error>; ) -> Result<Vec<Sound>, sqlx::Error>;
async fn autocomplete_favorite_sounds<U: Into<u64> + Send>(
&self,
query: &str,
user_id: U,
) -> Result<Vec<Sound>, sqlx::Error>;
async fn user_sounds<U: Into<u64> + Send>( async fn user_sounds<U: Into<u64> + Send>(
&self, &self,
user_id: U, user_id: U,
@ -131,7 +136,7 @@ SELECT name, id, public, server_id, uploader_id
SELECT 1 SELECT 1
FROM favorite_sounds FROM favorite_sounds
WHERE sound_id = id AND user_id = ? WHERE sound_id = id AND user_id = ?
), ) DESC,
server_id = ? DESC, server_id = ? DESC,
public = 1 DESC, public = 1 DESC,
rand() rand()
@ -162,7 +167,7 @@ SELECT name, id, public, server_id, uploader_id
SELECT 1 SELECT 1
FROM favorite_sounds FROM favorite_sounds
WHERE sound_id = id AND user_id = ? WHERE sound_id = id AND user_id = ?
), ) DESC,
server_id = ? DESC, server_id = ? DESC,
public = 1 DESC, public = 1 DESC,
rand() rand()
@ -189,18 +194,50 @@ SELECT name, id, public, server_id, uploader_id
guild_id: G, guild_id: G,
) -> Result<Vec<Sound>, sqlx::Error> { ) -> Result<Vec<Sound>, sqlx::Error> {
let db_pool = self.database.clone(); let db_pool = self.database.clone();
let user_id = user_id.into();
sqlx::query_as_unchecked!( sqlx::query_as_unchecked!(
Sound, Sound,
" "
SELECT name, id, public, server_id, uploader_id SELECT name, id, public, server_id, uploader_id
FROM sounds FROM sounds
WHERE name LIKE CONCAT(?, '%') AND (uploader_id = ? OR server_id = ?) WHERE name LIKE CONCAT(?, '%') AND (uploader_id = ? OR server_id = ? OR EXISTS(
SELECT 1
FROM favorite_sounds
WHERE sound_id = id AND user_id = ?
))
LIMIT 25
",
query,
user_id,
guild_id.into(),
user_id,
)
.fetch_all(&db_pool)
.await
}
async fn autocomplete_favorite_sounds<U: Into<u64> + Send>(
&self,
query: &str,
user_id: U,
) -> Result<Vec<Sound>, sqlx::Error> {
let db_pool = self.database.clone();
sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE name LIKE CONCAT(?, '%') AND EXISTS(
SELECT 1
FROM favorite_sounds
WHERE sound_id = id AND user_id = ?
)
LIMIT 25 LIMIT 25
", ",
query, query,
user_id.into(), user_id.into(),
guild_id.into(),
) )
.fetch_all(&db_pool) .fetch_all(&db_pool)
.await .await