diff options
| author | Jokler <jokler@protonmail.com> | 2020-01-25 16:30:37 +0100 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-01-29 20:39:27 +0100 |
| commit | 7be0a2f10f0cfeb89b2f498cfae316b35dcb0814 (patch) | |
| tree | 78474e06ce8c84f99569394345f4b9d7387f4b22 | |
| parent | 972adef5626706f9a192ff784ecc22d1a56b1fe4 (diff) | |
| download | pokebot-7be0a2f10f0cfeb89b2f498cfae316b35dcb0814.tar.gz pokebot-7be0a2f10f0cfeb89b2f498cfae316b35dcb0814.zip | |
Fix quit method not stopping audio playback
| -rw-r--r-- | src/audio_player.rs | 1 | ||||
| -rw-r--r-- | src/bot/master.rs | 20 | ||||
| -rw-r--r-- | src/bot/music.rs | 7 |
3 files changed, 21 insertions, 7 deletions
diff --git a/src/audio_player.rs b/src/audio_player.rs index 2ff7c11..97ecfbf 100644 --- a/src/audio_player.rs +++ b/src/audio_player.rs @@ -313,6 +313,7 @@ impl AudioPlayer { MessageView::Application(content) => { if let Some(s) = content.get_structure() { if s.get_name() == "quit" { + self.reset().unwrap(); return PollResult::Quit; } } diff --git a/src/bot/master.rs b/src/bot/master.rs index ce8b25f..641938a 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -1,5 +1,6 @@ use std::future::Future; use std::sync::{Arc, Mutex}; +use std::collections::HashMap; use futures::future::{FutureExt, TryFutureExt}; use futures01::future::Future as Future01; @@ -17,7 +18,7 @@ use crate::bot::{MusicBot, MusicBotMessage, MusicBotArgs}; pub struct MasterBot { config: MasterConfig, teamspeak: Option<Arc<TeamSpeakConnection>>, - connected_bots: Arc<Mutex<Vec<Arc<MusicBot>>>>, + connected_bots: Arc<Mutex<HashMap<String, Arc<MusicBot>>>>, } impl MasterBot { @@ -63,7 +64,7 @@ impl MasterBot { let bot = Arc::new(Self { config, teamspeak: connection, - connected_bots: Arc::new(Mutex::new(Vec::new())), + connected_bots: Arc::new(Mutex::new(HashMap::new())), }); let cbot = bot.clone(); @@ -85,22 +86,31 @@ impl MasterBot { String::from("local") }; - info!("Connecting to {} on {}", channel, self.config.address); let preset = self.config.bots[0].clone(); + let name = format!("{}({})", preset.name, self.config.name); + + let cconnected_bots = self.connected_bots.clone(); + let disconnect_cb = Box::new(move |n| { + let mut bots = cconnected_bots.lock().expect("Mutex was not poisoned"); + bots.remove(&n); + }); + + info!("Connecting to {} on {}", channel, self.config.address); let bot_args = MusicBotArgs { - name: format!("{}({})", preset.name, self.config.name), + name: name.clone(), owner: preset.owner, local: self.config.local, address: self.config.address.clone(), id: preset.id, channel, verbose: self.config.verbose, + disconnect_cb, }; let (app, fut) = MusicBot::new(bot_args).await; tokio::spawn(fut.unit_error().boxed().compat().map(|_| ())); let mut bots = self.connected_bots.lock().expect("Mutex was not poisoned"); - bots.push(app); + bots.insert(name, app); } async fn on_message(&self, message: MusicBotMessage) -> Result<(), AudioPlayerError> { diff --git a/src/bot/music.rs b/src/bot/music.rs index 3677796..4d67f88 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -44,7 +44,6 @@ pub struct MusicBot { state: Arc<Mutex<State>>, } -#[derive(Debug)] pub struct MusicBotArgs { pub name: String, pub owner: Option<ClientId>, @@ -53,6 +52,7 @@ pub struct MusicBotArgs { pub id: Identity, pub channel: String, pub verbose: u8, + pub disconnect_cb: Box<dyn FnMut(String) + Send + Sync>, } impl MusicBot { @@ -104,7 +104,7 @@ impl MusicBot { } let bot = Arc::new(Self { - name: args.name, + name: args.name.clone(), player, teamspeak: connection, playlist, @@ -112,11 +112,14 @@ impl MusicBot { }); let cbot = bot.clone(); + let mut disconnect_cb = args.disconnect_cb; + let name = args.name; let msg_loop = async move { 'outer: loop { while let Some(msg) = rx.recv().await { if let MusicBotMessage::Quit(reason) = msg { cbot.with_teamspeak(|ts| ts.disconnect(&reason)); + disconnect_cb(name); break 'outer; } cbot.on_message(msg).await.unwrap(); |
