autoleaves again

This commit is contained in:
jellywx 2021-04-09 10:48:51 +01:00
parent 11f388c65a
commit 626c21f38d
2 changed files with 70 additions and 58 deletions

20
Cargo.lock generated
View File

@ -695,9 +695,9 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.3" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11"
dependencies = [ dependencies = [
"bytes 1.0.1", "bytes 1.0.1",
"fnv", "fnv",
@ -717,9 +717,9 @@ dependencies = [
[[package]] [[package]]
name = "httparse" name = "httparse"
version = "1.3.5" version = "1.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" checksum = "bc35c995b9d93ec174cf9a27d425c7892722101e14993cd227fdb51d70cf9589"
[[package]] [[package]]
name = "httpdate" name = "httpdate"
@ -888,9 +888,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
[[package]] [[package]]
name = "libm" name = "libm"
@ -2016,9 +2016,9 @@ checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.68" version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" checksum = "48fe99c6bd8b1cc636890bcc071842de909d902c81ac7dab53ba33c421ab8ffb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2366,9 +2366,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.4" version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0"
dependencies = [ dependencies = [
"matches", "matches",
] ]

View File

@ -43,6 +43,7 @@ use dotenv::dotenv;
use crate::{framework::RegexFramework, guild_data::CtxGuildData}; use crate::{framework::RegexFramework, guild_data::CtxGuildData};
use dashmap::DashMap; use dashmap::DashMap;
use serenity::model::channel::Channel;
use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration}; use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration};
use tokio::sync::{MutexGuard, RwLock}; use tokio::sync::{MutexGuard, RwLock};
@ -141,69 +142,80 @@ impl EventHandler for Handler {
old: Option<VoiceState>, old: Option<VoiceState>,
new: VoiceState, new: VoiceState,
) { ) {
if old.is_none() { if let Some(past_state) = old {
if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { if let (Some(guild_id), None) = (guild_id_opt, new.channel_id) {
if let Some(guild) = ctx.cache.guild(guild_id).await { if let Some(channel_id) = past_state.channel_id {
let pool = ctx if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx).await
.data {
.read() if channel.members(&ctx).await.map(|m| m.len()).unwrap_or(0) <= 1 {
.await let songbird = songbird::get(&ctx).await.unwrap();
.get::<MySQL>()
.cloned()
.expect("Could not get SQLPool from data");
let guild_data_opt = ctx.get_from_id(guild.id).await; let _ = songbird.remove(guild_id).await;
if let Ok(guild_data) = guild_data_opt {
let volume;
let allowed_greets;
{
let read = guild_data.read().await;
volume = read.volume;
allowed_greets = read.allow_greets;
} }
}
}
}
} else if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
if let Some(guild) = ctx.cache.guild(guild_id).await {
let pool = ctx
.data
.read()
.await
.get::<MySQL>()
.cloned()
.expect("Could not get SQLPool from data");
if allowed_greets { let guild_data_opt = ctx.get_from_id(guild.id).await;
let join_id_res = sqlx::query!(
" if let Ok(guild_data) = guild_data_opt {
let volume;
let allowed_greets;
{
let read = guild_data.read().await;
volume = read.volume;
allowed_greets = read.allow_greets;
}
if allowed_greets {
let join_id_res = sqlx::query!(
"
SELECT join_sound_id SELECT join_sound_id
FROM users FROM users
WHERE user = ? AND join_sound_id IS NOT NULL WHERE user = ? AND join_sound_id IS NOT NULL
", ",
new.user_id.as_u64() new.user_id.as_u64()
) )
.fetch_one(&pool) .fetch_one(&pool)
.await; .await;
if let Ok(join_id_record) = join_id_res { if let Ok(join_id_record) = join_id_res {
let join_id = join_id_record.join_sound_id; let join_id = join_id_record.join_sound_id;
let mut sound = sqlx::query_as_unchecked!( let mut sound = sqlx::query_as_unchecked!(
Sound, Sound,
" "
SELECT name, id, plays, public, server_id, uploader_id SELECT name, id, plays, public, server_id, uploader_id
FROM sounds FROM sounds
WHERE id = ? WHERE id = ?
", ",
join_id join_id
) )
.fetch_one(&pool) .fetch_one(&pool)
.await .await
.unwrap(); .unwrap();
let (handler, _) = join_channel(&ctx, guild, user_channel).await; let (handler, _) = join_channel(&ctx, guild, user_channel).await;
let _ = play_audio( let _ = play_audio(
&mut sound, &mut sound,
volume, volume,
&mut handler.lock().await, &mut handler.lock().await,
pool, pool,
false, false,
) )
.await; .await;
}
} }
} }
} }