Fix autocompletes
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| 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)] | ||||
| 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( | ||||
|     ctx: Context<'_>, | ||||
|     #[description = "Name or ID of sound to favorite"] | ||||
|     #[autocomplete = "autocomplete_sound"] | ||||
|     name: String, | ||||
|     #[description = "Name or ID of sound to favorite"] name: String, | ||||
| ) -> Result<(), Error> { | ||||
|     let sounds = ctx | ||||
|         .data() | ||||
| @@ -61,7 +59,7 @@ pub async fn add_favorite( | ||||
| pub async fn remove_favorite( | ||||
|     ctx: Context<'_>, | ||||
|     #[description = "Name or ID of sound to favorite"] | ||||
|     #[autocomplete = "autocomplete_sound"] | ||||
|     #[autocomplete = "autocomplete_favorite"] | ||||
|     name: String, | ||||
| ) -> Result<(), Error> { | ||||
|     let sounds = ctx | ||||
|   | ||||
| @@ -23,3 +23,19 @@ pub async fn autocomplete_sound( | ||||
|         }) | ||||
|         .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() | ||||
| } | ||||
|   | ||||
| @@ -37,6 +37,11 @@ pub trait SoundCtx { | ||||
|         user_id: U, | ||||
|         guild_id: G, | ||||
|     ) -> 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>( | ||||
|         &self, | ||||
|         user_id: U, | ||||
| @@ -131,7 +136,7 @@ SELECT name, id, public, server_id, uploader_id | ||||
|             SELECT 1 | ||||
|             FROM favorite_sounds | ||||
|             WHERE sound_id = id AND user_id = ? | ||||
|         ), | ||||
|         ) DESC, | ||||
|         server_id = ? DESC, | ||||
|         public = 1 DESC, | ||||
|         rand() | ||||
| @@ -162,7 +167,7 @@ SELECT name, id, public, server_id, uploader_id | ||||
|             SELECT 1 | ||||
|             FROM favorite_sounds | ||||
|             WHERE sound_id = id AND user_id = ? | ||||
|         ), | ||||
|         ) DESC, | ||||
|         server_id = ? DESC, | ||||
|         public = 1 DESC, | ||||
|         rand() | ||||
| @@ -189,18 +194,50 @@ SELECT name, id, public, server_id, uploader_id | ||||
|         guild_id: G, | ||||
|     ) -> Result<Vec<Sound>, sqlx::Error> { | ||||
|         let db_pool = self.database.clone(); | ||||
|         let user_id = user_id.into(); | ||||
|  | ||||
|         sqlx::query_as_unchecked!( | ||||
|             Sound, | ||||
|             " | ||||
| SELECT name, id, public, server_id, uploader_id | ||||
| 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 | ||||
|             ", | ||||
|             query, | ||||
|             user_id.into(), | ||||
|             guild_id.into(), | ||||
|         ) | ||||
|         .fetch_all(&db_pool) | ||||
|         .await | ||||
|   | ||||
		Reference in New Issue
	
	Block a user