From 584f0ebc6e33ddc2905d82f44e7b42faea122668 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Fri, 16 Aug 2024 23:09:41 -0400 Subject: fill in queued playback info --- src/commands/playback.rs | 58 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'src/commands/playback.rs') 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) -> anyhow::Result<(Arc, Arc>)> { - 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::().cloned().unwrap() + }; + + if current_queue.is_empty() { util::reply(ctx, "nothing queued").await?; } - for track in queue.current_queue().into_iter() { + fn fmt_option(name: &str, opt: Option) -> String + where + T: std::fmt::Display, + { + if let Some(opt) = opt { + format!("{opt}") + } else { + format!("") + } + } + + 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?; } -- cgit v1.3.1