diff --git a/src/track.rs b/src/track.rs index 807cf46..42e076f 100644 --- a/src/track.rs +++ b/src/track.rs @@ -1,9 +1,10 @@ +use serenity::prelude::TypeMapKey; use tracing::warn; use crate::vc::enter_vc; use crate::{CommandResult, Context}; -crate::commands!(pause, resume); +crate::commands!(pause, resume, r#loop); #[poise::command(slash_command, category = "Controls")] /// Pause the current track @@ -35,3 +36,50 @@ async fn resume(ctx: Context<'_>) -> CommandResult { }) .await } + +pub enum LoopState { + Disabled, + Enabled, +} + +impl TypeMapKey for LoopState { + type Value = LoopState; +} + +#[poise::command(slash_command, category = "Controls")] +/// Toggle loop mode for the current track +async fn r#loop(ctx: Context<'_>) -> CommandResult { + enter_vc(ctx, false, |handler, c| async move { + let lock = handler.lock().await; + let current = lock.queue().current(); + drop(lock); + let Some(current) = current else { + c.say("No track is currently playing").await?; + return Ok(()); + }; + + let map = current.typemap().write().await; + + let new_state = match map.get() { + Some(LoopState::Disabled) => LoopState::Enabled, + Some(LoopState::Enabled) => LoopState::Disabled, + None => LoopState::Enabled, + }; + + map.insert(new_state); + + drop(map); + + match new_state { + LoopState::Disabled => { + current.disable_loop()?; + c.say("Looping disabled").await?; + } + LoopState::Enabled => { + current.enable_loop()?; + c.say("Looping enabled").await?; + } + } + + }).await +}