diff options
Diffstat (limited to 'src/bot/music.rs')
| -rw-r--r-- | src/bot/music.rs | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/bot/music.rs b/src/bot/music.rs index 4de4d4b..75e61de 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -3,12 +3,13 @@ use std::io::BufRead; use std::sync::{Arc, Mutex}; use std::thread; +use humantime; use log::{debug, info}; use structopt::StructOpt; use tokio02::sync::mpsc::UnboundedSender; use tsclientlib::{data, ChannelId, ClientId, ConnectOptions, Identity, Invoker, MessageTarget}; -use crate::audio_player::{AudioPlayer, AudioPlayerError, PollResult}; +use crate::audio_player::{AudioPlayer, AudioPlayerError, PollResult, Seek}; use crate::command::Command; use crate::playlist::Playlist; use crate::teamspeak::TeamSpeakConnection; @@ -21,6 +22,27 @@ pub struct Message { pub text: String, } +fn parse_seek(mut amount: &str) -> Result<Seek, ()> { + let sign = match amount.chars().next() { + Some('+') => 1, + Some('-') => -1, + _ => 0, + }; + let is_relative = sign != 0; + + if is_relative { + amount = &amount[1..]; + } + + let duration = humantime::parse_duration(amount).map_err(|_| ())?; + + match sign { + 1 => Ok(Seek::Positive(duration)), + -1 => Ok(Seek::Negative(duration)), + _ => Ok(Seek::Absolute(duration)), + } +} + #[derive(Debug, PartialEq, Eq)] pub enum State { Playing, @@ -268,6 +290,17 @@ impl MusicBot { Command::Stop => { self.player.reset()?; } + Command::Seek { amount } => { + if let Ok(seek) = parse_seek(&amount) { + if let Ok(time) = self.player.seek(seek) { + self.send_message(&format!("New position: {}", time)); + } else { + self.send_message("Failed to seek"); + } + } else { + info!("Failed to parse seeking command"); + } + } Command::Next => { let playlist = self.playlist.lock().expect("Mutex was not poisoned"); if !playlist.is_empty() { |
