From 6ed775f2c43a4e6b113c3e3b8149d26dcb182b51 Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Tue, 19 May 2020 22:05:10 +0100 Subject: [PATCH] fix for not playing on disconnecting --- src/main.rs | 66 ++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index ad7d994..e7e640a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,6 @@ use serenity::{ AudioSource, ffmpeg, Handler as VoiceHandler, - pcm, }, }; @@ -48,7 +47,6 @@ use dotenv::dotenv; use tokio::{ fs::File, - io::empty, process::Command, sync::{ Mutex, @@ -58,7 +56,10 @@ use tokio::{ }; use std::{ - collections::HashSet, + collections::{ + HashMap, + HashSet, + }, env, path::Path, sync::Arc, @@ -82,7 +83,7 @@ impl TypeMapKey for VoiceManager { struct VoiceGuilds; impl TypeMapKey for VoiceGuilds { - type Value = Arc>>; + type Value = Arc>>; } static THEME_COLOR: u32 = 0x00e0f3; @@ -590,18 +591,8 @@ SELECT * let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); - match voice_manager.get_mut(guild_id) { - Some(handler) => { - // play sound - play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await; - } - - None => { - // try & join a voice channel - if let Some(handler) = voice_manager.join(guild_id, user_channel) { - play_audio(&mut sound, guild_data, handler, voice_guilds, pool).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; } } @@ -612,7 +603,7 @@ SELECT * } } -async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashSet>, pool: MySqlPool) +async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap>, pool: MySqlPool) -> Result<(), Box> { let audio = handler.play_only(sound.store_sound_source().await?); @@ -626,7 +617,7 @@ async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHand sound.plays += 1; sound.commit(pool).await?; - voice_guilds.insert(GuildId(guild.id)); + voice_guilds.insert(GuildId(guild.id), 1); Ok(()) } @@ -636,7 +627,7 @@ async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHand async fn main() -> Result<(), Box> { dotenv()?; - let voice_guilds = Arc::new(Mutex::new(HashSet::new())); + let voice_guilds = Arc::new(Mutex::new(HashMap::new())); let framework = StandardFramework::new() .configure(|c| c @@ -644,8 +635,6 @@ async fn main() -> Result<(), Box> { let pool = ctx.data.read().await .get::().cloned().expect("Could not get SQLPool from data"); - println!("dynamic_prefix: acquired pool and now about to query"); - match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool).await { Some(guild) => Some(guild.prefix), @@ -688,7 +677,7 @@ async fn main() -> Result<(), Box> { Ok(()) } -async fn disconnect_from_inactive(voice_manager_mutex: Arc>, voice_guilds: Arc>>) { +async fn disconnect_from_inactive(voice_manager_mutex: Arc>, voice_guilds: Arc>>) { loop { time::delay_for(Duration::from_secs(30)).await; @@ -697,21 +686,20 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc CommandResult { let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); - match voice_manager.get_mut(guild_id) { + match voice_manager.join(guild_id, user_channel) { Some(handler) => { - // play sound play_audio(sound, guild_data, handler, voice_guilds, pool).await?; } None => { - // try & join a voice channel - match voice_manager.join(guild_id, user_channel) { - Some(handler) => { - play_audio(sound, guild_data, handler, voice_guilds, pool).await?; - } - - None => { - msg.channel_id.say(&ctx, "Failed to join channel").await?; - } - }; + msg.channel_id.say(&ctx, "Failed to join channel").await?; } - } + }; } None => {