diff options
Diffstat (limited to 'src/commands/playback.rs')
| -rw-r--r-- | src/commands/playback.rs | 153 |
1 files changed, 64 insertions, 89 deletions
diff --git a/src/commands/playback.rs b/src/commands/playback.rs index 1c3ab95..98ae613 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -1,78 +1,46 @@ +use std::sync::Arc; + use log::{ debug, - error, info, warn, }; -use serenity::{ - framework::standard::{ - macros::{ - command, - group, - }, - Args, - CommandError, - CommandResult, - }, - model::channel::Message, - prelude::*, -}; +use serenity::prelude::*; use songbird::{ input::YoutubeDl, Call, Songbird, }; -use std::sync::Arc; -use tap::Conv; use crate::{ bot::HttpKey, - commands::sound_levels::*, util, + PoiseContext, CONFIG, }; -#[group] -#[commands(skip, pause, resume, list, die, mute, unmute, play)] -#[only_in(guild)] -struct Playback; - -pub async fn songbird( - ctx: &Context, - msg: &Message, -) -> Result<(Arc<Songbird>, Arc<Mutex<Call>>), CommandError> { - let Some(gid) = msg.guild_id else { +pub async fn songbird(ctx: PoiseContext<'_>) -> anyhow::Result<(Arc<Songbird>, Arc<Mutex<Call>>)> { + let Some(gid) = ctx.guild_id() else { return Err(anyhow::anyhow!("no guild id").into()); }; - let sb = songbird::get(ctx).await.expect("acquiring songbird handle"); + let sb = songbird::get(ctx.serenity_context()).await.expect("acquiring songbird handle"); let call = sb.get_or_insert(gid); Ok((sb, call)) } -pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { - use url::{ - Host, - Url, - }; +pub async fn _play(ctx: PoiseContext<'_>, url: &url::Url) -> anyhow::Result<()> { + use url::Host; debug!("playing '{}'", url); - if !url.starts_with("http") { + if !url.scheme().starts_with("http") { warn!("got bad url argument to play: {}", url); - util::send(ctx, msg.channel_id, "bAD LiNk", msg.tts).await?; + + util::reply(ctx, "bAD LiNk").await?; return Ok(()); } - let url = match Url::parse(url) { - Err(e) => { - error!("bad url: {}", e); - util::send(ctx, msg.channel_id, "INVALID URL", msg.tts).await?; - return Ok(()); - }, - Ok(u) => u, - }; - let host = url.host().and_then(|u| match u { Host::Domain(h) => Some(h.to_owned()), _ => None, @@ -81,16 +49,16 @@ pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { if host.map(|h| h.to_lowercase().contains("imgur")).unwrap_or(false) { info!("detected imgur link"); - if msg.author.id.get() == 106160362109272064 { - util::send(ctx, msg.channel_id, "fuck you conway", true).await?; + if ctx.author().id == 106160362109272064 { + util::reply(ctx, "fuck you conway").await?; } else { - util::send(ctx, msg.channel_id, "IMGUR IS BAD, YOU TRASH CAN MAN", msg.tts).await?; + util::reply(ctx, "IMGUR IS BAD, YOU TRASH CAN MAN").await?; } return Ok(()); } - let (_sb, call) = songbird(ctx, msg).await?; + let (_sb, call) = songbird(ctx).await?; let mut call = call.lock().await; if call.current_channel().is_none() { @@ -98,38 +66,31 @@ pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { } let client = { - let data = ctx.data.read().await; + let data = ctx.serenity_context().data.read().await; data.get::<HttpKey>().unwrap().clone() }; - let input = YoutubeDl::new_ytdl_like("yt-dlp", client.clone(), url.conv::<String>()); + let input = YoutubeDl::new_ytdl_like("yt-dlp", client.clone(), url.to_string()); call.enqueue_input(input.into()).await; Ok(()) } -#[command] -pub async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { - if args.is_empty() { - 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 util::send(ctx, msg.channel_id, "BAD LINK", msg.tts) - .await - .map_err(CommandError::from); - }, +#[poise::command(slash_command, prefix_command, guild_only, category = "playback")] +pub async fn play( + ctx: PoiseContext<'_>, + #[description = "link to play (if absent, resumes playback)"] u: Option<url::Url>, +) -> anyhow::Result<()> { + let Some(u) = u else { + return _resume(ctx).await; }; - _play(ctx, msg, &url).await + _play(ctx, &u).await } -#[command] -pub async fn pause(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - let (_sb, call) = songbird(ctx, msg).await?; +#[poise::command(slash_command, prefix_command, guild_only, category = "playback")] +pub async fn pause(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let (_sb, call) = songbird(ctx).await?; let call = call.lock().await; call.queue().pause()?; @@ -137,14 +98,19 @@ pub async fn pause(ctx: &Context, msg: &Message, _: Args) -> CommandResult { Ok(()) } -#[command] -#[aliases("continue")] -pub async fn resume(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - _resume(ctx, msg).await +#[poise::command( + slash_command, + prefix_command, + guild_only, + aliases("continue"), + category = "playback" +)] +pub async fn resume(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + _resume(ctx).await } -async fn _resume(ctx: &Context, msg: &Message) -> CommandResult { - let (_sb, call) = songbird(ctx, msg).await?; +async fn _resume(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let (_sb, call) = songbird(ctx).await?; let call = call.lock().await; call.queue().resume()?; @@ -152,10 +118,9 @@ async fn _resume(ctx: &Context, msg: &Message) -> CommandResult { Ok(()) } -#[command] -#[aliases("next")] -pub async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { - let (_sb, call) = songbird(ctx, msg).await?; +#[poise::command(slash_command, prefix_command, guild_only, category = "playback", aliases("next"))] +pub async fn skip(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let (_sb, call) = songbird(ctx).await?; let call = call.lock().await; call.queue().skip()?; @@ -163,10 +128,15 @@ pub async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { Ok(()) } -#[command] -#[aliases("sudoku", "fuckoff", "stop")] -pub async fn die(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - let (_sb, call) = songbird(ctx, msg).await?; +#[poise::command( + slash_command, + prefix_command, + guild_only, + category = "playback", + aliases("sudoku", "fuckoff", "stop") +)] +pub async fn die(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let (_sb, call) = songbird(ctx).await?; let mut call = call.lock().await; call.queue().stop(); @@ -176,21 +146,26 @@ pub async fn die(ctx: &Context, msg: &Message, _: Args) -> CommandResult { Ok(()) } -#[command] -#[aliases("queue")] -pub async fn list(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - let (_sb, call) = songbird(ctx, msg).await?; +#[poise::command( + slash_command, + prefix_command, + guild_only, + category = "playback", + aliases("queue") +)] +pub async fn list(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let (_sb, call) = songbird(ctx).await?; let call = call.lock().await; let queue = call.queue(); - util::send(ctx, msg.channel_id, "(command fix work-in-progress)", msg.tts).await?; + util::reply(ctx, "(command fix work-in-progress)").await?; for track in queue.current_queue().into_iter() { let info = track.get_info().await?; - util::send(ctx, msg.channel_id, format!("track playing for {:?}", info.play_time), msg.tts) - .await?; + let fmt = format!("track playing for {:?}", info.play_time); + util::reply(ctx, fmt).await?; } Ok(()) |
