diff options
Diffstat (limited to 'src/bot/master.rs')
| -rw-r--r-- | src/bot/master.rs | 106 |
1 files changed, 64 insertions, 42 deletions
diff --git a/src/bot/master.rs b/src/bot/master.rs index 7a1201f..4cdb490 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -100,21 +100,16 @@ impl MasterBot { (bot, msg_loop) } - fn build_bot_args_for(&self, id: ClientId) -> Option<MusicBotArgs> { - let channel = self - .teamspeak - .channel_of_user(id) - .expect("Can find poke sender"); + fn build_bot_args_for(&self, id: ClientId) -> Result<MusicBotArgs, BotCreationError> { + let channel = match self.teamspeak.channel_of_user(id) { + Some(channel) => channel, + None => return Err(BotCreationError::UnfoundUser), + }; if channel == self.teamspeak.my_channel() { - self.teamspeak.send_message_to_user( - id, - &format!( - "Joining the channel of \"{}\" is not allowed", - self.config.master_name - ), - ); - return None; + return Err(BotCreationError::MasterChannel( + self.config.master_name.clone(), + )); } let MusicBots { @@ -126,15 +121,7 @@ impl MasterBot { for (_, bot) in connected_bots { if bot.my_channel() == channel { - self.teamspeak.send_message_to_user( - id, - &format!( - "\"{}\" is already in this channel. \ - Multiple bots in one channel are not allowed.", - bot.name() - ), - ); - return None; + return Err(BotCreationError::MultipleBots(bot.name().to_owned())); } } @@ -147,9 +134,7 @@ impl MasterBot { let name_index = match available_names.pop() { Some(v) => v, None => { - self.teamspeak - .send_message_to_user(id, "Out of names. Too many bots are already connected!"); - return None; + return Err(BotCreationError::OutOfNames); } }; let name = self.config.names[name_index].clone(); @@ -158,11 +143,7 @@ impl MasterBot { let id_index = match available_ids.pop() { Some(v) => v, None => { - self.teamspeak.send_message_to_user( - id, - "Out of identities. Too many bots are already connected!", - ); - return None; + return Err(BotCreationError::OutOfIdentities); } }; @@ -178,7 +159,7 @@ impl MasterBot { info!("Connecting to {} on {}", channel_path, self.config.address); - Some(MusicBotArgs { + Ok(MusicBotArgs { name, name_index, id_index, @@ -192,22 +173,32 @@ impl MasterBot { } async fn spawn_bot_for(&self, id: ClientId) { - if let Some(bot_args) = self.build_bot_args_for(id) { - let (bot, fut) = MusicBot::new(bot_args).await; - tokio::spawn(fut.unit_error().boxed().compat().map(|_| ())); - let mut music_bots = self.music_bots.write().expect("RwLock was not poisoned"); - music_bots - .connected_bots - .insert(bot.name().to_string(), bot); + match self.build_bot_args_for(id) { + Ok(bot_args) => { + let (bot, fut) = MusicBot::new(bot_args).await; + tokio::spawn(fut.unit_error().boxed().compat().map(|_| ())); + let mut music_bots = self.music_bots.write().expect("RwLock was not poisoned"); + music_bots + .connected_bots + .insert(bot.name().to_string(), bot); + } + Err(e) => self.teamspeak.send_message_to_user(id, &e.to_string()), } } 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(()) @@ -270,6 +261,37 @@ impl MasterBot { } } +#[derive(Debug)] +pub enum BotCreationError { + UnfoundUser, + MasterChannel(String), + MultipleBots(String), + OutOfNames, + OutOfIdentities, +} + +impl std::fmt::Display for BotCreationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use BotCreationError::*; + match self { + UnfoundUser => write!( + f, + "I can't find you in the channel list, \ + either I am not subscribed to your channel or this is a bug.", + ), + MasterChannel(name) => write!(f, "Joining the channel of \"{}\" is not allowed", name), + MultipleBots(name) => write!( + f, + "\"{}\" is already in this channel. \ + Multiple bots in one channel are not allowed.", + name + ), + OutOfNames => write!(f, "Out of names. Too many bots are already connected!"), + OutOfIdentities => write!(f, "Out of identities. Too many bots are already connected!"), + } + } +} + #[derive(Debug, Serialize, Deserialize)] pub struct MasterArgs { #[serde(default = "default_name")] |
