WIP: working on removing unsafe from appliction

This commit is contained in:
moonleay 2024-03-10 15:27:18 +01:00
parent 9c1f6bee6d
commit c6af238f06
Signed by: moonleay
GPG key ID: 82667543CCD715FB
5 changed files with 77 additions and 69 deletions

View file

@ -2,9 +2,9 @@ mod commands;
mod util; mod util;
mod music; mod music;
use serenity::all::{CommandInteraction, CreateInteractionResponseFollowup, OnlineStatus, VoiceState}; use serenity::all::{CommandInteraction, CreateInteractionResponseFollowup, GuildId, OnlineStatus, VoiceState};
use serenity::async_trait; use serenity::async_trait;
use serenity::builder::{CreateEmbed}; use serenity::builder::CreateEmbed;
use serenity::gateway::ActivityData; use serenity::gateway::ActivityData;
use serenity::model::application::{Command, Interaction}; use serenity::model::application::{Command, Interaction};
use serenity::model::gateway::Ready; use serenity::model::gateway::Ready;
@ -24,6 +24,45 @@ impl TypeMapKey for HttpKey {
type Value = HttpClient; 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<HashMap<GuildId, MusicQueue>> = {
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; struct Handler;
#[async_trait] #[async_trait]

View file

@ -1,3 +1,3 @@
pub mod music_manager; pub mod music_manager;
pub mod music_events; pub mod music_events;
mod music_queue; pub mod music_queue;

View file

@ -14,11 +14,10 @@ pub struct TrackEndNotifier {
#[async_trait] #[async_trait]
impl EventHandler for TrackEndNotifier { impl EventHandler for TrackEndNotifier {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> { async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
unsafe {
// TODO: Does this need to be unsafe? // TODO: Does this need to be unsafe?
if let EventContext::Track(..) = ctx { if let EventContext::Track(..) = ctx {
println!("The track ended!"); 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; let q = &music_queue.queue;
if q.is_empty() { if q.is_empty() {
// No more songs in queue, exit the vc // No more songs in queue, exit the vc
@ -44,7 +43,6 @@ impl EventHandler for TrackEndNotifier {
let head = head.unwrap(); let head = head.unwrap();
music_manager::play_song(&self.cmdctx, &self.guild_id, head).await; music_manager::play_song(&self.cmdctx, &self.guild_id, head).await;
} }
}
None None
} }

View file

@ -10,7 +10,7 @@ use songbird::{Event, TrackEvent};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
pub async unsafe fn attempt_to_queue_song( pub async fn attempt_to_queue_song(
ctx: &Context, ctx: &Context,
guild_id: &GuildId, guild_id: &GuildId,
user_id: &UserId, user_id: &UserId,
@ -139,7 +139,7 @@ pub async unsafe fn attempt_to_queue_song(
.thumbnail(thumbnail) .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 let manager = &songbird::get(ctx) // TODO match
.await .await
.expect("Cannot get Songbird.") .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 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, ctx: &Context,
guild_id: &GuildId, guild_id: &GuildId,
user_id: &UserId, user_id: &UserId,

View file

@ -1,76 +1,47 @@
use serenity::all::GuildId; use serenity::all::GuildId;
use songbird::input::YoutubeDl; use songbird::input::YoutubeDl;
#[derive(Debug, Clone)]
pub struct MusicQueue { pub struct MusicQueue {
// God this sucks. This needs to be reprogrammed properly. // God this sucks. This needs to be reprogrammed properly.
guild_id: GuildId, pub guild_id: GuildId,
pub queue: Vec<YoutubeDl>, pub queue: Vec<YoutubeDl>,
pub now_playing: Option<YoutubeDl>, pub now_playing: Option<YoutubeDl>,
} }
static mut MUSIC_QUEUE: Vec<MusicQueue> = Vec::new(); pub async fn delete_queue(guild_id: &GuildId) {
let mut queue = crate::get_queue(*guild_id).await;
pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue { queue.now_playing = None;
let queue = MUSIC_QUEUE.iter().find(|q| q.guild_id == *guild_id); queue.queue = Vec::new();
match queue { crate::set_queue(*guild_id, 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>) { pub async fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) {
let index = MUSIC_QUEUE let mut queue = crate::get_queue(*guild_id).await;
.iter() queue.queue.push(input);
.position(|q| q.guild_id == *guild_id) crate::set_queue(*guild_id, queue);
.expect("Cannot get index");
MUSIC_QUEUE[index] = MusicQueue {
guild_id: *guild_id,
queue,
now_playing,
};
} }
pub unsafe fn delete_queue(guild_id: &GuildId) { pub async fn get_head(guild_id: &GuildId) -> Option<&YoutubeDl> {
let index = MUSIC_QUEUE let music_queue = crate::get_queue(*guild_id).await;
.iter() if music_queue.queue.is_empty() {
.position(|q| q.guild_id == *guild_id) return None;
.expect("Cannot get index");
MUSIC_QUEUE.remove(index);
} }
let mut queue = music_queue.queue;
pub unsafe fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) { let result = queue.first();
let queue = get_queue(guild_id); queue.remove(0);
let mut new_queue = queue.queue.clone(); music_queue.queue = queue;
new_queue.push(input); crate::set_queue(*guild_id, music_queue);
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 result
} }
pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) { pub fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
let queue = get_queue(guild_id); let mut queue = crate::get_queue(*guild_id);
update_queue(guild_id, queue.queue.clone(), now_playing); queue.now_playing = now_playing;
crate::set_queue(*guild_id, queue)
} }
pub unsafe fn get_now_playing(guild_id: &GuildId) -> &Option<YoutubeDl> { pub fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
let queue = get_queue(guild_id); let queue = crate::get_queue(*guild_id);
&queue.now_playing queue.now_playing
} }