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 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<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;
#[async_trait]

View file

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

View file

@ -14,11 +14,10 @@ pub struct TrackEndNotifier {
#[async_trait]
impl EventHandler for TrackEndNotifier {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
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
}

View file

@ -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,

View file

@ -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<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")
}
}
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);
}
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 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 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 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;
}
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());
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<YoutubeDl>) {
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<YoutubeDl>) {
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<YoutubeDl> {
let queue = get_queue(guild_id);
&queue.now_playing
pub fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
let queue = crate::get_queue(*guild_id);
queue.now_playing
}