2025-01-31 22:24:33 +00:00
|
|
|
use std::sync::Arc;
|
2025-02-01 23:11:29 +00:00
|
|
|
use std::time::Duration;
|
2025-01-31 22:24:33 +00:00
|
|
|
|
2025-02-01 23:11:29 +00:00
|
|
|
use serenity::all::{CacheHttp, ChannelId, CreateMessage, GuildId};
|
2025-01-06 11:51:57 +00:00
|
|
|
use serenity::async_trait;
|
|
|
|
|
|
|
|
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
|
2025-01-31 22:24:33 +00:00
|
|
|
use songbird::{Songbird, TrackEvent};
|
2025-02-01 23:11:29 +00:00
|
|
|
use tokio::time::sleep;
|
2025-01-06 11:51:57 +00:00
|
|
|
|
2025-01-31 22:26:04 +00:00
|
|
|
use crate::{types::Context, utils::utilities::get_channel_by_name};
|
2025-01-24 20:52:13 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-01 23:11:29 +00:00
|
|
|
struct TrackEventNotifier {
|
|
|
|
ctx: serenity::all::Context,
|
|
|
|
guild_id: GuildId,
|
|
|
|
channel_id: ChannelId
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TrackEventNotifier {
|
|
|
|
fn new(ctx: serenity::all::Context,
|
|
|
|
guild_id: GuildId,
|
|
|
|
channel_id: ChannelId) -> TrackEventNotifier {
|
|
|
|
TrackEventNotifier {
|
|
|
|
ctx,
|
|
|
|
guild_id,
|
|
|
|
channel_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl VoiceEventHandler for TrackEventNotifier {
|
|
|
|
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
|
|
|
if let EventContext::Track(track_list) = ctx {
|
|
|
|
for (_, _) in *track_list {
|
|
|
|
sleep(Duration::from_secs(60)).await;
|
|
|
|
let manager = songbird::get(&self.ctx)
|
|
|
|
.await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.")
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
if let Some(handler_lock) = manager.get(self.guild_id) {
|
|
|
|
let handler = handler_lock.lock().await;
|
|
|
|
if !handler.queue().is_empty() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(handler);
|
|
|
|
drop(handler_lock);
|
|
|
|
match manager.remove(self.guild_id).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
dbg!(e);
|
|
|
|
()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = self.channel_id.say(self.ctx.http(), "Disconnected to save bandwidth!").await;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct TrackErrorNotifier {
|
|
|
|
ctx: serenity::all::Context,
|
|
|
|
channel_id: ChannelId
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TrackErrorNotifier {
|
|
|
|
fn new(ctx: serenity::all::Context, channel_id: ChannelId) -> TrackErrorNotifier {
|
|
|
|
TrackErrorNotifier {
|
|
|
|
ctx,
|
|
|
|
channel_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-01-06 11:51:57 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl VoiceEventHandler for TrackErrorNotifier {
|
|
|
|
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
|
|
|
if let EventContext::Track(track_list) = ctx {
|
2025-02-01 23:11:29 +00:00
|
|
|
for (_, _) in *track_list {
|
|
|
|
match self.channel_id.send_message(
|
|
|
|
self.ctx.http(),
|
|
|
|
CreateMessage::new().content("There was an error when playing a track!")
|
|
|
|
).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => println!("{}", e)
|
|
|
|
};
|
2025-01-06 11:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-01 23:11:29 +00:00
|
|
|
|
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() {
|
2025-01-31 22:26:04 +00:00
|
|
|
return Err("This command can be used only in guild".to_string())
|
2025-01-31 22:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let Some(guild_id) = ctx.guild_id() else {
|
2025-01-31 22:26:04 +00:00
|
|
|
return Err("Guild id not found".to_string())
|
2025-01-31 22:24:33 +00:00
|
|
|
};
|
|
|
|
|
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-02-01 23:11:29 +00:00
|
|
|
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier::new(ctx.serenity_context().clone(), voice_channel));
|
2025-01-31 22:24:33 +00:00
|
|
|
for event in events {
|
2025-02-01 23:11:29 +00:00
|
|
|
handler.add_global_event(
|
|
|
|
event.into(),
|
|
|
|
TrackEventNotifier::new(
|
|
|
|
ctx.serenity_context().clone(),
|
|
|
|
guild_id,
|
|
|
|
voice_channel
|
|
|
|
)
|
|
|
|
);
|
2025-01-31 22:24:33 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|