use std::sync::Arc; use serenity::all::{ChannelId, GuildId}; use serenity::async_trait; use serenity::model::voice; use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler}; use songbird::{Songbird, TrackEvent}; use crate::{types::{Context, Error}, 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 TrackErrorNotifier; #[async_trait] impl VoiceEventHandler for TrackErrorNotifier { async fn act(&self, ctx: &EventContext<'_>) -> Option { if let EventContext::Track(track_list) = ctx { for (state, handle) in *track_list { println!( "Track {:?} encountered an error: {:?}", handle.uuid(), state.playing ); } } None } } pub async fn connect(ctx: &Context<'_>, channel: Option, events: Vec) -> Result<(Arc, GuildId), String> { if ctx.guild().is_none() { return Err("dadsa".to_string()) } let Some(guild_id) = ctx.guild_id() else { return Err("dadsa".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; for event in events { handler.add_global_event(event.into(), TrackErrorNotifier); } } 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() } }