diff options
| author | Felix Kaaman <tmtu@tmtu.ee> | 2020-02-21 23:24:55 +0200 |
|---|---|---|
| committer | Felix Kaaman <tmtu@tmtu.ee> | 2020-02-21 23:29:22 +0200 |
| commit | d9bfc97b599af4e4e96ae403df8287a533223e32 (patch) | |
| tree | fc2102295e2b3f0fd5db6f4d2ed811a6a0e87ffd /src/bot | |
| parent | 8e708b65365aeb0591aee39d2ffb7c59239ffc3f (diff) | |
| download | pokebot-d9bfc97b599af4e4e96ae403df8287a533223e32.tar.gz pokebot-d9bfc97b599af4e4e96ae403df8287a533223e32.zip | |
Add seeking to audio player
Adds a new "!seek" command which takes in a string that will be parsed
by humantime in addition to a '+' or '-' character that can be prefixed
to determine if it is a relative seek. Without any prefix characters the
seek will be absolute.
Diffstat (limited to 'src/bot')
| -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() { |
