Updated Grid
This commit is contained in:
@@ -13,6 +13,10 @@ use std::{
|
||||
pub struct TrackInfo {
|
||||
pub title: String,
|
||||
pub url: String,
|
||||
/// Total duration in seconds, if known (from yt-dlp metadata).
|
||||
pub duration_secs: Option<f64>,
|
||||
/// Whether this track should loop indefinitely.
|
||||
pub loop_enabled: bool,
|
||||
}
|
||||
|
||||
/// Global map of guild_id → ordered queue of TrackInfo.
|
||||
@@ -21,9 +25,7 @@ static TRACK_QUEUES: OnceLock<Arc<DashMap<u64, VecDeque<TrackInfo>>>> = OnceLock
|
||||
|
||||
/// Call once from the `ready` event handler to initialise the store.
|
||||
pub fn init_track_queues() {
|
||||
TRACK_QUEUES
|
||||
.set(Arc::new(DashMap::new()))
|
||||
.ok();
|
||||
TRACK_QUEUES.set(Arc::new(DashMap::new())).ok();
|
||||
}
|
||||
|
||||
/// Returns a reference to the global TRACK_QUEUES map.
|
||||
@@ -61,9 +63,7 @@ pub fn clear_queue(guild_id: u64) {
|
||||
pub fn get_queue(guild_id: u64) -> Vec<TrackInfo> {
|
||||
queues()
|
||||
.get(&guild_id)
|
||||
.map(|q: dashmap::mapref::one::Ref<u64, VecDeque<TrackInfo>>| {
|
||||
q.iter().cloned().collect()
|
||||
})
|
||||
.map(|q: dashmap::mapref::one::Ref<u64, VecDeque<TrackInfo>>| q.iter().cloned().collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -86,3 +86,61 @@ pub async fn get_is_paused(guild_id: u64) -> bool {
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Toggle or set loop on the currently playing track
|
||||
pub async fn set_loop_current(guild_id: u64, enabled: bool) -> bool {
|
||||
// Update our metadata store first
|
||||
let updated = {
|
||||
if let Some(mut q) = queues().get_mut(&guild_id) {
|
||||
if let Some(front) = q.front_mut() {
|
||||
front.loop_enabled = enabled;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
if !updated {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tell songbird to loop / unloop the live track handle
|
||||
let manager = get_songbird();
|
||||
let serenity_guild_id = GuildId::from(guild_id);
|
||||
if let Some(handler_lock) = manager.get(serenity_guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
let current = handler.queue().current();
|
||||
drop(handler);
|
||||
if let Some(track) = current {
|
||||
if enabled {
|
||||
let _ = track.enable_loop();
|
||||
} else {
|
||||
let _ = track.disable_loop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns the current playback position (in seconds) for the active track in the
|
||||
/// given guild, or `0.0` if nothing is playing or the info is unavailable.
|
||||
pub async fn get_current_position(guild_id: u64) -> f64 {
|
||||
let manager = get_songbird();
|
||||
let serenity_guild_id = GuildId::from(guild_id);
|
||||
if let Some(handler_lock) = manager.get(serenity_guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
let current = handler.queue().current();
|
||||
drop(handler);
|
||||
if let Some(track) = current {
|
||||
return track
|
||||
.get_info()
|
||||
.await
|
||||
.map(|info| info.position.as_secs_f64())
|
||||
.unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
0.0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user