chore: use VecDeque for music queue

This commit is contained in:
Miguel da Mota 2024-03-10 20:11:39 +01:00
parent 05fae26549
commit f3fec5292a

View file

@ -2,11 +2,19 @@ use serenity::all::GuildId;
use songbird::input::YoutubeDl;
use tokio::sync::Mutex;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
type MusicQueueItem = Arc<Mutex<MusicQueue>>;
#[derive(Debug)]
pub struct MusicQueue {
// God this sucks. This needs to be reprogrammed properly.
pub guild_id: GuildId,
pub queue: VecDeque<YoutubeDl>,
pub now_playing: Option<YoutubeDl>,
}
lazy_static! {
static ref HASHMAP: Mutex<HashMap<GuildId, MusicQueueItem>> = Mutex::new(HashMap::new());
}
@ -18,7 +26,7 @@ async fn get_music_queue(guild_id: &GuildId) -> MusicQueueItem {
.entry(*guild_id)
.or_insert(Arc::new(Mutex::new(MusicQueue {
guild_id: *guild_id,
queue: Vec::new(),
queue: VecDeque::new(),
now_playing: None,
})))
.clone()
@ -35,14 +43,6 @@ where
f(&mut queue)
}
#[derive(Debug)]
pub struct MusicQueue {
// God this sucks. This needs to be reprogrammed properly.
pub guild_id: GuildId,
pub queue: Vec<YoutubeDl>,
pub now_playing: Option<YoutubeDl>,
}
pub async fn delete_queue(guild_id: &GuildId) {
with_music_queue(guild_id, |queue| {
queue.now_playing = None;
@ -52,21 +52,11 @@ pub async fn delete_queue(guild_id: &GuildId) {
}
pub async fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) {
with_music_queue(guild_id, |queue| {
queue.queue.push(input);
})
.await;
with_music_queue(guild_id, |queue| queue.queue.push_back(input)).await;
}
pub async fn get_head(guild_id: &GuildId) -> Option<YoutubeDl> {
with_music_queue(guild_id, |queue| {
if queue.queue.is_empty() {
None
} else {
Some(queue.queue.remove(0))
}
})
.await
with_music_queue(guild_id, |queue| queue.queue.pop_front()).await
}
pub async fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {