moover_rust/src/commands/voice/player_common.rs

116 lines
3 KiB
Rust

use std::vec;
use songbird::input::{File, Input};
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};
use super::util::{connect, autocomplete_channel};
/**
* Common commands that are the same for every implementation
*/
#[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_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("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(())
}
}
}
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", "Play song from server storage")
)]
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 manager.get(guild_id).is_none() {
match connect(&ctx, guild_id, channel).await {
Ok(_) => (),
Err(e) => {
ctx.reply_ephemeral(&e.to_string()).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_only_input(input);
}
else {
ctx.reply_ephemeral("Not in a voice channel").await?;
return Ok(())
}
ctx.reply_ephemeral("Done!").await?;
Ok(())
}