Rustendo/src/music/music_events.rs

57 lines
2 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use serenity::all::{ChannelId, GuildId, Http};
use serenity::async_trait;
use songbird::{Event, EventContext, EventHandler};
2024-03-09 00:25:12 +01:00
use crate::music::{music_manager, music_queue};
pub struct TrackEndNotifier {
pub guild_id: GuildId,
pub channel_id: ChannelId,
pub http: Arc<Http>,
pub cmdctx: Arc<serenity::client::Context>,
}
#[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(track_list) = ctx {
println!("The track ended!");
2024-03-09 00:25:12 +01:00
let music_queue = music_queue::get_queue(&self.guild_id);
let q = &music_queue.queue;
if q.len() == 0 {
// No more songs in queue, exit the vc
let stopped = match music_manager::stop(&self.cmdctx, &self.guild_id).await {
Ok(stopped) => stopped,
Err(e) => {
println!("Cannot stop: {:?}", e);
return None;
}
};
if stopped {
println!("Stopped playing successfully.");
} else {
println!("Failed to stop playing.");
}
return None;
}
2024-03-09 00:25:12 +01:00
let head = music_queue::get_head(&self.guild_id);
if head.is_none() {
println!("Cannot get head of queue");
return None;
}
let head = head.unwrap();
/*let started = match music_manager::play(&self.cmdctx, &self.guild_id, &head).await {
Ok(started) => started,
Err(e) => {
println!("Cannot play: {:?}", e);
return None;
}
}; */
}
}
None
}
}