WIP: continued work on queue

This commit is contained in:
moonleay 2024-03-09 20:06:08 +01:00
parent ef06cbc90b
commit e1cf394362
Signed by: moonleay
GPG key ID: 82667543CCD715FB
4 changed files with 47 additions and 36 deletions

View file

@ -59,8 +59,8 @@ impl EventHandler for Handler {
println!("Commands are registered and Rustendo is ready for Freddy."); println!("Commands are registered and Rustendo is ready for Freddy.");
} }
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) { async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) { // FIXME: This does not work, when switching channels
if !new.channel_id.is_none() { if new.channel_id.is_some() {
return; // User did not leave, ignore return; // User did not leave, ignore
} }
if let Some(old) = old { if let Some(old) = old {

View file

@ -41,13 +41,7 @@ impl EventHandler for TrackEndNotifier {
return None; return None;
} }
let head = head.unwrap(); let head = head.unwrap();
/*let started = match music_manager::play(&self.cmdctx, &self.guild_id, &head).await { music_manager::play_song(&self.cmdctx, &self.guild_id, head).await;
Ok(started) => started,
Err(e) => {
println!("Cannot play: {:?}", e);
return None;
}
}; */
} }
} }

View file

@ -53,16 +53,6 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use
} }
} }
let currently_playing = music_queue::get_now_playing(&guild_id);
music_queue::add_to_queue(&guild_id, query.to_string());
if currently_playing != "".to_string() {
// Add to queue
return Embed::create(username, "Added to queue", "The song was added to the queue.");
}
let query = music_queue::get_head(&guild_id).expect("Cannot get head of queue");
music_queue::set_now_playing(&guild_id, query.clone());
// Get query // Get query
let do_search = !query.starts_with("http"); let do_search = !query.starts_with("http");
let http_client = { let http_client = {
@ -71,13 +61,25 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use
.cloned() .cloned()
.expect("Guaranteed to exist in the typemap.") .expect("Guaranteed to exist in the typemap.")
}; };
// Create source // Create source
let mut src = if do_search { let mut src = if do_search {
YoutubeDl::new_search(http_client, query.to_string()) YoutubeDl::new_search(http_client, query.to_string())
} else { } else {
YoutubeDl::new(http_client, query.to_string()) YoutubeDl::new(http_client, query.to_string())
}; };
let currently_playing = music_queue::get_now_playing(&guild_id);
music_queue::add_to_queue(&guild_id, src.clone());
if currently_playing.is_some() {
// Add to queue
return Embed::create(username, "Added to queue", "The song was added to the queue.");
}
let query = music_queue::get_head(&guild_id).expect("Cannot get head of queue");
music_queue::set_now_playing(&guild_id, Some(src.clone()));
let handler_lock = match manager.get(*guild_id) { let handler_lock = match manager.get(*guild_id) {
Some(handler) => handler, Some(handler) => handler,
None => { None => {
@ -97,7 +99,7 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use
cmdctx: Arc::new(ctx.clone()), cmdctx: Arc::new(ctx.clone()),
}, },
); );
// Get metadata // Get metadata
let metadata = src.aux_metadata().await.expect("Cannot get metadata"); let metadata = src.aux_metadata().await.expect("Cannot get metadata");
let title = metadata.title.unwrap_or("Unknown title".to_string()); let title = metadata.title.unwrap_or("Unknown title".to_string());
@ -111,8 +113,27 @@ pub async unsafe fn attempt_to_queue_song(ctx: &Context, guild_id: &GuildId, use
.thumbnail(thumbnail) .thumbnail(thumbnail)
} }
pub async unsafe fn play() { pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, target: YoutubeDl) {
let manager = &songbird::get(ctx) // TODO match
.await
.expect("Cannot get Songbird.")
.clone();
if !user_util::is_self_connected_to_vc(ctx, guild_id).await {
println!("Bot is not connected to a VC, cannot play.");
return;
}
music_queue::set_now_playing(&guild_id, Some(target.clone()));
let handler_lock = match manager.get(*guild_id) {
Some(handler) => handler,
None => {
return
},
};
let mut handler = handler_lock.lock().await;
let _ = handler.play_input(target.into()); // TODO: Add event handlers
} }
pub async unsafe fn attempt_to_stop(ctx: &Context, guild_id: &GuildId, user_id: &UserId, username: &str) -> CreateEmbed { pub async unsafe fn attempt_to_stop(ctx: &Context, guild_id: &GuildId, user_id: &UserId, username: &str) -> CreateEmbed {
@ -157,11 +178,6 @@ pub async unsafe fn attempt_to_stop(ctx: &Context, guild_id: &GuildId, user_id:
} }
} }
/// pub async unsafe fn play_song(ctx: &Context, guild_id: &GuildId, query: String) -> Result<bool, JoinError> {
/// }
// Make the bot leave the voice channel. Returns Ok(true) if bot was connected, returns Ok(false) if bot was not connected. Returns Err if something went wrong. // Make the bot leave the voice channel. Returns Ok(true) if bot was connected, returns Ok(false) if bot was not connected. Returns Err if something went wrong.
pub async fn stop(ctx: &Context, guild_id: &GuildId) -> Result<bool, JoinError> { pub async fn stop(ctx: &Context, guild_id: &GuildId) -> Result<bool, JoinError> {
let manager = songbird::get(ctx) let manager = songbird::get(ctx)

View file

@ -1,9 +1,10 @@
use serenity::all::GuildId; use serenity::all::GuildId;
use songbird::input::YoutubeDl;
pub struct MusicQueue { // God this sucks. This needs to be reprogrammed properly. pub struct MusicQueue { // God this sucks. This needs to be reprogrammed properly.
guild_id: GuildId, guild_id: GuildId,
pub queue: Vec<String>, pub queue: Vec<YoutubeDl>,
pub now_playing: String, pub now_playing: Option<YoutubeDl>,
} }
static mut MUSIC_QUEUE: Vec<MusicQueue> = Vec::new(); static mut MUSIC_QUEUE: Vec<MusicQueue> = Vec::new();
@ -16,7 +17,7 @@ pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue {
let new_queue = MusicQueue { let new_queue = MusicQueue {
guild_id: *guild_id, guild_id: *guild_id,
queue: Vec::new(), queue: Vec::new(),
now_playing: "".to_string(), now_playing: None,
}; };
MUSIC_QUEUE.push(new_queue); MUSIC_QUEUE.push(new_queue);
MUSIC_QUEUE.iter().find(|q| q.guild_id == *guild_id).expect("Cannot get queue") MUSIC_QUEUE.iter().find(|q| q.guild_id == *guild_id).expect("Cannot get queue")
@ -24,7 +25,7 @@ pub unsafe fn get_queue(guild_id: &GuildId) -> &MusicQueue {
} }
} }
unsafe fn update_queue(guild_id: &GuildId, queue: Vec<String>, now_playing: String) { 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"); let index = MUSIC_QUEUE.iter().position(|q| q.guild_id == *guild_id).expect("Cannot get index");
MUSIC_QUEUE[index] = MusicQueue{ MUSIC_QUEUE[index] = MusicQueue{
guild_id: *guild_id, guild_id: *guild_id,
@ -38,14 +39,14 @@ pub unsafe fn delete_queue(guild_id: &GuildId) {
MUSIC_QUEUE.remove(index); MUSIC_QUEUE.remove(index);
} }
pub unsafe fn add_to_queue(guild_id: &GuildId, input: String) { pub unsafe fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) {
let queue = get_queue(guild_id); let queue = get_queue(guild_id);
let mut new_queue = queue.queue.clone(); let mut new_queue = queue.queue.clone();
new_queue.push(input); new_queue.push(input);
update_queue(guild_id, new_queue, queue.now_playing.clone()); update_queue(guild_id, new_queue, queue.now_playing.clone());
} }
pub unsafe fn get_head(guild_id: &GuildId) -> Option<String> { pub unsafe fn get_head(guild_id: &GuildId) -> Option<YoutubeDl> {
let queue = get_queue(guild_id); let queue = get_queue(guild_id);
let mut q = queue.queue.clone(); let mut q = queue.queue.clone();
let result = q.first().map(|s| s.clone()); let result = q.first().map(|s| s.clone());
@ -54,12 +55,12 @@ pub unsafe fn get_head(guild_id: &GuildId) -> Option<String> {
result result
} }
pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: String) { pub unsafe fn set_now_playing(guild_id: &GuildId, now_playing: Option<YoutubeDl>) {
let queue = get_queue(guild_id); let queue = get_queue(guild_id);
update_queue(guild_id, queue.queue.clone(), now_playing); update_queue(guild_id, queue.queue.clone(), now_playing);
} }
pub unsafe fn get_now_playing(guild_id: &GuildId) -> String { pub unsafe fn get_now_playing(guild_id: &GuildId) -> &Option<YoutubeDl> {
let queue = get_queue(guild_id); let queue = get_queue(guild_id);
queue.now_playing.clone() &queue.now_playing
} }