moover_rust/src/commands/voice/voice_utils.rs

92 lines
2.7 KiB
Rust
Raw Normal View History

2025-01-31 22:24:33 +00:00
use std::sync::Arc;
2025-01-26 20:45:24 +00:00
use serenity::all::{ChannelId, GuildId};
2025-01-06 11:51:57 +00:00
use serenity::async_trait;
2025-01-31 22:24:33 +00:00
use serenity::model::voice;
2025-01-06 11:51:57 +00:00
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
2025-01-31 22:24:33 +00:00
use songbird::{Songbird, TrackEvent};
2025-01-06 11:51:57 +00:00
2025-01-24 20:52:13 +00:00
use crate::{types::{Context, Error}, utils::utilities::get_channel_by_name};
pub const MAX_ENTRIES: &str = "15";
2025-01-06 11:51:57 +00:00
/// 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())
}
}
}
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
}
}
2025-01-31 22:24:33 +00:00
pub async fn connect(ctx: &Context<'_>, channel: Option<String>, events: Vec<TrackEvent>) -> Result<(Arc<Songbird>, 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())
};
2025-01-06 11:51:57 +00:00
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;
2025-01-31 22:24:33 +00:00
for event in events {
handler.add_global_event(event.into(), TrackErrorNotifier);
}
2025-01-06 11:51:57 +00:00
}
2025-01-31 22:24:33 +00:00
Ok((manager, guild_id))
2025-01-06 11:51:57 +00:00
}
pub async fn autocomplete_channel(
ctx: Context<'_>,
_partial: &str,
) -> Vec<String> {
2025-01-24 20:52:13 +00:00
use crate::utils::utilities::get_vc_names;
2025-01-06 11:51:57 +00:00
match ctx.guild() {
Some(guild) => get_vc_names(guild),
None => [].to_vec()
}
}