fix: bug fixes

This commit is contained in:
Miguel da Mota 2024-03-10 19:46:27 +01:00
parent ed89386ed9
commit 05fae26549
4 changed files with 76 additions and 30 deletions

View file

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

View file

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

View file

@ -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 {
@ -50,7 +53,11 @@ pub async fn attempt_to_queue_song(
// 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 {
@ -162,13 +182,21 @@ pub async fn attempt_to_skip_current_song(ctx: &Context, guild_id: &GuildId, use
// 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

View file

@ -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<YoutubeDl> {
}
pub async fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
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<YoutubeDl> {
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 {