use std::sync::Arc; use std::time::Duration; use serenity::all::{CacheHttp, ChannelId, CreateMessage, GuildId}; use serenity::async_trait; use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler}; use songbird::{Songbird, TrackEvent}; use tokio::time::sleep; use crate::{types::Context, utils::utilities::get_channel_by_name}; pub const MAX_ENTRIES: &str = "15"; /// Returns either voice channel to which the user is currently connected to or the one passed via name async fn get_voice_channel(ctx: &Context<'_>, name: Option) -> Result { if name.is_none() || name.as_ref().is_some_and(|n| n.is_empty()) { match ctx.guild().and_then(|guild| guild.voice_states.get(&ctx.author().id).and_then(|voice_state| voice_state.channel_id ) ) { Some(c) => Ok(c), None => Err("You must be in a voice channel or specify explicit voice channel by name".to_string()) } } else { match ctx.guild().and_then(|guild| get_channel_by_name(guild, name.unwrap()) ) { Some(c) => Ok(c), None => Err("Channel with this name does not exist".to_string()) } } } struct TrackEventNotifier { ctx: serenity::all::Context, guild_id: GuildId, channel_id: ChannelId } impl TrackEventNotifier { fn new(ctx: serenity::all::Context, guild_id: GuildId, channel_id: ChannelId) -> TrackEventNotifier { TrackEventNotifier { ctx, guild_id, channel_id } } } #[async_trait] impl VoiceEventHandler for TrackEventNotifier { async fn act(&self, ctx: &EventContext<'_>) -> Option { if let EventContext::Track(track_list) = ctx { for (_, _) in *track_list { sleep(Duration::from_secs(60)).await; let manager = songbird::get(&self.ctx) .await .expect("Songbird Voice client placed in at initialisation.") .clone(); if let Some(handler_lock) = manager.get(self.guild_id) { let handler = handler_lock.lock().await; if !handler.queue().is_empty() { return None } drop(handler); drop(handler_lock); match manager.remove(self.guild_id).await { Ok(_) => (), Err(e) => { dbg!(e); () } } let _ = self.channel_id.say(self.ctx.http(), "Disconnected to save bandwidth!").await; }; } } None } } struct TrackErrorNotifier { ctx: serenity::all::Context, channel_id: ChannelId } impl TrackErrorNotifier { fn new(ctx: serenity::all::Context, channel_id: ChannelId) -> TrackErrorNotifier { TrackErrorNotifier { ctx, channel_id } } } #[async_trait] impl VoiceEventHandler for TrackErrorNotifier { async fn act(&self, ctx: &EventContext<'_>) -> Option { if let EventContext::Track(track_list) = ctx { for (_, _) in *track_list { match self.channel_id.send_message( self.ctx.http(), CreateMessage::new().content("There was an error when playing a track!") ).await { Ok(_) => (), Err(e) => println!("{}", e) }; } } None } } pub async fn connect(ctx: &Context<'_>, channel: Option, events: Vec) -> Result<(Arc, GuildId), String> { if ctx.guild().is_none() { return Err("This command can be used only in guild".to_string()) } let Some(guild_id) = ctx.guild_id() else { return Err("Guild id not found".to_string()) }; let voice_channel = get_voice_channel(&ctx, channel).await?; let manager = songbird::get(ctx.serenity_context()) .await .expect("Songbird Voice client placed in at initialisation.") .clone(); if let Ok(handler_lock) = manager.join(guild_id, voice_channel).await { let mut handler = handler_lock.lock().await; handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier::new(ctx.serenity_context().clone(), voice_channel)); for event in events { handler.add_global_event( event.into(), TrackEventNotifier::new( ctx.serenity_context().clone(), guild_id, voice_channel ) ); } } Ok((manager, guild_id)) } pub async fn autocomplete_channel( ctx: Context<'_>, _partial: &str, ) -> Vec { use crate::utils::utilities::get_vc_names; match ctx.guild() { Some(guild) => get_vc_names(guild), None => [].to_vec() } }