[WIP] fix: music queue bugs
This commit is contained in:
parent
c6af238f06
commit
e79b4142de
4 changed files with 70 additions and 70 deletions
43
src/main.rs
43
src/main.rs
|
@ -1,8 +1,10 @@
|
|||
mod commands;
|
||||
mod util;
|
||||
mod music;
|
||||
mod util;
|
||||
|
||||
use serenity::all::{CommandInteraction, CreateInteractionResponseFollowup, GuildId, OnlineStatus, VoiceState};
|
||||
use serenity::all::{
|
||||
CommandInteraction, CreateInteractionResponseFollowup, GuildId, OnlineStatus, VoiceState,
|
||||
};
|
||||
use serenity::async_trait;
|
||||
use serenity::builder::CreateEmbed;
|
||||
use serenity::gateway::ActivityData;
|
||||
|
@ -24,45 +26,36 @@ impl TypeMapKey for HttpKey {
|
|||
type Value = HttpClient;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// lazy static stuff. I don't like it, but it has to be here, bc it has to be @ root
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::music::music_queue::MusicQueue;
|
||||
use std::collections::HashMap;
|
||||
|
||||
lazy_static! {
|
||||
static ref HASHMAP: Mutex<HashMap<GuildId, MusicQueue>> = {
|
||||
Mutex::new(HashMap::new())
|
||||
};
|
||||
static ref HASHMAP: Mutex<HashMap<GuildId, MusicQueue>> = Mutex::new(HashMap::new());
|
||||
}
|
||||
|
||||
pub async fn get_queue(guild_id: GuildId) -> MusicQueue {
|
||||
pub async fn get_queue(guild_id: &GuildId) -> MusicQueue {
|
||||
match HASHMAP.lock().await.get(&guild_id) {
|
||||
Some(music_queue) => music_queue.clone(),
|
||||
None => {
|
||||
let q = MusicQueue {
|
||||
guild_id: guild_id,
|
||||
guild_id: *guild_id,
|
||||
queue: Vec::new(),
|
||||
now_playing: None
|
||||
now_playing: None,
|
||||
};
|
||||
HASHMAP.lock().await.insert(guild_id, q.clone());
|
||||
HASHMAP.lock().await.insert(*guild_id, q.clone());
|
||||
q
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn set_queue(guild_id: GuildId, queue: MusicQueue) {
|
||||
HASHMAP.lock().await.insert(guild_id, queue);
|
||||
pub async fn set_queue(guild_id: &GuildId, queue: MusicQueue) {
|
||||
HASHMAP.lock().await.insert(*guild_id, queue);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
|
@ -98,7 +91,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<VoiceState>, new: VoiceState) { // FIXME: This does not work, when switching channels
|
||||
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) {
|
||||
// FIXME: This does not work, when switching channels
|
||||
if new.channel_id.is_some() {
|
||||
return; // User did not leave, ignore
|
||||
}
|
||||
|
@ -106,7 +100,14 @@ impl EventHandler for Handler {
|
|||
if !user_util::is_self_connected_to_vc(&ctx, &old.guild_id.unwrap()).await {
|
||||
return; // Bot is not connected to a VC, ignore
|
||||
}
|
||||
if user_util::get_amount_of_members_in_vc(&ctx, &old.guild_id.unwrap(), &old.channel_id.unwrap()).await < 2 {
|
||||
if user_util::get_amount_of_members_in_vc(
|
||||
&ctx,
|
||||
&old.guild_id.unwrap(),
|
||||
&old.channel_id.unwrap(),
|
||||
)
|
||||
.await
|
||||
< 2
|
||||
{
|
||||
let manager = songbird::get(&ctx).await.expect("Cannot get Songbird");
|
||||
if let Err(e) = manager.remove(old.guild_id.unwrap()).await {
|
||||
println!("Failed to remove handler: {:?}", e);
|
||||
|
|
|
@ -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…
Reference in a new issue