Skip to content

Commit

Permalink
feat(loop): Display amount of remaining loops for track
Browse files Browse the repository at this point in the history
  • Loading branch information
jontze committed Dec 1, 2023
1 parent aad656b commit e70e482
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 19 additions & 1 deletion cadency_commands/src/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cadency_core::{
CadencyCommand, CadencyError,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};
use songbird::tracks::LoopState;

#[derive(CommandBaseline, Default)]
#[description = "Shows current song"]
Expand All @@ -29,16 +30,33 @@ impl CadencyCommand for Now {
message: ":x: **No song is playing**".to_string(),
})?;

// Extract Loop State from Track
let loop_state = track.get_info().await.unwrap().loops;

// Create message from track metadata. This is scoped to drop the read lock on the
// trackmeta as soon as possible.
let message = {
let track_map = track.typemap().read().await;
let metadata = track_map
.get::<TrackMetaKey>()
.expect("Metadata to be present in track map");

metadata.title.as_ref().map_or(
String::from(":x: **Could not add audio source to the queue!**"),
|title| format!(":newspaper: `{title}`"),
|title| {
let mut track_info = format!(":newspaper: `{title}`");
match loop_state {
LoopState::Infinite => {
track_info.push_str("\n:repeat: `Infinite`");
}
LoopState::Finite(loop_amount) => {
if loop_amount > 0 {
track_info.push_str(&format!("\n:repeat: `{}`", loop_amount));
}
}
}
track_info
},
)
};

Expand Down
22 changes: 19 additions & 3 deletions cadency_commands/src/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serenity::{
client::Context,
model::{application::CommandInteraction, Color},
};
use songbird::tracks::LoopState;

#[derive(CommandBaseline, Default)]
#[description = "List all tracks in the queue"]
Expand Down Expand Up @@ -42,7 +43,8 @@ impl CadencyCommand for Tracks {
let track_position = index + 1;
// Extract title and url of the track. This is scoped to drop the read lock on
// the track meta as soon as possible.
let (title, url) = {
let (title, url, loop_state) = {
// Extract track Metadata from tracks TyeMap
let track_map = track.typemap().read().await;
let metadata = track_map
.get::<TrackMetaKey>()
Expand All @@ -55,11 +57,25 @@ impl CadencyCommand for Tracks {
.source_url
.as_ref()
.map_or("**No url provided**", |u| u);
(title.to_owned(), url.to_owned())

// Extract loop state from track state
let track_info = track.get_info().await.unwrap();
(title.to_owned(), url.to_owned(), track_info.loops)
};
let mut embed_value = format!(":notes: `{url}`");
match loop_state {
LoopState::Infinite => {
embed_value.push_str("\n:repeat: `Infinite`");
}
LoopState::Finite(loop_amount) => {
if loop_amount > 0 {
embed_value.push_str(&format!("\n:repeat: `{}`", loop_amount));
}
}
}
embeded_tracks = embeded_tracks.field(
format!("{track_position}. :newspaper: `{title}`"),
format!(":notes: `{url}`"),
embed_value,
false,
);
}
Expand Down

0 comments on commit e70e482

Please sign in to comment.