Skip to content

Commit

Permalink
add loop command
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed May 8, 2023
1 parent e0804df commit 87fb92d
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/track.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 => {

Check failure on line 74 in src/track.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 74 in src/track.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types
current.disable_loop()?;
c.say("Looping disabled").await?;
}

Check failure on line 77 in src/track.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> src/track.rs:74:36 | 74 | LoopState::Disabled => { | ____________________________________^ 75 | | current.disable_loop()?; 76 | | c.say("Looping disabled").await?; 77 | | } | |_____________^ expected `Result<(), _>`, found `()` | = note: expected enum `std::result::Result<(), _>` found unit type `()`
LoopState::Enabled => {

Check failure on line 78 in src/track.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 78 in src/track.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types
current.enable_loop()?;
c.say("Looping enabled").await?;
}

Check failure on line 81 in src/track.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> src/track.rs:78:35 | 78 | LoopState::Enabled => { | ___________________________________^ 79 | | current.enable_loop()?; 80 | | c.say("Looping enabled").await?; 81 | | } | |_____________^ expected `Result<(), _>`, found `()` | = note: expected enum `std::result::Result<(), _>` found unit type `()`
}

}).await
}

0 comments on commit 87fb92d

Please sign in to comment.