forked from DiscordBots/Rustendo
[WIP] fix: music queue bugs
This commit is contained in:
parent
c6af238f06
commit
e79b4142de
4 changed files with 70 additions and 70 deletions
|
@ -14,35 +14,35 @@ pub struct TrackEndNotifier {
|
|||
#[async_trait]
|
||||
impl EventHandler for TrackEndNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
// TODO: Does this need to be unsafe?
|
||||
if let EventContext::Track(..) = ctx {
|
||||
println!("The track ended!");
|
||||
let music_queue = crate::get_queue(self.guild_id).await;
|
||||
let q = &music_queue.queue;
|
||||
if q.is_empty() {
|
||||
// No more songs in queue, exit the vc
|
||||
let stopped = match music_manager::stop(&self.cmdctx, &self.guild_id).await {
|
||||
Ok(stopped) => stopped,
|
||||
Err(e) => {
|
||||
println!("Cannot stop: {:?}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
if stopped {
|
||||
println!("Stopped playing successfully.");
|
||||
} else {
|
||||
println!("Failed to stop playing.");
|
||||
// TODO: Does this need to be unsafe?
|
||||
if let EventContext::Track(..) = ctx {
|
||||
println!("The track ended!");
|
||||
let music_queue = crate::get_queue(&self.guild_id).await;
|
||||
let q = &music_queue.queue;
|
||||
if q.is_empty() {
|
||||
// No more songs in queue, exit the vc
|
||||
let stopped = match music_manager::stop(&self.cmdctx, &self.guild_id).await {
|
||||
Ok(stopped) => stopped,
|
||||
Err(e) => {
|
||||
println!("Cannot stop: {:?}", e);
|
||||
return None;
|
||||
}
|
||||
return None;
|
||||
};
|
||||
if stopped {
|
||||
println!("Stopped playing successfully.");
|
||||
} else {
|
||||
println!("Failed to stop playing.");
|
||||
}
|
||||
let head = music_queue::get_head(&self.guild_id);
|
||||
if head.is_none() {
|
||||
println!("Cannot get head of queue");
|
||||
return None;
|
||||
}
|
||||
let head = head.unwrap();
|
||||
music_manager::play_song(&self.cmdctx, &self.guild_id, head).await;
|
||||
return None;
|
||||
}
|
||||
let head = music_queue::get_head(&self.guild_id).await;
|
||||
if head.is_none() {
|
||||
println!("Cannot get head of queue");
|
||||
return None;
|
||||
}
|
||||
let head = head.unwrap();
|
||||
music_manager::play_song(&self.cmdctx, &self.guild_id, &head).await;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
|
|
@ -78,8 +78,8 @@ pub async fn attempt_to_queue_song(
|
|||
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());
|
||||
let currently_playing = music_queue::get_now_playing(guild_id).await;
|
||||
music_queue::add_to_queue(guild_id, src.clone()).await;
|
||||
if currently_playing.is_some() {
|
||||
// Add to queue
|
||||
return Embed::create(
|
||||
|
@ -89,8 +89,10 @@ pub async fn attempt_to_queue_song(
|
|||
);
|
||||
}
|
||||
|
||||
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 _query = music_queue::get_head(guild_id)
|
||||
.await
|
||||
.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,
|
||||
|
@ -150,7 +152,7 @@ pub async fn play_song(ctx: &Context, guild_id: &GuildId, target: &YoutubeDl) {
|
|||
return;
|
||||
}
|
||||
|
||||
music_queue::set_now_playing(guild_id, Some(target.clone()));
|
||||
music_queue::set_now_playing(guild_id, Some(target.clone())).await;
|
||||
let handler_lock = match manager.get(*guild_id) {
|
||||
Some(handler) => handler,
|
||||
None => return,
|
||||
|
@ -209,7 +211,7 @@ pub async fn attempt_to_stop(
|
|||
"I am not connected. I cant stop doing something, when I'm not doing it".to_string(),
|
||||
)
|
||||
} else {
|
||||
music_queue::delete_queue(guild_id); // Clear queue
|
||||
music_queue::delete_queue(guild_id).await; // Clear queue
|
||||
|
||||
Embed::create(username, "I stopped and left", "Just like your girlfriend.")
|
||||
}
|
||||
|
|
|
@ -10,38 +10,35 @@ pub struct MusicQueue {
|
|||
}
|
||||
|
||||
pub async fn delete_queue(guild_id: &GuildId) {
|
||||
let mut queue = crate::get_queue(*guild_id).await;
|
||||
let mut queue = crate::get_queue(guild_id).await;
|
||||
queue.now_playing = None;
|
||||
queue.queue = Vec::new();
|
||||
crate::set_queue(*guild_id, queue);
|
||||
crate::set_queue(guild_id, queue).await;
|
||||
}
|
||||
|
||||
pub async fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) {
|
||||
let mut queue = crate::get_queue(*guild_id).await;
|
||||
let mut queue = crate::get_queue(guild_id).await;
|
||||
queue.queue.push(input);
|
||||
crate::set_queue(*guild_id, queue);
|
||||
crate::set_queue(guild_id, queue).await;
|
||||
}
|
||||
|
||||
pub async fn get_head(guild_id: &GuildId) -> Option<&YoutubeDl> {
|
||||
let music_queue = crate::get_queue(*guild_id).await;
|
||||
pub async fn get_head(guild_id: &GuildId) -> Option<YoutubeDl> {
|
||||
let mut music_queue = crate::get_queue(guild_id).await;
|
||||
if music_queue.queue.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut queue = music_queue.queue;
|
||||
let result = queue.first();
|
||||
queue.remove(0);
|
||||
music_queue.queue = queue;
|
||||
crate::set_queue(*guild_id, music_queue);
|
||||
result
|
||||
let result = music_queue.queue.remove(0);
|
||||
crate::set_queue(guild_id, music_queue).await;
|
||||
Some(result)
|
||||
}
|
||||
|
||||
pub fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
|
||||
let mut queue = crate::get_queue(*guild_id);
|
||||
pub async fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
|
||||
let mut queue = crate::get_queue(guild_id).await;
|
||||
queue.now_playing = now_playing;
|
||||
crate::set_queue(*guild_id, queue)
|
||||
crate::set_queue(guild_id, queue).await;
|
||||
}
|
||||
|
||||
pub fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
|
||||
let queue = crate::get_queue(*guild_id);
|
||||
pub async fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
|
||||
let queue = crate::get_queue(guild_id).await;
|
||||
queue.now_playing
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue