moover_rust/src/commands/voice/player.rs

256 lines
6.9 KiB
Rust
Raw Normal View History

2024-12-17 14:28:52 +00:00
use std::vec;
use serenity::{all::ChannelId, async_trait};
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
use songbird::input::{File, Input};
use songbird::TrackEvent;
use crate::util::debug::send_error;
use crate::util::poise_context_extension::ContextExt;
use crate::util::utilities::get_local_songs;
use crate::{types::{Context, Error}, util::utilities::get_channel_by_name};
#[poise::command(
slash_command,
description_localized("en-US", "Play song")
)]
pub async fn play(ctx: Context<'_>,
#[description = "Song: "]
_url: String
) -> Result<(), Error> {
ctx.reply("Done!").await?;
Ok(())
}
/// 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<String>) -> Result<ChannelId, String> {
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())
}
}
}
#[poise::command(
slash_command,
description_localized("en-US", "Connect to channel")
)]
pub async fn connect(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "]
name: Option<String>
) -> Result<(), Error> {
if ctx.guild().is_none() {
ctx.reply_ephemeral("Can't use this outside of guild").await?;
return Ok(());
}
let voice_channel = match get_voice_channel(&ctx, name).await {
Ok(c) => c,
Err(e) => {
ctx.reply_ephemeral(e.as_str()).await?;
return Ok(())
}
};
let manager = songbird::get(ctx.serenity_context())
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
let Some(guild_id) = ctx.guild_id() else {
ctx.reply_ephemeral("Guild id not found").await?;
return Ok(())
};
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);
}
ctx.reply_ephemeral("Done!").await?;
Ok(())
}
struct TrackErrorNotifier;
#[async_trait]
impl VoiceEventHandler for TrackErrorNotifier {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
if let EventContext::Track(track_list) = ctx {
for (state, handle) in *track_list {
println!(
"Track {:?} encountered an error: {:?}",
handle.uuid(),
state.playing
);
}
}
None
}
}
async fn autocomplete_channel(
ctx: Context<'_>,
_partial: &str,
) -> Vec<String> {
use crate::util::utilities::get_vc_names;
match ctx.guild() {
Some(guild) => get_vc_names(guild),
None => [].to_vec()
}
}
#[poise::command(
slash_command,
description_localized("en-US", "Disconnect from voice channel")
)]
pub async fn disconnect(
ctx: Context<'_>
) -> Result<(), Error> {
let Some(guild_id) = ctx.guild_id() else {
ctx.reply("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("I am not connected to a channel!").await?;
return Ok(())
}
match manager.remove(guild_id).await {
Ok(()) => {
ctx.reply("Disconnected").await?;
return Ok(())
}
Err(e) => {
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
return Ok(())
}
}
}
async fn autocomplete_song(
_ctx: Context<'_>,
partial: &str,
) -> Vec<String> {
match get_local_songs(partial) {
Ok(names) => names,
Err(_) => vec![]
}
}
#[poise::command(
slash_command,
description_localized("en-US", "Connect to channel")
)]
pub async fn play_local(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "]
_channel: Option<String>,
#[autocomplete = "autocomplete_song"]
#[description = "Filename of local song: "]
file_name: String
) -> Result<(), Error> {
if ctx.guild().is_none() {
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 Some(guild_id) = ctx.guild_id() else {
ctx.reply_ephemeral("Guild id not found").await?;
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_input(input);
}
else {
ctx.reply_ephemeral("Not in a voice channel").await?;
return Ok(())
}
ctx.reply_ephemeral("Done!").await?;
Ok(())
}
// #[poise::command(
// slash_command,
// description_localized("en-US", "Connect to channel")
// )]
// pub async fn play_local(ctx: Context<'_>,
// #[autocomplete = "autocomplete_song"]
// #[description = "Voice channel name: "]
// file_name: String
// ) -> Result<(), Error> {
// if ctx.guild().is_none() {
// 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 Some(guild_id) = ctx.guild_id() else {
// ctx.reply_ephemeral("Guild id not found").await?;
// 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_input(input);
// }
// else {
// ctx.reply_ephemeral("Not in a voice channel").await?;
// return Ok(())
// }
// ctx.reply_ephemeral("Done!").await?;
// Ok(())
// }