summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-01-15 19:50:18 +0100
committerGitHub <noreply@github.com>2020-01-15 19:50:18 +0100
commit22b225e63ae5add1fad96506989f61c9c29d59e4 (patch)
tree40a7cab36fb395ef191df42c33bcdebf45d2ea6e
parent7e3ef6868ec138992ca22e96539acf385afb8a1c (diff)
downloadpokebot-22b225e63ae5add1fad96506989f61c9c29d59e4.tar.gz
pokebot-22b225e63ae5add1fad96506989f61c9c29d59e4.zip
Use structopt for command parsing (#7)
-rw-r--r--src/command.rs25
-rw-r--r--src/main.rs96
2 files changed, 75 insertions, 46 deletions
diff --git a/src/command.rs b/src/command.rs
new file mode 100644
index 0000000..283a47e
--- /dev/null
+++ b/src/command.rs
@@ -0,0 +1,25 @@
+use structopt::clap::AppSettings::*;
+use structopt::StructOpt;
+
+#[derive(StructOpt, Debug)]
+#[structopt(
+ rename_all = "kebab-case",
+ template = "Try one of these commands:\n{subcommands}",
+ raw(global_settings = "&[VersionlessSubcommands, ColorNever]",)
+)]
+pub enum Command {
+ /// Adds url to playlist
+ Add { url: String },
+ /// Starts audio playback
+ Play,
+ /// Pauses audio playback
+ Pause,
+ /// Stops audio playback
+ Stop,
+ /// Switches to the next queue entry
+ Next,
+ /// Clears the playback queue
+ Clear,
+ /// Changes the volume to the specified value
+ Volume { percent: f64 },
+}
diff --git a/src/main.rs b/src/main.rs
index 93f2fd7..0ba8e34 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,5 @@
use std::io::{BufRead, Read};
use std::path::PathBuf;
-use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::thread;
@@ -12,6 +11,7 @@ use tokio02::sync::mpsc::UnboundedSender;
use tsclientlib::{ClientId, ConnectOptions, Identity, Invoker, MessageTarget};
mod audio_player;
+mod command;
mod playlist;
mod teamspeak;
mod youtube_dl;
@@ -20,6 +20,7 @@ use audio_player::*;
use playlist::*;
use teamspeak::*;
use youtube_dl::AudioMetadata;
+use command::Command;
#[derive(StructOpt, Debug)]
#[structopt(raw(global_settings = "&[AppSettings::ColoredHelp]"))]
@@ -169,60 +170,63 @@ impl Application {
async fn on_text(&self, message: Message) -> Result<(), AudioPlayerError> {
let msg = message.text;
- if msg.starts_with("!") {
- let tokens = msg[1..].split_whitespace().collect::<Vec<_>>();
+ if msg.starts_with("poke") {
+ let tokens = msg.split_whitespace().collect::<Vec<_>>();
+
+ let args = Command::from_iter_safe(&tokens);
+ match args {
+ Ok(v) => self.on_command(v).await?,
+ Err(e) => self.send_message(&format!("\n{}", e.message)),
+ };
+ }
- match tokens.get(0).map(|t| *t) {
- Some("add") => {
- if let Some(url) = &tokens.get(1) {
- // strip bbcode tags from url
- let url = url.replace("[URL]", "").replace("[/URL]", "");
+ Ok(())
+ }
- self.add_audio(url.to_string()).await;
- }
- }
- Some("play") => {
- let playlist = self.playlist.lock().expect("Mutex was not poisoned");
+ async fn on_command(&self, command: Command) -> Result<(), AudioPlayerError> {
+ match command {
+ Command::Play => {
+ let playlist = self.playlist.lock().expect("Mutex was not poisoned");
- if !self.player.is_started() {
- if !playlist.is_empty() {
- self.player.stop_current();
- }
- } else {
- self.player.play()?;
- }
- }
- Some("pause") => {
- self.player.pause()?;
- }
- Some("stop") => {
- self.player.reset()?;
- }
- Some("next") => {
- let playlist = self.playlist.lock().expect("Mutex was not poisoned");
+ if !self.player.is_started() {
if !playlist.is_empty() {
- info!("Skipping to next track");
self.player.stop_current();
- } else {
- info!("Playlist empty, cannot skip");
- self.player.reset()?;
}
+ } else {
+ self.player.play()?;
}
- Some("clear") => {
- self.playlist
- .lock()
- .expect("Mutex was not poisoned")
- .clear();
+ }
+ Command::Add { url } => {
+ // strip bbcode tags from url
+ let url = url.replace("[URL]", "").replace("[/URL]", "");
+
+ self.add_audio(url.to_string()).await;
+ }
+ Command::Pause => {
+ self.player.pause()?;
+ }
+ Command::Stop => {
+ self.player.reset()?;
+ }
+ Command::Next => {
+ let playlist = self.playlist.lock().expect("Mutex was not poisoned");
+ if !playlist.is_empty() {
+ info!("Skipping to next track");
+ self.player.stop_current();
+ } else {
+ info!("Playlist empty, cannot skip");
+ self.player.reset()?;
}
- Some("volume") => {
- if let Some(&volume) = &tokens.get(1) {
- if let Ok(volume) = f64::from_str(volume) {
- let volume = volume.max(0.0).min(100.0) * 0.01;
- self.player.set_volume(volume)?;
- }
- }
+ }
+ Command::Clear => {
+ self.playlist
+ .lock()
+ .expect("Mutex was not poisoned")
+ .clear();
}
- _ => {}
+ Command::Volume { percent: volume } => {
+ let volume = volume.max(0.0).min(100.0) * 0.01;
+ self.player.set_volume(volume)?;
}
}