From 3905449f5c0691960876482427f127dad3648993 Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Mon, 4 May 2020 01:07:47 +0100 Subject: [PATCH] query for all of a user or guilds sounds --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main.rs b/src/main.rs index 17fbf2b..2635efc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -359,6 +359,34 @@ VALUES (?, ?, ?, 1, ?) None => Err(Box::new(ErrorTypes::InvalidFile)) } } + + async fn get_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result, Box> { + let sounds = sqlx::query_as_unchecked!( + Sound, + " +SELECT * + FROM sounds + WHERE uploader_id = ? + ", + user_id + ).fetch_all(&db_pool).await?; + + Ok(sounds) + } + + async fn get_guild_sounds(guild_id: u64, db_pool: MySqlPool) -> Result, Box> { + let sounds = sqlx::query_as_unchecked!( + Sound, + " +SELECT * + FROM sounds + WHERE server_id = ? + ", + guild_id + ).fetch_all(&db_pool).await?; + + Ok(sounds) + } } struct GuildData { @@ -796,3 +824,20 @@ INSERT INTO roles (guild_id, role) Ok(()) } + +#[command] +async fn list_sounds(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let sounds; + + if args.rest() == "me" { + sounds = Sound::get_user_sounds(*msg.author.id.as_u64(), pool).await?; + } + else { + sounds = Sound::get_guild_sounds(*msg.guild_id.unwrap().as_u64(), pool).await?; + } + + Ok(()) +}