From d6d825e86d3e19417974ae401fe2681914890f67 Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Fri, 29 May 2020 14:35:24 +0100 Subject: [PATCH] added command to allow/disallow greet sounds --- src/guilddata.rs | 11 +++--- src/main.rs | 93 ++++++++++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 38 deletions(-) diff --git a/src/guilddata.rs b/src/guilddata.rs index 1474682..a4e2d7e 100644 --- a/src/guilddata.rs +++ b/src/guilddata.rs @@ -6,14 +6,15 @@ pub struct GuildData { pub name: Option, pub prefix: String, pub volume: u8, + pub allow_greets: bool, } impl GuildData { pub async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option { - let guild = sqlx::query_as!( + let guild = sqlx::query_as_unchecked!( GuildData, " -SELECT id, name, prefix, volume +SELECT id, name, prefix, volume, allow_greets FROM servers WHERE id = ? ", guild_id @@ -57,6 +58,7 @@ INSERT INTO servers (id, name) name: Some(guild.name.clone()), prefix: String::from("?"), volume: 100, + allow_greets: true }) } @@ -67,11 +69,12 @@ UPDATE servers SET name = ?, prefix = ?, - volume = ? + volume = ?, + allow_greets = ? WHERE id = ? ", - self.name, self.prefix, self.volume, self.id + self.name, self.prefix, self.volume, self.allow_greets, self.id ) .execute(&db_pool) .await?; diff --git a/src/main.rs b/src/main.rs index 3c39902..42a41a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ struct AllUsers; struct RoleManagedUsers; #[group] -#[commands(change_prefix, set_allowed_roles)] +#[commands(change_prefix, set_allowed_roles, allow_greet_sounds)] #[checks(permission_check)] struct PermissionManagedUsers; @@ -225,54 +225,61 @@ struct Handler; impl EventHandler for Handler { async fn voice_state_update(&self, ctx: Context, guild_id_opt: Option, old: Option, new: VoiceState) { if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { - let pool = ctx.data.read().await - .get::().cloned().expect("Could not get SQLPool from data"); if old.is_none() { - let join_id_res = sqlx::query!( - " -SELECT join_sound_id - FROM users - WHERE user = ? AND join_sound_id IS NOT NULL - ", - new.user_id.as_u64() - ) - .fetch_one(&pool) - .await; + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); - match join_id_res { - Ok(join_id_record) => { - let join_id = join_id_record.join_sound_id; - let mut sound = sqlx::query_as_unchecked!( - Sound, + let guild_data_opt = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await; + + if let Some(guild_data) = guild_data_opt { + if guild_data.allow_greets { + let join_id_res = sqlx::query!( " -SELECT name, id, plays, public, server_id, uploader_id - FROM sounds - WHERE id = ? + SELECT join_sound_id + FROM users + WHERE user = ? AND join_sound_id IS NOT NULL ", - join_id + new.user_id.as_u64() ) .fetch_one(&pool) - .await.unwrap(); + .await; - let voice_manager_lock = ctx.data.read().await - .get::().cloned().expect("Could not get VoiceManager from data"); + match join_id_res { + Ok(join_id_record) => { + let join_id = join_id_record.join_sound_id; + let mut sound = sqlx::query_as_unchecked!( + Sound, + " + SELECT name, id, plays, public, server_id, uploader_id + FROM sounds + WHERE id = ? + ", + join_id + ) + .fetch_one(&pool) + .await.unwrap(); - let mut voice_manager = voice_manager_lock.lock().await; + let voice_manager_lock = ctx.data.read().await + .get::().cloned().expect("Could not get VoiceManager from data"); - let voice_guilds_lock = ctx.data.read().await - .get::().cloned().expect("Could not get VoiceGuilds from data"); + let mut voice_manager = voice_manager_lock.lock().await; - let voice_guilds = voice_guilds_lock.lock().await; + let voice_guilds_lock = ctx.data.read().await + .get::().cloned().expect("Could not get VoiceGuilds from data"); - let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); + let voice_guilds = voice_guilds_lock.lock().await; - if let Some(handler) = voice_manager.join(guild_id, user_channel) { - let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await; + let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); + + if let Some(handler) = voice_manager.join(guild_id, user_channel) { + let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await; + } + } + + Err(_) => {} } } - - Err(_) => {} } } } @@ -1032,3 +1039,21 @@ async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResul Ok(()) } + +#[command("allow_greet")] +async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not acquire SQL pool from data"); + + let guild_data_opt = GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool.clone()).await; + + if let Some(mut guild_data) = guild_data_opt { + guild_data.allow_greets = !guild_data.allow_greets; + + guild_data.commit(pool).await?; + + msg.channel_id.say(&ctx, format!("Greet sounds have been {}abled in this server", if guild_data.allow_greets { "en" } else { "dis" })).await?; + } + + Ok(()) +}