diff options
Diffstat (limited to 'src/commands/playback.rs')
| -rw-r--r-- | src/commands/playback.rs | 144 |
1 files changed, 81 insertions, 63 deletions
diff --git a/src/commands/playback.rs b/src/commands/playback.rs index f80ec17..21393a2 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -1,4 +1,7 @@ -use either::{Left, Right}; +use either::{ + Left, + Right, +}; use log::{ debug, error, @@ -7,12 +10,22 @@ use log::{ }; use serenity::{ framework::standard::{ + macros::{ + command, + group, + }, Args, - macros::{command, group}, + CommandError, + CommandResult, }, + futures::TryFutureExt, model::channel::Message, prelude::*, }; +use tap::{ + Conv, + Pipe, +}; use crate::{ audio::{ @@ -21,44 +34,34 @@ use crate::{ PlayQueue, VoiceManager, }, - Result, - CONFIG, - util::CtxExt, commands::sound_levels::*, + util, + CONFIG, }; -group!({ - name: "playback", - options: { - only_in: "guild", - }, - commands: [ - skip, - pause, - resume, - list, - die, - mute, - unmute, - play, - volume, - ], -}); +#[group] +#[commands(skip, pause, resume, list, die, mute, unmute, play, volume)] +#[only_in(guild)] +struct Playback; -pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { - use url::{Url, Host}; +pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { + use url::{ + Host, + Url, + }; debug!("playing '{}'", url); if !url.starts_with("http") { warn!("got bad url argument to play: {}", url); - ctx.send(msg.channel_id, "bAD LiNk", msg.tts)?; + util::send(ctx, msg.channel_id, "bAD LiNk", msg.tts).await?; return Ok(()); } let url = match Url::parse(url) { Err(e) => { error!("bad url: {}", e); - return ctx.send(msg.channel_id, "INVALID URL", msg.tts); + util::send(ctx, msg.channel_id, "INVALID URL", msg.tts).await?; + return Ok(()); }, Ok(u) => u, }; @@ -71,10 +74,10 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { if host.map(|h| h.to_lowercase().contains("imgur")).unwrap_or(false) { info!("detected imgur link"); - if msg.author.id.0 == 106160362109272064 { - ctx.send(msg.channel_id, "fuck you conway", true)?; + if msg.author.id.get() == 106160362109272064 { + util::send(ctx, msg.channel_id, "fuck you conway", true).await?; } else { - ctx.send(msg.channel_id, "IMGUR IS BAD, YOU TRASH CAN MAN", msg.tts)?; + util::send(ctx, msg.channel_id, "IMGUR IS BAD, YOU TRASH CAN MAN", msg.tts).await?; } return Ok(()); @@ -82,12 +85,12 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { let (start, end) = parse_times(&msg.content); - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); + let queue_lock = ctx.data.write().await.get::<PlayQueue>().cloned().unwrap(); let mut play_queue = queue_lock.write().unwrap(); - play_queue.general_queue.push_back(PlayArgs{ + play_queue.general_queue.push_back(PlayArgs { initiator: msg.author.name.clone(), - data: Left(url.into_string()), + data: Left(url.conv::<String>()), sender_channel: msg.channel_id, start, end, @@ -97,16 +100,18 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { } #[command] -pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { if args.len() == 0 { - return _resume(ctx, msg); + return _resume(ctx, msg).await; } let url = match args.single::<String>() { Ok(url) => url, Err(e) => { error!("unable to parse url from args: {}", e); - return ctx.send(msg.channel_id, "BAD LINK", msg.tts); + return util::send(ctx, msg.channel_id, "BAD LINK", msg.tts) + .await + .map_err(CommandError::from); }, }; @@ -114,16 +119,16 @@ pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { } #[command] -pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); +pub async fn pause(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + let queue_lock = ctx.data.write().await.get::<PlayQueue>().cloned().unwrap(); - let done = || ctx.send(msg.channel_id, "r u srs", msg.tts); + let done = || util::send(ctx, msg.channel_id, "r u srs", msg.tts).map_err(CommandError::from); let playing = { let play_queue = queue_lock.read().unwrap(); let current_item = match play_queue.playing { Some(ref x) => x, - None => return done(), + None => return done().await, }; let audio = current_item.audio.lock(); @@ -131,7 +136,7 @@ pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { }; if !playing { - return done(); + return done().await; } { @@ -147,21 +152,21 @@ pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { #[command] #[aliases("continue")] -pub fn resume(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { - _resume(ctx, msg) +pub async fn resume(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + _resume(ctx, msg).await } -fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> { - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); +async fn _resume(ctx: &Context, msg: &Message) -> CommandResult { + let queue_lock = ctx.data.write().await.get::<PlayQueue>().cloned().unwrap(); - let done = || ctx.send(msg.channel_id, "r u srs", msg.tts); + let done = || util::send(ctx, msg.channel_id, "r u srs", msg.tts).map_err(CommandError::from); let playing = { let play_queue = queue_lock.read().unwrap(); let current_item = match play_queue.playing { Some(ref x) => x, None => { - done()?; + done().await?; return Ok(()); }, }; @@ -171,7 +176,7 @@ fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> { }; if playing { - done()?; + done().await?; debug!("attempted to resume playback while sound was already playing"); return Ok(()); } @@ -188,8 +193,8 @@ fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> { #[command] #[aliases("next")] -pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> Result<()> { - let data = ctx.data.write(); +pub async fn skip(ctx: &Context, _msg: &Message, _args: Args) -> CommandResult { + let data = ctx.data.write().await; let mgr_lock = data.get::<VoiceManager>().cloned().unwrap(); let mut manager = mgr_lock.lock(); @@ -210,8 +215,8 @@ pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> Result<()> { #[command] #[aliases("sudoku", "fuckoff", "stop")] -pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { - let data = ctx.data.write(); +pub async fn die(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + let data = ctx.data.write().await; let mgr_lock = data.get::<VoiceManager>().cloned().unwrap(); let mut manager = mgr_lock.lock(); @@ -231,7 +236,7 @@ pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { handler.stop(); handler.leave(); } else { - ctx.send(msg.channel_id, "YOU die", msg.tts)?; + util::send(ctx, msg.channel_id, "YOU die", msg.tts).await?; debug!("got die with no handler attached"); } @@ -240,43 +245,56 @@ pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { #[command] #[aliases("queue")] -pub fn list(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); +pub async fn list(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + let queue_lock = ctx.data.write().await.get::<PlayQueue>().cloned().unwrap(); let play_queue = queue_lock.read().unwrap(); - let channel_tmp = msg.channel(&ctx).unwrap().guild().unwrap(); - let channel = channel_tmp.read(); + let channel = msg.channel(&ctx).await.unwrap().guild().unwrap(); info!("listing queue"); match play_queue.playing { Some(ref info) => { let audio = info.audio.lock(); - let status = if audio.playing { "playing" } else { "paused:" }; + let status = if audio.playing { + "playing" + } else { + "paused:" + }; let playing_info = match info.init_args.data { Left(ref url) => format!(" `{}`", url), Right(_) => "memeing".to_owned(), }; - ctx.send(msg.channel_id, &format!("Currently {} {} ({})", status, playing_info, info.init_args.initiator), msg.tts)?; + util::send( + ctx, + msg.channel_id, + &format!("Currently {} {} ({})", status, playing_info, info.init_args.initiator), + msg.tts, + ) + .await?; }, None => { debug!("`list` called with no items in queue"); - ctx.send(msg.channel_id, "Nothing is playing you meme", msg.tts)?; + util::send(ctx, msg.channel_id, "Nothing is playing you meme", msg.tts).await?; return Ok(()); }, } - play_queue.meme_queue.iter() + play_queue + .meme_queue + .iter() .chain(play_queue.general_queue.iter()) - .for_each(|info| { + .pipe(serenity::futures::stream::iter) + .for_each(|info| async move { let playing_info = match info.data { Left(ref url) => format!("`{}`", url), Right(_) => "meme".to_owned(), }; - let _ = channel.say(&ctx, &format!("{} ({})", playing_info, info.initiator)); - }); + let _ = channel.say(&ctx, &format!("{} ({})", playing_info, info.initiator)).await; + }) + .await; Ok(()) } |
