moover_rust/src/commands/voice/player_common.rs

164 lines
4.3 KiB
Rust
Raw Normal View History

2024-12-17 14:28:52 +00:00
use std::vec;
2025-01-26 20:45:24 +00:00
use poise::CreateReply;
2024-12-17 14:28:52 +00:00
use songbird::input::{File, Input};
2025-01-24 20:52:13 +00:00
use crate::utils::debug::send_error;
2025-01-26 20:45:24 +00:00
use crate::types::{Context, ContextExt, Error};
2025-01-24 20:52:13 +00:00
use crate::utils::utilities::get_local_songs;
2024-12-17 14:28:52 +00:00
2025-01-24 20:52:13 +00:00
use super::voice_utils::{connect, autocomplete_channel};
2024-12-17 14:28:52 +00:00
2025-01-26 22:47:57 +00:00
/************************************************************
* Common commands that are the same for every implementation
2025-01-26 22:47:57 +00:00
************************************************************/
2024-12-17 14:28:52 +00:00
2025-01-26 22:47:57 +00:00
/// Disconnect bot from voice channel
2024-12-17 14:28:52 +00:00
#[poise::command(
slash_command,
description_localized("en-US", "Disconnect from voice channel"),
category = "Voice"
2024-12-17 14:28:52 +00:00
)]
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?;
2024-12-17 14:28:52 +00:00
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?;
2024-12-17 14:28:52 +00:00
return Ok(())
}
Err(e) => {
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
return Ok(())
}
}
}
2025-01-26 21:47:06 +00:00
#[allow(dead_code)]
2024-12-17 14:28:52 +00:00
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"),
category = "Voice"
2024-12-17 14:28:52 +00:00
)]
pub async fn play_local(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "]
2025-01-06 11:51:57 +00:00
channel: Option<String>,
2024-12-17 14:28:52 +00:00
#[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(())
};
2025-01-06 11:51:57 +00:00
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(())
}
}
}
2024-12-17 14:28:52 +00:00
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));
2025-01-06 11:51:57 +00:00
let _ = handler.play_only_input(input);
2024-12-17 14:28:52 +00:00
}
else {
ctx.reply_ephemeral("Not in a voice channel").await?;
return Ok(())
}
ctx.reply_ephemeral("Done!").await?;
Ok(())
}
2025-01-26 20:45:24 +00:00
2025-01-26 22:47:57 +00:00
/// Sends embed with some info about currently playing source
2025-01-26 20:45:24 +00:00
#[poise::command(
slash_command,
description_localized("en-US", "Display currently playing info"),
category = "Voice"
2025-01-26 20:45:24 +00:00
)]
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(())
}