feat: add autodisconnect when bot is alone
This commit is contained in:
parent
7efd98f1f3
commit
976fa24aa4
5 changed files with 107 additions and 7 deletions
2
src/handlers.rs
Normal file
2
src/handlers.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod message_handler;
|
||||||
|
pub mod voice_state_handler;
|
75
src/handlers/voice_state_handler.rs
Normal file
75
src/handlers/voice_state_handler.rs
Normal file
|
@ -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<GuildChannel> {
|
||||||
|
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
|
||||||
|
}
|
17
src/main.rs
17
src/main.rs
|
@ -6,6 +6,7 @@ use std::time::Duration;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
use serenity::all::VoiceState;
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::futures::lock::Mutex;
|
use serenity::futures::lock::Mutex;
|
||||||
use serenity::prelude::GatewayIntents;
|
use serenity::prelude::GatewayIntents;
|
||||||
|
@ -19,12 +20,14 @@ use reqwest::Client as HttpClient;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
use songbird::SerenityInit;
|
use songbird::SerenityInit;
|
||||||
|
|
||||||
use tokio_cron_scheduler::{JobScheduler, Job};
|
use tokio_cron_scheduler::{JobScheduler, Job};
|
||||||
|
|
||||||
use radiobrowser::RadioBrowserAPI;
|
use radiobrowser::RadioBrowserAPI;
|
||||||
|
|
||||||
mod message_handler;
|
mod handlers;
|
||||||
use message_handler::handle;
|
use handlers::message_handler::handle;
|
||||||
|
use handlers::voice_state_handler::handle_voice_update;
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
@ -35,6 +38,7 @@ use other::notice;
|
||||||
mod types;
|
mod types;
|
||||||
use types::{Data, Error};
|
use types::{Data, Error};
|
||||||
|
|
||||||
|
|
||||||
struct Handler;
|
struct Handler;
|
||||||
|
|
||||||
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||||
|
@ -53,6 +57,15 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
|
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, 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) {
|
async fn message(&self, ctx: Context, msg: Message) {
|
||||||
handle(ctx, msg).await;
|
handle(ctx, msg).await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, vec};
|
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<ChannelId> {
|
pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
|
||||||
|
@ -15,6 +15,16 @@ pub async fn replace_msg(http: Arc<Http>, msg: Message, content: String) -> Resu
|
||||||
return ChannelId::new(msg.channel_id.get()).send_message(http.clone(), CreateMessage::new().content(content)).await;
|
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<GuildChannel> {
|
||||||
|
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<String> {
|
pub fn get_vc_names(guild: GuildRef) -> Vec<String> {
|
||||||
|
|
||||||
let mut result: Vec<String> = [].to_vec();
|
let mut result: Vec<String> = [].to_vec();
|
||||||
|
|
Loading…
Reference in a new issue