forked from DiscordBots/Rustendo
76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
use serenity::all::GuildId;
|
|
use songbird::input::YoutubeDl;
|
|
|
|
pub struct MusicQueue {
|
|
// God this sucks. This needs to be reprogrammed properly.
|
|
guild_id: GuildId,
|
|
pub queue: Vec<YoutubeDl>,
|
|
pub now_playing: Option<YoutubeDl>,
|
|
}
|
|
|
|
static mut MUSIC_QUEUE: Vec<MusicQueue> = Vec::new();
|
|
|
|
pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue {
|
|
let queue = MUSIC_QUEUE.iter().find(|q| q.guild_id == *guild_id);
|
|
match queue {
|
|
Some(queue) => queue,
|
|
None => {
|
|
let new_queue = MusicQueue {
|
|
guild_id: *guild_id,
|
|
queue: Vec::new(),
|
|
now_playing: None,
|
|
};
|
|
MUSIC_QUEUE.push(new_queue);
|
|
MUSIC_QUEUE
|
|
.iter()
|
|
.find(|q| q.guild_id == *guild_id)
|
|
.expect("Cannot get queue")
|
|
}
|
|
}
|
|
}
|
|
|
|
unsafe fn update_queue(guild_id: &GuildId, queue: Vec<YoutubeDl>, now_playing: Option<YoutubeDl>) {
|
|
let index = MUSIC_QUEUE
|
|
.iter()
|
|
.position(|q| q.guild_id == *guild_id)
|
|
.expect("Cannot get index");
|
|
MUSIC_QUEUE[index] = MusicQueue {
|
|
guild_id: *guild_id,
|
|
queue,
|
|
now_playing,
|
|
};
|
|
}
|
|
|
|
pub unsafe fn delete_queue(guild_id: &GuildId) {
|
|
let index = MUSIC_QUEUE
|
|
.iter()
|
|
.position(|q| q.guild_id == *guild_id)
|
|
.expect("Cannot get index");
|
|
MUSIC_QUEUE.remove(index);
|
|
}
|
|
|
|
pub unsafe fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) {
|
|
let queue = get_queue(guild_id);
|
|
let mut new_queue = queue.queue.clone();
|
|
new_queue.push(input);
|
|
update_queue(guild_id, new_queue, queue.now_playing.clone());
|
|
}
|
|
|
|
pub unsafe fn get_head(guild_id: &GuildId) -> Option<YoutubeDl> {
|
|
let queue = get_queue(guild_id);
|
|
let mut q = queue.queue.clone();
|
|
let result = q.first().cloned();
|
|
q.remove(0);
|
|
update_queue(guild_id, q, queue.now_playing.clone());
|
|
result
|
|
}
|
|
|
|
pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
|
|
let queue = get_queue(guild_id);
|
|
update_queue(guild_id, queue.queue.clone(), now_playing);
|
|
}
|
|
|
|
pub unsafe fn get_now_playing(guild_id: &GuildId) -> &Option<YoutubeDl> {
|
|
let queue = get_queue(guild_id);
|
|
&queue.now_playing
|
|
}
|