From e1cf394362cd909aa682909acbd03bc194f3b1a4 Mon Sep 17 00:00:00 2001 From: moonleay Date: Sat, 9 Mar 2024 20:06:08 +0100 Subject: [PATCH] WIP: continued work on queue --- src/main.rs | 4 +-- src/music/music_events.rs | 8 +----- src/music/music_manager.rs | 52 +++++++++++++++++++++++++------------- src/music/music_queue.rs | 19 +++++++------- 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 090a238..6f32485 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,8 +59,8 @@ impl EventHandler for Handler { println!("Commands are registered and Rustendo is ready for Freddy."); } - async fn voice_state_update(&self, ctx: Context, old: Option, new: VoiceState) { - if !new.channel_id.is_none() { + async fn voice_state_update(&self, ctx: Context, old: Option, new: VoiceState) { // FIXME: This does not work, when switching channels + if new.channel_id.is_some() { return; // User did not leave, ignore } if let Some(old) = old { diff --git a/src/music/music_events.rs b/src/music/music_events.rs index 6358fd4..dbf84a0 100644 --- a/src/music/music_events.rs +++ b/src/music/music_events.rs @@ -41,13 +41,7 @@ impl EventHandler for TrackEndNotifier { return None; } let head = head.unwrap(); - /*let started = match music_manager::play(&self.cmdctx, &self.guild_id, &head).await { - Ok(started) => started, - Err(e) => { - println!("Cannot play: {:?}", e); - return None; - } - }; */ + music_manager::play_song(&self.cmdctx, &self.guild_id, head).await; } } diff --git a/src/music/music_manager.rs b/src/music/music_manager.rs index 64a2063..26c0496 100644 --- a/src/music/music_manager.rs +++ b/src/music/music_manager.rs @@ -53,16 +53,6 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use } } - let currently_playing = music_queue::get_now_playing(&guild_id); - music_queue::add_to_queue(&guild_id, query.to_string()); - if currently_playing != "".to_string() { - // Add to queue - return Embed::create(username, "Added to queue", "The song was added to the queue."); - } - - let query = music_queue::get_head(&guild_id).expect("Cannot get head of queue"); - music_queue::set_now_playing(&guild_id, query.clone()); - // Get query let do_search = !query.starts_with("http"); let http_client = { @@ -71,13 +61,25 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use .cloned() .expect("Guaranteed to exist in the typemap.") }; - + // Create source let mut src = if do_search { YoutubeDl::new_search(http_client, query.to_string()) } else { YoutubeDl::new(http_client, query.to_string()) }; + + let currently_playing = music_queue::get_now_playing(&guild_id); + music_queue::add_to_queue(&guild_id, src.clone()); + if currently_playing.is_some() { + // Add to queue + return Embed::create(username, "Added to queue", "The song was added to the queue."); + } + + let query = music_queue::get_head(&guild_id).expect("Cannot get head of queue"); + music_queue::set_now_playing(&guild_id, Some(src.clone())); + + let handler_lock = match manager.get(*guild_id) { Some(handler) => handler, None => { @@ -97,7 +99,7 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use cmdctx: Arc::new(ctx.clone()), }, ); - + // Get metadata let metadata = src.aux_metadata().await.expect("Cannot get metadata"); let title = metadata.title.unwrap_or("Unknown title".to_string()); @@ -111,8 +113,27 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use .thumbnail(thumbnail) } -pub async unsafe fn play() { +pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, target: YoutubeDl) { + let manager = &songbird::get(ctx) // TODO match + .await + .expect("Cannot get Songbird.") + .clone(); + if !user_util::is_self_connected_to_vc(ctx, guild_id).await { + println!("Bot is not connected to a VC, cannot play."); + return; + } + + music_queue::set_now_playing(&guild_id, Some(target.clone())); + let handler_lock = match manager.get(*guild_id) { + Some(handler) => handler, + None => { + return + }, + }; + + let mut handler = handler_lock.lock().await; + let _ = handler.play_input(target.into()); // TODO: Add event handlers } pub async unsafe fn attempt_to_stop(ctx: &Context, guild_id: &GuildId, user_id: &UserId, username: &str) -> CreateEmbed { @@ -157,11 +178,6 @@ pub async unsafe fn attempt_to_stop(ctx: &Context, guild_id: &GuildId, user_id: } } -/// pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, query: String) -> Result { - -/// } - - // Make the bot leave the voice channel. Returns Ok(true) if bot was connected, returns Ok(false) if bot was not connected. Returns Err if something went wrong. pub async fn stop(ctx: &Context, guild_id: &GuildId) -> Result { let manager = songbird::get(ctx) diff --git a/src/music/music_queue.rs b/src/music/music_queue.rs index 182a8ee..77b20aa 100644 --- a/src/music/music_queue.rs +++ b/src/music/music_queue.rs @@ -1,9 +1,10 @@ use serenity::all::GuildId; +use songbird::input::YoutubeDl; pub struct MusicQueue { // God this sucks. This needs to be reprogrammed properly. guild_id: GuildId, - pub queue: Vec, - pub now_playing: String, + pub queue: Vec, + pub now_playing: Option, } static mut MUSIC_QUEUE: Vec = Vec::new(); @@ -16,7 +17,7 @@ pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue { let new_queue = MusicQueue { guild_id: *guild_id, queue: Vec::new(), - now_playing: "".to_string(), + now_playing: None, }; MUSIC_QUEUE.push(new_queue); MUSIC_QUEUE.iter().find(|q| q.guild_id == *guild_id).expect("Cannot get queue") @@ -24,7 +25,7 @@ pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue { } } -unsafe fn update_queue(guild_id: &GuildId, queue: Vec, now_playing: String) { +unsafe fn update_queue(guild_id: &GuildId, queue: Vec, now_playing: Option) { let index = MUSIC_QUEUE.iter().position(|q| q.guild_id == *guild_id).expect("Cannot get index"); MUSIC_QUEUE[index] = MusicQueue{ guild_id: *guild_id, @@ -38,14 +39,14 @@ pub unsafe fn delete_queue(guild_id: &GuildId) { MUSIC_QUEUE.remove(index); } -pub unsafe fn add_to_queue(guild_id: &GuildId, input: String) { +pub unsafe fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) { let queue = get_queue(guild_id); let mut new_queue = queue.queue.clone(); new_queue.push(input); update_queue(guild_id, new_queue, queue.now_playing.clone()); } -pub unsafe fn get_head(guild_id: &GuildId) -> Option { +pub unsafe fn get_head(guild_id: &GuildId) -> Option { let queue = get_queue(guild_id); let mut q = queue.queue.clone(); let result = q.first().map(|s| s.clone()); @@ -54,12 +55,12 @@ pub unsafe fn get_head(guild_id: &GuildId) -> Option { result } -pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: String) { +pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: Option) { let queue = get_queue(guild_id); update_queue(guild_id, queue.queue.clone(), now_playing); } -pub unsafe fn get_now_playing(guild_id: &GuildId) -> String { +pub unsafe fn get_now_playing(guild_id: &GuildId) -> &Option { let queue = get_queue(guild_id); - queue.now_playing.clone() + &queue.now_playing } \ No newline at end of file