summaryrefslogtreecommitdiffstats
path: root/src/bot/music.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bot/music.rs')
-rw-r--r--src/bot/music.rs42
1 files changed, 40 insertions, 2 deletions
diff --git a/src/bot/music.rs b/src/bot/music.rs
index 4d67f88..94e7350 100644
--- a/src/bot/music.rs
+++ b/src/bot/music.rs
@@ -6,9 +6,9 @@ use std::thread;
use log::{debug, info};
use structopt::StructOpt;
use tokio02::sync::mpsc::UnboundedSender;
-use tsclientlib::{ClientId, ConnectOptions, Identity, Invoker, MessageTarget};
+use tsclientlib::{data, ChannelId, ClientId, ConnectOptions, Identity, Invoker, MessageTarget};
-use crate::audio_player::{AudioPlayerError, AudioPlayer, PollResult};
+use crate::audio_player::{AudioPlayer, AudioPlayerError, PollResult};
use crate::command::Command;
use crate::playlist::Playlist;
use crate::teamspeak::TeamSpeakConnection;
@@ -32,6 +32,14 @@ pub enum State {
#[derive(Debug)]
pub enum MusicBotMessage {
TextMessage(Message),
+ ClientChannel {
+ client: ClientId,
+ old_channel: ChannelId,
+ },
+ ClientDisconnected {
+ id: ClientId,
+ client: data::Client,
+ },
StateChange(State),
Quit(String),
}
@@ -179,6 +187,20 @@ impl MusicBot {
}
}
+ fn my_channel(&self) -> ChannelId {
+ self.teamspeak
+ .as_ref()
+ .map(|ts| ts.my_channel())
+ .expect("my_channel needs ts")
+ }
+
+ fn user_count(&self, channel: ChannelId) -> u32 {
+ self.teamspeak
+ .as_ref()
+ .map(|ts| ts.user_count(channel))
+ .expect("user_count needs ts")
+ }
+
fn send_message(&self, text: &str) {
debug!("Sending message to TeamSpeak: {}", text);
@@ -307,6 +329,22 @@ impl MusicBot {
self.on_text(message).await?;
}
}
+ MusicBotMessage::ClientChannel {
+ client: _,
+ old_channel,
+ } => {
+ let my_channel = self.my_channel();
+ if old_channel == my_channel && self.user_count(my_channel) <= 1 {
+ self.quit(String::from("Channel is empty"));
+ }
+ }
+ MusicBotMessage::ClientDisconnected { id: _, client } => {
+ let old_channel = client.channel;
+ let my_channel = self.my_channel();
+ if old_channel == my_channel && self.user_count(my_channel) <= 1 {
+ self.quit(String::from("Channel is empty"));
+ }
+ }
MusicBotMessage::StateChange(state) => {
self.on_state(state)?;
}