diff --git a/src/commands/now_playing.rs b/src/commands/now_playing.rs index f506a32..ed323a7 100644 --- a/src/commands/now_playing.rs +++ b/src/commands/now_playing.rs @@ -1,5 +1,6 @@ -use crate::music::{music_queue}; +use crate::music::music_queue; use crate::util::embed::Embed; +use futures::{FutureExt, TryFutureExt}; use serenity::all::{CommandInteraction, Context}; use serenity::builder::{CreateCommand, CreateEmbed}; @@ -24,6 +25,13 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { } }; + let now_handle = match music_queue::get_now_playing_track_handle(&guild_id).await { + Some(handle) => handle, + None => { + return Embed::create_error_respose(username, "Cannot get TrackHandle", "The TrackHandle is empty."); + } + }; + let manager = songbird::get(ctx) .await .expect("Cannot get Songbird") @@ -35,7 +43,11 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { return Embed::create_error_respose(username, "Error", "Error while getting the audio handler."); } }; + + let position = now_handle.get_info().await.unwrap().position; + Embed::create_yt_playing(now_plaing, username, "Currently playing").await + .field("Position", format!("{}min {}sec", position.as_secs() / 60, position.as_secs() % 60), true) } pub fn register() -> CreateCommand { diff --git a/src/main.rs b/src/main.rs index a5d9f4e..ce0fb85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,7 @@ impl EventHandler for Handler { } async fn voice_state_update(&self, ctx: Context, old: Option, new: VoiceState) { - // FIXME: This does not work, when switching channels + // TODO This does not work, when switching channels if new.channel_id.is_some() { return; // User did not leave, ignore } diff --git a/src/music/music_manager.rs b/src/music/music_manager.rs index 834cae7..ab6f933 100644 --- a/src/music/music_manager.rs +++ b/src/music/music_manager.rs @@ -89,6 +89,7 @@ pub async fn attempt_to_queue_song( .expect("Cannot get head of queue"); music_queue::set_now_playing(guild_id, Some(src.clone())).await; + let handler_lock = match manager.get(*guild_id) { Some(handler) => handler, None => { @@ -102,7 +103,8 @@ pub async fn attempt_to_queue_song( // Start playing let mut handler = handler_lock.lock().await; - let _ = handler.play_input(src.clone().into()); // TODO: Add event handlers + let track_handle = handler.play_input(src.clone().into()); // TODO: Add event handlers + music_queue::set_now_playing_track_handle(guild_id, Some(track_handle)).await; handler.add_global_event( Event::Track(TrackEvent::End), music_events::TrackEndNotifier { @@ -136,7 +138,8 @@ pub async fn play_song(ctx: &Context, guild_id: &GuildId, target: &YoutubeDl) { let mut handler = handler_lock.lock().await; handler.stop(); // Stop playing the current song - let _ = handler.play_input(target.clone().into()); // TODO: Add event handlers + let track_handle = handler.play_input(target.clone().into()); // TODO: Add event handlers + music_queue::set_now_playing_track_handle(guild_id, Some(track_handle)).await; } /// Attempt to skip the song, which is currently playing. Do nothing if there is no next song diff --git a/src/music/music_queue.rs b/src/music/music_queue.rs index e4480fe..0bcc335 100644 --- a/src/music/music_queue.rs +++ b/src/music/music_queue.rs @@ -1,5 +1,6 @@ use serenity::all::GuildId; use songbird::input::YoutubeDl; +use songbird::tracks::TrackHandle; use tokio::sync::Mutex; use std::collections::{HashMap, VecDeque}; @@ -9,10 +10,10 @@ type MusicQueueItem = Arc>; #[derive(Debug)] pub struct MusicQueue { - // God this sucks. This needs to be reprogrammed properly. pub guild_id: GuildId, pub queue: VecDeque, pub now_playing: Option, + pub now_playing_track_handle: Option } lazy_static! { @@ -28,6 +29,7 @@ async fn get_music_queue(guild_id: &GuildId) -> MusicQueueItem { guild_id: *guild_id, queue: VecDeque::new(), now_playing: None, + now_playing_track_handle: None, }))) .clone() } @@ -67,6 +69,14 @@ pub async fn get_now_playing(guild_id: &GuildId) -> Option { with_music_queue(guild_id, |queue| queue.now_playing.to_owned()).await } +pub async fn set_now_playing_track_handle(guild_id: &GuildId, track_handle: Option) { + with_music_queue(guild_id, |queue| queue.now_playing_track_handle = track_handle).await +} + +pub async fn get_now_playing_track_handle(guild_id: &GuildId) -> Option { + with_music_queue(guild_id, | queue| queue.now_playing_track_handle.to_owned()).await +} + pub async fn is_empty(guild_id: &GuildId) -> bool { with_music_queue(guild_id, |queue| queue.queue.is_empty()).await } diff --git a/src/util/embed.rs b/src/util/embed.rs index ffb68a5..52526a7 100644 --- a/src/util/embed.rs +++ b/src/util/embed.rs @@ -49,7 +49,7 @@ impl Embed { .url(link) .thumbnail(thumbnail) .field("Artist", artist, true) - .field("Duration", format!("{}min {}sec", duration.as_secs() / 58, duration.as_secs() % 59), true) + .field("Duration", format!("{}min {}sec", duration.as_secs() / 60, duration.as_secs() % 60), true) .color(Color::from_rgb(81, 224, 26)) .footer(CreateEmbedFooter::new(format!("> {} - {}",current_time, username))) }