use std::vec; use poise::CreateReply; use songbird::input::{File, Input}; use songbird::TrackEvent; use crate::utils::debug::send_error; use crate::types::{Context, ContextExt, Error}; use crate::utils::utilities::get_local_songs; use super::voice_utils::{connect, autocomplete_channel}; /************************************************************ * Common commands that are the same for every implementation ************************************************************/ /// Disconnect bot from voice channel #[poise::command( slash_command, description_localized("en-US", "Disconnect from voice channel"), category = "Voice", guild_only )] pub async fn disconnect( ctx: Context<'_> ) -> Result<(), Error> { let Some(guild_id) = ctx.guild_id() else { ctx.reply_ephemeral("Can't use this outside of guild").await?; return Ok(()); }; let manager = songbird::get(ctx.serenity_context()) .await .expect("Songbird Voice client placed in at initialisation.") .clone(); let has_handler = manager.get(guild_id).is_some(); if ! has_handler { ctx.reply_ephemeral("I am not connected to a channel!").await?; return Ok(()) } match manager.remove(guild_id).await { Ok(()) => { ctx.reply_ephemeral("Disconnected").await?; return Ok(()) } Err(e) => { let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await; return Ok(()) } } } #[allow(dead_code)] async fn autocomplete_song( _ctx: Context<'_>, partial: &str, ) -> Vec { match get_local_songs(partial) { Ok(names) => names, Err(_) => vec![] } } #[poise::command( slash_command, description_localized("en-US", "Play song from server storage"), category = "Voice", guild_only )] pub async fn play_local(ctx: Context<'_>, #[autocomplete = "autocomplete_channel"] #[description = "Voice channel name: "] channel: Option, #[autocomplete = "autocomplete_song"] #[description = "Filename of local song: "] file_name: String ) -> Result<(), Error> { let events = vec![TrackEvent::End]; let (manager, guild_id) = match connect(&ctx, channel, events).await { Ok(result) => result, Err(e) => { ctx.reply_ephemeral(&e.to_string()).await?; println!("SONGBIRD MANAGER ERROR: {}", e.to_string()); return Ok(()) } }; if let Some(handler_lock) = manager.get(guild_id) { let mut handler = handler_lock.lock().await; let input_file = File::new(format!("/home/emil/Music/{file_name}")); let input = Input::Lazy(Box::new(input_file)); let _ = handler.play_only_input(input); } else { ctx.reply_ephemeral("Not in a voice channel").await?; return Ok(()) } ctx.reply_ephemeral("Done!").await?; Ok(()) } /// Sends embed with some info about currently playing source #[poise::command( slash_command, description_localized("en-US", "Display currently playing info"), category = "Voice", guild_only )] pub async fn playing(ctx: Context<'_>) -> Result<(), Error> { let Some(guild_id) = ctx.guild_id() else { ctx.reply_ephemeral("Guild id not found").await?; return Ok(()) }; let manager = songbird::get(ctx.serenity_context()) .await .expect("Songbird Voice client placed in at initialisation.") .clone(); let Some(_) = manager.get(guild_id) else { ctx.reply_ephemeral("I am not connected to any voice channel right now").await?; return Ok(()) }; let embed = { let mutex_hashmap = ctx.data().playing_info.lock().await; let Some(playing_info) = mutex_hashmap.get(&guild_id) else { ctx.reply_ephemeral("Entry not found, try to reconnect me").await?; return Ok(()); }; playing_info.generate_embed().await }; ctx.send( CreateReply::default() .embed(embed) ).await?; Ok(()) }