From 05fae26549bb42b8f01c14f7567c2e92608423bf Mon Sep 17 00:00:00 2001 From: Miguel da Mota Date: Sun, 10 Mar 2024 19:46:27 +0100 Subject: [PATCH] fix: bug fixes --- src/commands/skip.rs | 11 ++++-- src/music/music_events.rs | 7 +++- src/music/music_manager.rs | 75 ++++++++++++++++++++++++++++++-------- src/music/music_queue.rs | 13 ++----- 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/commands/skip.rs b/src/commands/skip.rs index 3598dc5..ccf8ad6 100644 --- a/src/commands/skip.rs +++ b/src/commands/skip.rs @@ -3,17 +3,22 @@ use crate::util::embed::Embed; use serenity::all::{CommandInteraction, Context}; use serenity::builder::{CreateCommand, CreateEmbed}; -pub async unsafe fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { +pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { let username = command.user.name.as_str(); let guild_id = match &command.guild_id { Some(guild_id) => guild_id, None => { - return Embed::create_error_respose(username, "guild_id not found", "Could not find guild id."); + return Embed::create_error_respose( + username, + "guild_id not found", + "Could not find guild id.", + ); } }; - music_manager::attempt_to_skip_current_song(ctx, guild_id, &command.user.id, &command.user.name).await + music_manager::attempt_to_skip_current_song(ctx, guild_id, &command.user.id, &command.user.name) + .await } pub fn register() -> CreateCommand { diff --git a/src/music/music_events.rs b/src/music/music_events.rs index acfd813..30abb9a 100644 --- a/src/music/music_events.rs +++ b/src/music/music_events.rs @@ -40,11 +40,14 @@ impl EventHandler for TrackEndNotifier { let mut head = match music_queue::get_head(&self.guild_id).await { Some(head) => head, None => { - println!("Cannot get head of queue: {:?}", e); + println!("Cannot get head of queue"); return None; } }; - println!("Now playing: {}", head.aux_metadata().await.unwrap().title.unwrap()); + println!( + "Now playing: {}", + head.aux_metadata().await.unwrap().title.unwrap() + ); 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 74a1b3c..834cae7 100644 --- a/src/music/music_manager.rs +++ b/src/music/music_manager.rs @@ -9,7 +9,6 @@ use songbird::input::YoutubeDl; use songbird::{Event, TrackEvent}; use std::sync::Arc; - /// Either queues the song, or start playing it instantly, depending on if there is already a song playing pub async fn attempt_to_queue_song( ctx: &Context, @@ -19,7 +18,11 @@ pub async fn attempt_to_queue_song( query: &str, ) -> CreateEmbed { if !user_util::is_user_connected_to_vc(ctx, guild_id, user_id).await { - return Embed::create_error_respose(username, "You are not connected to a VC", "Connect to my vc to start controlling the music.") + return Embed::create_error_respose( + username, + "You are not connected to a VC", + "Connect to my vc to start controlling the music.", + ); } let connect_to = match get_vc_id(ctx, guild_id, user_id).await { @@ -46,11 +49,15 @@ pub async fn attempt_to_queue_song( .expect("Cannot connect>..."); } } else { - let self_channel = self_channel.expect("Cannot get self channel");// TODO: match + let self_channel = self_channel.expect("Cannot get self channel"); // TODO: match // Check if user is in the same VC as the bot if self_channel != connect_to { - return Embed::create_error_respose(username, "You are not in my VC", "You have to be in my VC in order to controll the music.") + return Embed::create_error_respose( + username, + "You are not in my VC", + "You have to be in my VC in order to controll the music.", + ); } } @@ -85,7 +92,11 @@ pub async fn attempt_to_queue_song( let handler_lock = match manager.get(*guild_id) { Some(handler) => handler, None => { - return Embed::create_error_respose(username, "Error", "Cannot get handler of this guild."); + return Embed::create_error_respose( + username, + "Error", + "Cannot get handler of this guild.", + ); } }; @@ -124,14 +135,23 @@ pub async fn play_song(ctx: &Context, guild_id: &GuildId, target: &YoutubeDl) { }; let mut handler = handler_lock.lock().await; - let _ = handler.stop(); // Stop playing the current song + handler.stop(); // Stop playing the current song let _ = handler.play_input(target.clone().into()); // TODO: Add event handlers } /// Attempt to skip the song, which is currently playing. Do nothing if there is no next song -pub async fn attempt_to_skip_current_song(ctx: &Context, guild_id: &GuildId, user_id: &UserId, username: &str) -> CreateEmbed { +pub async fn attempt_to_skip_current_song( + ctx: &Context, + guild_id: &GuildId, + user_id: &UserId, + username: &str, +) -> CreateEmbed { if !user_util::is_user_connected_to_vc(ctx, guild_id, user_id).await { - return Embed::create_error_respose(username, "You are not connected to a VC", "Connect to my vc to start controlling the music.") + return Embed::create_error_respose( + username, + "You are not connected to a VC", + "Connect to my vc to start controlling the music.", + ); } let connect_to = match get_vc_id(ctx, guild_id, user_id).await { @@ -158,17 +178,25 @@ pub async fn attempt_to_skip_current_song(ctx: &Context, guild_id: &GuildId, use .expect("Cannot connect>..."); } } else { - let self_channel = self_channel.expect("Cannot get self channel");// TODO: match + let self_channel = self_channel.expect("Cannot get self channel"); // TODO: match // Check if user is in the same VC as the bot if self_channel != connect_to { - return Embed::create_error_respose(username, "You are not in my VC", "You have to be in my VC in order to controll the music.") + return Embed::create_error_respose( + username, + "You are not in my VC", + "You have to be in my VC in order to controll the music.", + ); } } let head = music_queue::get_head(guild_id).await; // TODO match if head.is_none() { - return Embed::create_error_respose(username, "Cannot find a song to play", "The queue is empty."); + return Embed::create_error_respose( + username, + "Cannot find a song to play", + "The queue is empty.", + ); } let head = head.unwrap(); play_song(ctx, guild_id, &head).await; @@ -176,7 +204,6 @@ pub async fn attempt_to_skip_current_song(ctx: &Context, guild_id: &GuildId, use Embed::create_yt_playing(head, username, "Song skipped; Now playing").await } - /// Try to clear the queue and stop playing. Also leave the vc pub async fn attempt_to_stop( ctx: &Context, @@ -186,7 +213,11 @@ pub async fn attempt_to_stop( ) -> CreateEmbed { if !user_util::is_self_connected_to_vc(ctx, guild_id).await { // Bot is not connectd to vc; no need to dc - return Embed::create_error_respose(username, "Bot is not connected", "And therefore there is no need to do anything."); + return Embed::create_error_respose( + username, + "Bot is not connected", + "And therefore there is no need to do anything.", + ); } let self_channel = user_util::get_self_vc_id(ctx, guild_id) .await @@ -197,19 +228,31 @@ pub async fn attempt_to_stop( // Check if user is in the same VC as the bot if self_channel != connect_to { - return Embed::create_error_respose(username, "You are not in my VC.", "Connect to my VC to controll the music."); + return Embed::create_error_respose( + username, + "You are not in my VC.", + "Connect to my VC to controll the music.", + ); } let stopped = match leave(ctx, guild_id).await { Ok(stopped) => stopped, Err(e) => { println!("Error while stopping: {:?}", e); - return Embed::create_error_respose(username, "There was an error", "Tell moonleay to check the logs."); + return Embed::create_error_respose( + username, + "There was an error", + "Tell moonleay to check the logs.", + ); } }; if !stopped { - Embed::create_error_respose(username, "Can't stop, what ain't running", "I am not connected.\nI cant stop doing something, when I'm not doing it.") + Embed::create_error_respose( + username, + "Can't stop, what ain't running", + "I am not connected.\nI cant stop doing something, when I'm not doing it.", + ) } else { music_queue::delete_queue(guild_id).await; // Clear queue diff --git a/src/music/music_queue.rs b/src/music/music_queue.rs index 741720e..37b76fb 100644 --- a/src/music/music_queue.rs +++ b/src/music/music_queue.rs @@ -1,6 +1,6 @@ use serenity::all::GuildId; use songbird::input::YoutubeDl; -use tokio::sync::{Mutex, MutexGuard}; +use tokio::sync::Mutex; use std::collections::HashMap; use std::sync::Arc; @@ -32,7 +32,7 @@ where let queue = get_music_queue(guild_id).await; let mut queue = queue.lock().await; - f(&mut *queue) + f(&mut queue) } #[derive(Debug)] @@ -70,16 +70,11 @@ pub async fn get_head(guild_id: &GuildId) -> Option { } pub async fn set_now_playing(guild_id: &GuildId, now_playing: Option) { - let queue = get_music_queue(guild_id).await; - let mut queue = queue.lock().await; - - queue.now_playing = now_playing; + with_music_queue(guild_id, |queue| queue.now_playing = now_playing).await; } pub async fn get_now_playing(guild_id: &GuildId) -> Option { - let queue = get_queue(guild_id).await; - - queue.now_playing.to_owned() + with_music_queue(guild_id, |queue| queue.now_playing.to_owned()).await } pub async fn is_empty(guild_id: &GuildId) -> bool {