fixed playing greet on all voice actions

This commit is contained in:
jellywx 2021-03-15 23:38:05 +00:00
parent 35b3cb81c9
commit 4f2b57a9b3

View File

@ -136,60 +136,62 @@ impl EventHandler for Handler {
&self, &self,
ctx: Context, ctx: Context,
guild_id_opt: Option<GuildId>, guild_id_opt: Option<GuildId>,
_old: Option<VoiceState>, old: Option<VoiceState>,
new: VoiceState, new: VoiceState,
) { ) {
if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { if old.is_none() {
if let Some(guild) = ctx.cache.guild(guild_id).await { if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
let pool = ctx if let Some(guild) = ctx.cache.guild(guild_id).await {
.data let pool = ctx
.read() .data
.await .read()
.get::<MySQL>() .await
.cloned() .get::<MySQL>()
.expect("Could not get SQLPool from data"); .cloned()
.expect("Could not get SQLPool from data");
let guild_data_opt = GuildData::get_from_id(guild.clone(), pool.clone()).await; let guild_data_opt = GuildData::get_from_id(guild.clone(), pool.clone()).await;
if let Some(guild_data) = guild_data_opt { if let Some(guild_data) = guild_data_opt {
if guild_data.allow_greets { if guild_data.allow_greets {
let join_id_res = sqlx::query!( 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,
guild_data, guild_data,
&mut handler.lock().await, &mut handler.lock().await,
pool, pool,
false, false,
) )
.await; .await;
}
} }
} }
} }
@ -198,6 +200,15 @@ SELECT name, id, plays, public, server_id, uploader_id
} }
} }
struct LeaveInactive;
#[async_trait]
impl SongbirdEventHandler for LeaveInactive {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
None
}
}
async fn play_audio( async fn play_audio(
sound: &mut Sound, sound: &mut Sound,
guild: GuildData, guild: GuildData,
@ -239,7 +250,7 @@ async fn join_channel(
.get(&current_user) .get(&current_user)
.and_then(|voice_state| voice_state.channel_id); .and_then(|voice_state| voice_state.channel_id);
if current_voice_state == Some(channel_id) { let (call, res) = if current_voice_state == Some(channel_id) {
let call_opt = songbird.get(guild.id); let call_opt = songbird.get(guild.id);
if let Some(call) = call_opt { if let Some(call) = call_opt {
@ -249,7 +260,14 @@ async fn join_channel(
} }
} else { } else {
songbird.join(guild.id, channel_id).await songbird.join(guild.id, channel_id).await
} };
call.lock().await.add_global_event(
Event::Periodic(Duration::from_secs(300), None),
LeaveInactive {},
);
(call, res)
} }
// entry point // entry point