diff --git a/src/handlers.rs b/src/handlers.rs new file mode 100644 index 0000000..bf2d338 --- /dev/null +++ b/src/handlers.rs @@ -0,0 +1,2 @@ +pub mod message_handler; +pub mod voice_state_handler; diff --git a/src/message_handler.rs b/src/handlers/message_handler.rs similarity index 100% rename from src/message_handler.rs rename to src/handlers/message_handler.rs diff --git a/src/handlers/voice_state_handler.rs b/src/handlers/voice_state_handler.rs new file mode 100644 index 0000000..dec2f84 --- /dev/null +++ b/src/handlers/voice_state_handler.rs @@ -0,0 +1,75 @@ +use std::time::Duration; + +use serenity::all::{Context, CreateMessage, GuildChannel, VoiceState}; +use tokio::time::sleep; + +use crate::utils::{debug::send_error, voice_state_to_guild_channel}; + +async fn get_channel_info(ctx: &Context, voice_state: &VoiceState) -> Option { + let voice_channel = voice_state_to_guild_channel(&ctx, &voice_state).await?; + + let Ok(members) = voice_channel.members(&ctx.cache) else { + return None + }; + + let mut is_connected = false; + let mut users_connected: usize = 0; + for member in &members { + if member.user.id == ctx.cache.current_user().id { + is_connected = true + } + if ! member.user.bot { + users_connected += 1; + } + } + + // Check if there is no real user in the voice channel + if ! is_connected || users_connected > 0 { + return None; + } + + Some(voice_channel) +} + +pub async fn handle_voice_update(ctx: Context, voice_state_old: VoiceState, voice_state: VoiceState) -> Option<()> { + // The user did not disconnect so we don't need to handle if the bot was left alone + // Logic is as follows: + // User connected -> voice_state_old is None (we handle this before this function) + // User moved to different VC or disconnected -> we only check old channel for number of users anyway + if voice_state.channel_id.is_some() { + return None + } + + let mut voice_channel = get_channel_info(&ctx, &voice_state_old).await?; + + let manager = songbird::get(&ctx) + .await + .expect("Songbird Voice client placed in at initialisation.") + .clone(); + + // bot is not playing any music so we don't need to handle this + if manager.get(voice_channel.guild_id).is_none() { + return None; + } + + // There is a problem with this implementation + // if bot is left alone and users joins and disconnects while this counts down + // this is not a big problem since we want to disconnect anyway + sleep(Duration::from_secs(5)).await; + + voice_channel = get_channel_info(&ctx, &voice_state_old).await?; + + match manager.remove(voice_channel.guild_id).await { + Ok(()) => { + let _ = voice_channel.send_message(ctx.http, + CreateMessage::new() + .content("Disconnected to save bandwidth") + ).await; + } + Err(e) => { + let _ = send_error(ctx.http, e.to_string()).await; + } + } + + None +} diff --git a/src/main.rs b/src/main.rs index 5047ce2..7d38d84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::time::Duration; use std::error; use std::env; +use serenity::all::VoiceState; use serenity::async_trait; use serenity::futures::lock::Mutex; use serenity::prelude::GatewayIntents; @@ -19,12 +20,14 @@ use reqwest::Client as HttpClient; use dotenv::dotenv; use songbird::SerenityInit; + use tokio_cron_scheduler::{JobScheduler, Job}; use radiobrowser::RadioBrowserAPI; -mod message_handler; -use message_handler::handle; +mod handlers; +use handlers::message_handler::handle; +use handlers::voice_state_handler::handle_voice_update; mod commands; mod utils; @@ -35,6 +38,7 @@ use other::notice; mod types; use types::{Data, Error}; + struct Handler; async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { @@ -53,6 +57,15 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { #[async_trait] impl EventHandler for Handler { + async fn voice_state_update(&self, ctx: Context, old: Option, new: VoiceState) { + // User was not connected to any channel and connected to VC + let Some(old_channel) = old else { + return + }; + + handle_voice_update(ctx, old_channel, new).await; + } + async fn message(&self, ctx: Context, msg: Message) { handle(ctx, msg).await; } @@ -113,11 +126,11 @@ async fn main() -> anyhow::Result<()> { ..Default::default() }; - + let framework = poise::Framework::builder() .setup(move |ctx, _ready, framework| { Box::pin(async move { - + #[cfg(feature="GUILD_COMMAND")] { use poise::samples::register_in_guild; use serenity::all::GuildId; @@ -125,7 +138,7 @@ async fn main() -> anyhow::Result<()> { let debug_guild_id = env::var("DEBUG_GUILD_ID") .context("DEBUG_GUILD_ID not found in env")? .parse::().unwrap(); - + register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?; } @@ -134,7 +147,7 @@ async fn main() -> anyhow::Result<()> { register_globally(ctx, &framework.options().commands).await?; } - + Ok(Data { http_client: HttpClient::new(), radio_browser: RadioBrowserAPI::new().await?, diff --git a/src/utils/utilities.rs b/src/utils/utilities.rs index 1af175f..8d915f8 100644 --- a/src/utils/utilities.rs +++ b/src/utils/utilities.rs @@ -1,6 +1,6 @@ use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, vec}; -use serenity::{all::{ChannelId, ChannelType, CreateMessage, GuildId, GuildRef, Message}, http::Http}; +use serenity::{all::{ChannelId, ChannelType, Context, CreateMessage, GuildChannel, GuildId, GuildRef, Message, VoiceState}, http::Http}; pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result { @@ -15,6 +15,16 @@ pub async fn replace_msg(http: Arc, msg: Message, content: String) -> Resu return ChannelId::new(msg.channel_id.get()).send_message(http.clone(), CreateMessage::new().content(content)).await; } +pub async fn voice_state_to_guild_channel(ctx: &Context, voice_state: &VoiceState) -> Option { + let Ok(channel) = voice_state.channel_id? + .to_channel(&ctx.http) + .await else { + return None + }; + + Some(channel.guild()?) +} + pub fn get_vc_names(guild: GuildRef) -> Vec { let mut result: Vec = [].to_vec();