reset loop state on leave/stop

This commit is contained in:
2024-06-18 15:49:58 +02:00
parent c0d9f6cad5
commit dbaf1a1374
7 changed files with 51 additions and 19 deletions

View File

@@ -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(())
}

View File

@@ -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<GuildMarker>,
state: Arc<StateRef>,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// 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<InteractionCreate>,
@@ -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(())
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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();