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

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