77 lines
2.4 KiB
Rust
77 lines
2.4 KiB
Rust
|
use serenity::all::{ChannelId, GuildId};
|
||
|
use serenity::async_trait;
|
||
|
|
||
|
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
|
||
|
use songbird::TrackEvent;
|
||
|
|
||
|
use crate::{types::{Context, Error}, util::utilities::get_channel_by_name};
|
||
|
|
||
|
/// 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
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn connect(ctx: &Context<'_>, guild_id: GuildId, channel: Option<String>) -> Result<(), Error> {
|
||
|
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;
|
||
|
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
pub 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()
|
||
|
}
|
||
|
}
|