fix for not playing on disconnecting

This commit is contained in:
jude-lafitteIII 2020-05-19 22:05:10 +01:00
parent 296f019bb5
commit 6ed775f2c4

View File

@ -32,7 +32,6 @@ use serenity::{
AudioSource, AudioSource,
ffmpeg, ffmpeg,
Handler as VoiceHandler, Handler as VoiceHandler,
pcm,
}, },
}; };
@ -48,7 +47,6 @@ use dotenv::dotenv;
use tokio::{ use tokio::{
fs::File, fs::File,
io::empty,
process::Command, process::Command,
sync::{ sync::{
Mutex, Mutex,
@ -58,7 +56,10 @@ use tokio::{
}; };
use std::{ use std::{
collections::HashSet, collections::{
HashMap,
HashSet,
},
env, env,
path::Path, path::Path,
sync::Arc, sync::Arc,
@ -82,7 +83,7 @@ impl TypeMapKey for VoiceManager {
struct VoiceGuilds; struct VoiceGuilds;
impl TypeMapKey for VoiceGuilds { impl TypeMapKey for VoiceGuilds {
type Value = Arc<Mutex<HashSet<GuildId>>>; type Value = Arc<Mutex<HashMap<GuildId, u8>>>;
} }
static THEME_COLOR: u32 = 0x00e0f3; 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(); let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap();
match voice_manager.get_mut(guild_id) { if let Some(handler) = voice_manager.join(guild_id, user_channel) {
Some(handler) => { let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
// 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;
}
}
} }
} }
@ -612,7 +603,7 @@ SELECT *
} }
} }
async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashSet<GuildId>>, pool: MySqlPool) async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap<GuildId, u8>>, pool: MySqlPool)
-> Result<(), Box<dyn std::error::Error>> { -> Result<(), Box<dyn std::error::Error>> {
let audio = handler.play_only(sound.store_sound_source().await?); 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.plays += 1;
sound.commit(pool).await?; sound.commit(pool).await?;
voice_guilds.insert(GuildId(guild.id)); voice_guilds.insert(GuildId(guild.id), 1);
Ok(()) Ok(())
} }
@ -636,7 +627,7 @@ async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHand
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv()?; dotenv()?;
let voice_guilds = Arc::new(Mutex::new(HashSet::new())); let voice_guilds = Arc::new(Mutex::new(HashMap::new()));
let framework = StandardFramework::new() let framework = StandardFramework::new()
.configure(|c| c .configure(|c| c
@ -644,8 +635,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .get::<SQLPool>().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 { match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool).await {
Some(guild) => Some(guild.prefix), Some(guild) => Some(guild.prefix),
@ -688,7 +677,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientVoiceManager>>, voice_guilds: Arc<Mutex<HashSet<GuildId>>>) { async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientVoiceManager>>, voice_guilds: Arc<Mutex<HashMap<GuildId, u8>>>) {
loop { loop {
time::delay_for(Duration::from_secs(30)).await; time::delay_for(Duration::from_secs(30)).await;
@ -697,21 +686,20 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientV
let mut to_remove = HashSet::new(); let mut to_remove = HashSet::new();
for guild in voice_guilds_acquired.iter() { for (guild, ticker) in voice_guilds_acquired.iter_mut() {
let manager_opt = voice_manager.get_mut(guild); if *ticker == 0 {
let manager_opt = voice_manager.get_mut(guild);
if let Some(manager) = manager_opt { if let Some(manager) = manager_opt {
let audio = manager.play_returning(pcm(false, empty()));
time::delay_for(Duration::from_millis(10)).await;
if !audio.lock().await.playing {
manager.leave(); manager.leave();
to_remove.insert(guild.clone()); to_remove.insert(guild.clone());
} }
else {
to_remove.insert(guild.clone());
}
} }
else { else {
to_remove.insert(guild.clone()); *ticker -= 1;
} }
} }
@ -769,25 +757,15 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); 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) => { Some(handler) => {
// play sound
play_audio(sound, guild_data, handler, voice_guilds, pool).await?; play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
} }
None => { None => {
// try & join a voice channel msg.channel_id.say(&ctx, "Failed to join channel").await?;
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?;
}
};
} }
} };
} }
None => { None => {