From c6af238f06d5a26e27d5d1decd041ffdda6423f5 Mon Sep 17 00:00:00 2001 From: moonleay Date: Sun, 10 Mar 2024 15:27:18 +0100 Subject: [PATCH] WIP: working on removing unsafe from appliction --- src/main.rs | 43 +++++++++++++++++- src/music/mod.rs | 2 +- src/music/music_events.rs | 4 +- src/music/music_manager.rs | 8 ++-- src/music/music_queue.rs | 89 +++++++++++++------------------------- 5 files changed, 77 insertions(+), 69 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6f32485..1422ec3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ mod commands; mod util; mod music; -use serenity::all::{CommandInteraction, CreateInteractionResponseFollowup, OnlineStatus, VoiceState}; +use serenity::all::{CommandInteraction, CreateInteractionResponseFollowup, GuildId, OnlineStatus, VoiceState}; use serenity::async_trait; -use serenity::builder::{CreateEmbed}; +use serenity::builder::CreateEmbed; use serenity::gateway::ActivityData; use serenity::model::application::{Command, Interaction}; use serenity::model::gateway::Ready; @@ -24,6 +24,45 @@ 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; + +lazy_static! { + static ref HASHMAP: Mutex> = { + Mutex::new(HashMap::new()) + }; +} + +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, + queue: Vec::new(), + now_playing: None + }; + 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); +} + + + + struct Handler; #[async_trait] diff --git a/src/music/mod.rs b/src/music/mod.rs index 28737b1..8224adc 100644 --- a/src/music/mod.rs +++ b/src/music/mod.rs @@ -1,3 +1,3 @@ pub mod music_manager; pub mod music_events; -mod music_queue; \ No newline at end of file +pub mod music_queue; \ No newline at end of file diff --git a/src/music/music_events.rs b/src/music/music_events.rs index abde30f..a58c78c 100644 --- a/src/music/music_events.rs +++ b/src/music/music_events.rs @@ -14,11 +14,10 @@ pub struct TrackEndNotifier { #[async_trait] impl EventHandler for TrackEndNotifier { async fn act(&self, ctx: &EventContext<'_>) -> Option { - unsafe { // TODO: Does this need to be unsafe? if let EventContext::Track(..) = ctx { println!("The track ended!"); - let music_queue = music_queue::get_queue(&self.guild_id); + 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 @@ -44,7 +43,6 @@ impl EventHandler for TrackEndNotifier { let head = head.unwrap(); music_manager::play_song(&self.cmdctx, &self.guild_id, head).await; } - } None } diff --git a/src/music/music_manager.rs b/src/music/music_manager.rs index 18c1e88..476af45 100644 --- a/src/music/music_manager.rs +++ b/src/music/music_manager.rs @@ -10,7 +10,7 @@ use songbird::{Event, TrackEvent}; use std::sync::Arc; use std::time::Duration; -pub async unsafe fn attempt_to_queue_song( +pub async fn attempt_to_queue_song( ctx: &Context, guild_id: &GuildId, user_id: &UserId, @@ -139,7 +139,7 @@ pub async unsafe fn attempt_to_queue_song( .thumbnail(thumbnail) } -pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, target: YoutubeDl) { +pub async fn play_song(ctx: &Context, guild_id: &GuildId, target: &YoutubeDl) { let manager = &songbird::get(ctx) // TODO match .await .expect("Cannot get Songbird.") @@ -157,10 +157,10 @@ pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, target: Youtube }; let mut handler = handler_lock.lock().await; - let _ = handler.play_input(target.into()); // TODO: Add event handlers + let _ = handler.play_input(target.clone().into()); // TODO: Add event handlers } -pub async unsafe fn attempt_to_stop( +pub async fn attempt_to_stop( ctx: &Context, guild_id: &GuildId, user_id: &UserId, diff --git a/src/music/music_queue.rs b/src/music/music_queue.rs index 3d6a2ff..6862ebd 100644 --- a/src/music/music_queue.rs +++ b/src/music/music_queue.rs @@ -1,76 +1,47 @@ use serenity::all::GuildId; use songbird::input::YoutubeDl; +#[derive(Debug, Clone)] pub struct MusicQueue { // God this sucks. This needs to be reprogrammed properly. - guild_id: GuildId, + pub guild_id: GuildId, pub queue: Vec, pub now_playing: Option, } -static mut MUSIC_QUEUE: Vec = Vec::new(); +pub async fn delete_queue(guild_id: &GuildId) { + let mut queue = crate::get_queue(*guild_id).await; + queue.now_playing = None; + queue.queue = Vec::new(); + crate::set_queue(*guild_id, queue); +} -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") - } +pub async fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) { + let mut queue = crate::get_queue(*guild_id).await; + queue.queue.push(input); + crate::set_queue(*guild_id, queue); +} + +pub async fn get_head(guild_id: &GuildId) -> Option<&YoutubeDl> { + let music_queue = crate::get_queue(*guild_id).await; + if music_queue.queue.is_empty() { + return None; } -} - -unsafe fn update_queue(guild_id: &GuildId, queue: Vec, now_playing: Option) { - 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 { - 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()); + 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 } -pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: Option) { - let queue = get_queue(guild_id); - update_queue(guild_id, queue.queue.clone(), now_playing); +pub fn set_now_playing(guild_id: &GuildId, now_playing: Option) { + let mut queue = crate::get_queue(*guild_id); + queue.now_playing = now_playing; + crate::set_queue(*guild_id, queue) } -pub unsafe fn get_now_playing(guild_id: &GuildId) -> &Option { - let queue = get_queue(guild_id); - &queue.now_playing +pub fn get_now_playing(guild_id: &GuildId) -> Option { + let queue = crate::get_queue(*guild_id); + queue.now_playing }