diff --git a/Cargo.lock b/Cargo.lock index b6c9b6b..2f5519e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -695,9 +695,9 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "http" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" +checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11" dependencies = [ "bytes 1.0.1", "fnv", @@ -717,9 +717,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.3.5" +version = "1.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" +checksum = "bc35c995b9d93ec174cf9a27d425c7892722101e14993cd227fdb51d70cf9589" [[package]] name = "httpdate" @@ -888,9 +888,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" +checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" [[package]] name = "libm" @@ -2016,9 +2016,9 @@ checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" +checksum = "48fe99c6bd8b1cc636890bcc071842de909d902c81ac7dab53ba33c421ab8ffb" dependencies = [ "proc-macro2", "quote", @@ -2366,9 +2366,9 @@ dependencies = [ [[package]] name = "unicode-bidi" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0" dependencies = [ "matches", ] diff --git a/src/main.rs b/src/main.rs index 7936b06..2ce7a7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ use dotenv::dotenv; use crate::{framework::RegexFramework, guild_data::CtxGuildData}; use dashmap::DashMap; +use serenity::model::channel::Channel; use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration}; use tokio::sync::{MutexGuard, RwLock}; @@ -141,69 +142,80 @@ impl EventHandler for Handler { old: Option, new: VoiceState, ) { - if old.is_none() { - if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { - if let Some(guild) = ctx.cache.guild(guild_id).await { - let pool = ctx - .data - .read() - .await - .get::() - .cloned() - .expect("Could not get SQLPool from data"); + if let Some(past_state) = old { + if let (Some(guild_id), None) = (guild_id_opt, new.channel_id) { + if let Some(channel_id) = past_state.channel_id { + if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx).await + { + if channel.members(&ctx).await.map(|m| m.len()).unwrap_or(0) <= 1 { + let songbird = songbird::get(&ctx).await.unwrap(); - let guild_data_opt = ctx.get_from_id(guild.id).await; - - if let Ok(guild_data) = guild_data_opt { - let volume; - let allowed_greets; - - { - let read = guild_data.read().await; - - volume = read.volume; - allowed_greets = read.allow_greets; + let _ = songbird.remove(guild_id).await; } + } + } + } + } else if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { + if let Some(guild) = ctx.cache.guild(guild_id).await { + let pool = ctx + .data + .read() + .await + .get::() + .cloned() + .expect("Could not get SQLPool from data"); - if allowed_greets { - let join_id_res = sqlx::query!( - " + let guild_data_opt = ctx.get_from_id(guild.id).await; + + if let Ok(guild_data) = guild_data_opt { + let volume; + let allowed_greets; + + { + let read = guild_data.read().await; + + volume = read.volume; + allowed_greets = read.allow_greets; + } + + if allowed_greets { + 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; + new.user_id.as_u64() + ) + .fetch_one(&pool) + .await; - if let Ok(join_id_record) = join_id_res { - let join_id = join_id_record.join_sound_id; + if let Ok(join_id_record) = join_id_res { + let join_id = join_id_record.join_sound_id; - let mut sound = sqlx::query_as_unchecked!( - Sound, - " + 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(); + join_id + ) + .fetch_one(&pool) + .await + .unwrap(); - let (handler, _) = join_channel(&ctx, guild, user_channel).await; + let (handler, _) = join_channel(&ctx, guild, user_channel).await; - let _ = play_audio( - &mut sound, - volume, - &mut handler.lock().await, - pool, - false, - ) - .await; - } + let _ = play_audio( + &mut sound, + volume, + &mut handler.lock().await, + pool, + false, + ) + .await; } } }