diff options
| author | Jokler <jokler@protonmail.com> | 2020-07-24 17:13:52 +0200 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-07-24 17:13:52 +0200 |
| commit | 4e2e4afbef3eb8a4fdd6c08c8d531090825e6eb6 (patch) | |
| tree | 8563101e5415d552846bfc69c0dd851ff8e35391 /src | |
| parent | 10ef0bc5bc1246f20b46e547eba97f4d36583e05 (diff) | |
| download | pokebot-4e2e4afbef3eb8a4fdd6c08c8d531090825e6eb6.tar.gz pokebot-4e2e4afbef3eb8a4fdd6c08c8d531090825e6eb6.zip | |
Resubscribe to channels when a new one is created
Diffstat (limited to 'src')
| -rw-r--r-- | src/bot/master.rs | 15 | ||||
| -rw-r--r-- | src/bot/music.rs | 20 | ||||
| -rw-r--r-- | src/teamspeak/mod.rs | 24 |
3 files changed, 49 insertions, 10 deletions
diff --git a/src/bot/master.rs b/src/bot/master.rs index 9e69444..4cdb490 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -187,11 +187,18 @@ impl MasterBot { } async fn on_message(&self, message: MusicBotMessage) -> Result<(), AudioPlayerError> { - if let MusicBotMessage::TextMessage(message) = message { - if let MessageTarget::Poke(who) = message.target { - info!("Poked by {}, creating bot for their channel", who); - self.spawn_bot_for(who).await; + match message { + MusicBotMessage::TextMessage(message) => { + if let MessageTarget::Poke(who) = message.target { + info!("Poked by {}, creating bot for their channel", who); + self.spawn_bot_for(who).await; + } + } + MusicBotMessage::ChannelCreated(_) => { + // TODO Only subscribe to one channel + self.teamspeak.subscribe_all(); } + _ => (), } Ok(()) diff --git a/src/bot/music.rs b/src/bot/music.rs index 96cc383..14a73f9 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -13,10 +13,10 @@ use tsclientlib::{data, ChannelId, ClientId, ConnectOptions, Identity, Invoker, use crate::audio_player::{AudioPlayer, AudioPlayerError, PollResult}; use crate::command::Command; +use crate::command::VolumeChange; use crate::playlist::Playlist; use crate::teamspeak as ts; use crate::youtube_dl::AudioMetadata; -use crate::command::VolumeChange; use ts::TeamSpeakConnection; #[derive(Debug)] @@ -53,6 +53,7 @@ pub enum MusicBotMessage { client: ClientId, old_channel: ChannelId, }, + ChannelCreated(ChannelId), ClientDisconnected { id: ClientId, client: data::Client, @@ -175,7 +176,11 @@ impl MusicBot { format!("") }; - self.send_message(&format!("Playing {} {}", ts::underline(&metadata.title), duration)); + self.send_message(&format!( + "Playing {} {}", + ts::underline(&metadata.title), + duration + )); self.set_description(&format!("Currently playing '{}'", metadata.title)); self.player.reset().unwrap(); self.player.set_metadata(metadata).unwrap(); @@ -273,6 +278,10 @@ impl MusicBot { self.with_teamspeak(|ts| ts.set_description(desc)); } + fn subscribe_all(&self) { + self.with_teamspeak(|ts| ts.subscribe_all()); + } + async fn on_text(&self, message: Message) -> Result<(), AudioPlayerError> { let msg = message.text; if msg.starts_with("!") { @@ -310,7 +319,8 @@ impl MusicBot { self.add_audio(url.to_string(), invoker.name).await; } Command::Search { query } => { - self.add_audio(format!("ytsearch:{}", query.join(" ")), invoker.name).await; + self.add_audio(format!("ytsearch:{}", query.join(" ")), invoker.name) + .await; } Command::Pause => { self.player.pause()?; @@ -415,6 +425,10 @@ impl MusicBot { let old_channel = client.channel; self.on_client_left_channel(old_channel); } + MusicBotMessage::ChannelCreated(_) => { + // TODO Only subscribe to one channel + self.subscribe_all(); + } MusicBotMessage::StateChange(state) => { self.on_state(state)?; } diff --git a/src/teamspeak/mod.rs b/src/teamspeak/mod.rs index 4d31f2e..b53d1d0 100644 --- a/src/teamspeak/mod.rs +++ b/src/teamspeak/mod.rs @@ -35,6 +35,15 @@ fn get_message<'a>(event: &Event) -> Option<MusicBotMessage> { invoker: sender.clone(), text: msg.clone(), })), + Event::PropertyAdded { + id: property, + invoker: _, + } => match property { + PropertyId::Channel(id) => { + Some(MusicBotMessage::ChannelCreated(*id)) + } + _ => None, + }, Event::PropertyChanged { id: property, old: from, @@ -188,7 +197,7 @@ impl TeamSpeakConnection { .lock() .to_mut() .get_client(&self.conn.lock().own_client) - .expect("Can get myself") + .expect("can get myself") .set_description(desc) .map_err(|e| error!("Failed to change description: {}", e)), ); @@ -204,16 +213,25 @@ impl TeamSpeakConnection { ); } - pub fn send_message_to_user(&self, id: ClientId, text: &str) { + pub fn send_message_to_user(&self, client: ClientId, text: &str) { tokio::spawn( self.conn .lock() .to_mut() - .send_message(MessageTarget::Client(id), text) + .send_message(MessageTarget::Client(client), text) .map_err(|e| error!("Failed to send message: {}", e)), ); } + pub fn subscribe_all(&self) { + let packet = self.conn.lock().to_mut().server.set_subscribed(true); + tokio::spawn( + self.conn + .send_packet(packet) + .map_err(|e| error!("Failed to send subscribe packet: {}", e)), + ); + } + pub fn disconnect(&self, reason: &str) { let opt = DisconnectOptions::new() .reason(Reason::Clientdisconnect) |
