diff options
Diffstat (limited to 'src/commands/playback.rs')
| -rw-r--r-- | src/commands/playback.rs | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/src/commands/playback.rs b/src/commands/playback.rs index 4d6d0be..e7bf830 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -1,4 +1,7 @@ -use std::sync::Arc; +use std::{ + fmt::Debug, + sync::Arc, +}; use grate::tracing; use serenity::prelude::*; @@ -10,7 +13,10 @@ use songbird::{ use tap::Conv; use crate::{ - bot::HttpKey, + bot::{ + HttpKey, + PlaybackKey, + }, util, PoiseContext, PoiseData, @@ -21,9 +27,7 @@ pub fn commands() -> impl IntoIterator<Item = poise::Command<PoiseData, anyhow:: } 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 gid = util::guild_id(ctx)?; let sb = songbird::get(ctx.serenity_context()).await.expect("acquiring songbird handle"); let call = sb.get_or_insert(gid); @@ -196,19 +200,51 @@ pub async fn die(ctx: PoiseContext<'_>) -> anyhow::Result<()> { /// List queued audio. #[poise::command(prefix_command, guild_only, category = "playback", aliases("queue"))] pub async fn list(ctx: PoiseContext<'_>) -> anyhow::Result<()> { - let (_sb, call) = songbird(ctx).await?; + let current_queue = { + let (_sb, call) = songbird(ctx).await?; - let call = call.lock().await; - let queue = call.queue(); + let call = call.lock().await; + let queue = call.queue(); - if queue.current_queue().is_empty() { + queue.current_queue() + }; + + let playback_data = { + let data = ctx.serenity_context().data.read().await; + data.get::<PlaybackKey>().cloned().unwrap() + }; + + if current_queue.is_empty() { util::reply(ctx, "nothing queued").await?; } - for track in queue.current_queue().into_iter() { + fn fmt_option<T>(name: &str, opt: Option<T>) -> String + where + T: std::fmt::Display, + { + if let Some(opt) = opt { + format!("{opt}") + } else { + format!("<no {name}>") + } + } + + for track in current_queue.into_iter() { let info = track.get_info().await?; - let fmt = format!("track playing for {:?}", info.play_time); + let meta = playback_data.get(&track.uuid()).map(|x| x.clone()); + let meta = meta.as_ref(); + + let fmt = format!( + "{}: {:?} / {}", + fmt_option("title", meta.and_then(|x| x.title.as_ref())), + humantime::format_duration(info.position), + fmt_option( + "duration", + meta.and_then(|x| x.duration.as_ref().map(|&dur| humantime::format_duration(dur))) + ) + ); + util::reply(ctx, fmt).await?; } |
