feat: added current position to nowplaying command

This commit is contained in:
moonleay 2024-03-10 21:36:10 +01:00
parent eecb61b9f5
commit a72f9753a2
Signed by: moonleay
GPG key ID: 82667543CCD715FB
5 changed files with 31 additions and 6 deletions

View file

@ -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 {

View file

@ -68,7 +68,7 @@ impl EventHandler for Handler {
}
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, 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
}

View file

@ -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

View file

@ -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<Mutex<MusicQueue>>;
#[derive(Debug)]
pub struct MusicQueue {
// God this sucks. This needs to be reprogrammed properly.
pub guild_id: GuildId,
pub queue: VecDeque<YoutubeDl>,
pub now_playing: Option<YoutubeDl>,
pub now_playing_track_handle: Option<TrackHandle>
}
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<YoutubeDl> {
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<TrackHandle>) {
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<TrackHandle> {
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
}

View file

@ -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)))
}