forked from DiscordBots/Rustendo
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
use crate::music::{music_manager, music_queue};
|
|
|
|
use serenity::all::{ChannelId, GuildId, Http};
|
|
use serenity::async_trait;
|
|
use songbird::{Event, EventContext, EventHandler};
|
|
use std::sync::Arc;
|
|
|
|
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> {
|
|
// TODO: Does this need to be unsafe?
|
|
if let EventContext::Track(..) = ctx {
|
|
println!("The track ended!");
|
|
|
|
if music_queue::is_empty(&self.guild_id).await {
|
|
// 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;
|
|
}
|
|
|
|
let head = match music_queue::get_head(&self.guild_id).await {
|
|
Some(head) => head,
|
|
None => {
|
|
println!("Cannot get head of queue");
|
|
return None;
|
|
}
|
|
};
|
|
|
|
music_manager::play_song(&self.cmdctx, &self.guild_id, &head).await;
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|