From dbaf1a1374190d89753c57a756d5087b3abdb86f Mon Sep 17 00:00:00 2001 From: Johannes Heuel Date: Tue, 18 Jun 2024 15:49:58 +0200 Subject: [PATCH] reset loop state on leave/stop --- src/commands/join.rs | 3 +++ src/commands/leave.rs | 30 ++++++++++++++++++++++++++---- src/commands/loop_queue.rs | 8 +------- src/commands/mod.rs | 1 + src/commands/stop.rs | 4 ++++ src/handler.rs | 12 ++++-------- src/state.rs | 12 ++++++++++++ 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/commands/join.rs b/src/commands/join.rs index defc93e..efa2148 100644 --- a/src/commands/join.rs +++ b/src/commands/join.rs @@ -38,6 +38,9 @@ pub(crate) async fn join_channel( call.deafen(true).await?; } + // create guild config + state.guild_settings.entry(guild_id).or_default(); + Ok(()) } diff --git a/src/commands/leave.rs b/src/commands/leave.rs index 9d6cd06..3f53ff6 100644 --- a/src/commands/leave.rs +++ b/src/commands/leave.rs @@ -1,6 +1,26 @@ -use crate::state::State; -use std::error::Error; -use twilight_model::gateway::payload::incoming::InteractionCreate; +use crate::state::{State, StateRef}; +use std::{error::Error, sync::Arc}; +use twilight_model::{ + gateway::payload::incoming::InteractionCreate, + id::{marker::GuildMarker, Id}, +}; + +pub(crate) async fn leave_channel( + guild_id: Id, + state: Arc, +) -> Result<(), Box> { + // stop playing + if let Some(call_lock) = state.songbird.get(guild_id) { + let call = call_lock.lock().await; + call.queue().stop(); + } + // leave the voice channel + state.songbird.leave(guild_id).await?; + + // reset guild settings + state.guild_settings.remove(&guild_id); + Ok(()) +} pub(crate) async fn leave( interaction: Box, @@ -16,6 +36,8 @@ pub(crate) async fn leave( let Some(guild_id) = interaction.guild_id else { return Ok(()); }; - state.songbird.leave(guild_id).await?; + + leave_channel(guild_id, Arc::clone(&state)).await?; + Ok(()) } diff --git a/src/commands/loop_queue.rs b/src/commands/loop_queue.rs index 689c09b..f79d079 100644 --- a/src/commands/loop_queue.rs +++ b/src/commands/loop_queue.rs @@ -1,5 +1,5 @@ use crate::metadata::MetadataMap; -use crate::state::{Settings, State, StateRef}; +use crate::state::{State, StateRef}; use async_trait::async_trait; use songbird::{Event, EventContext, EventHandler, TrackEvent}; use std::ops::Sub; @@ -29,14 +29,8 @@ pub(crate) async fn loop_queue( return Ok(()); }; - state - .guild_settings - .entry(guild_id) - .or_insert_with(|| Settings { loop_queue: false }); - state.guild_settings.entry(guild_id).and_modify(|settings| { settings.loop_queue = !settings.loop_queue; - println!("loop_queue: {}", settings.loop_queue); }); let looping = state diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 318bbc9..4213714 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,6 +3,7 @@ pub(crate) use join::join; mod leave; pub(crate) use leave::leave; +pub(crate) use leave::leave_channel; mod pause; pub(crate) use pause::pause; diff --git a/src/commands/stop.rs b/src/commands/stop.rs index d171616..60708d9 100644 --- a/src/commands/stop.rs +++ b/src/commands/stop.rs @@ -23,6 +23,10 @@ pub(crate) async fn stop( return Ok(()); }; + state.guild_settings.entry(guild_id).and_modify(|settings| { + settings.loop_queue = false; + }); + if let Some(call_lock) = state.songbird.get(guild_id) { let call = call_lock.lock().await; call.queue().stop(); diff --git a/src/handler.rs b/src/handler.rs index 09a9085..36f9622 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,5 +1,7 @@ use crate::commands::queue::{build_action_row, build_queue_embeds, TRACKS_PER_PAGE}; -use crate::commands::{delete, join, leave, loop_queue, pause, play, queue, resume, skip, stop}; +use crate::commands::{ + delete, join, leave, leave_channel, loop_queue, pause, play, queue, resume, skip, stop, +}; use crate::state::State; use futures::Future; use std::error::Error; @@ -63,13 +65,7 @@ pub(crate) async fn leave_if_alone( // count is 1 if the bot is the only one in the channel if count == 1 { - // stop playing - if let Some(call_lock) = state.songbird.get(guild_id) { - let call = call_lock.lock().await; - call.queue().stop(); - } - // leave the voice channel - state.songbird.leave(guild_id).await?; + leave_channel(guild_id, Arc::clone(&state)).await?; } Ok(()) } diff --git a/src/state.rs b/src/state.rs index df881bc..8ac8ac8 100644 --- a/src/state.rs +++ b/src/state.rs @@ -13,6 +13,18 @@ pub(crate) struct Settings { pub(crate) loop_queue: bool, } +impl Settings { + pub(crate) fn new() -> Self { + Self { loop_queue: false } + } +} + +impl Default for Settings { + fn default() -> Self { + Self::new() + } +} + #[derive(Debug)] pub(crate) struct StateRef { pub(crate) http: HttpClient,