save media source in metadata typemap
All checks were successful
tests / fmt (push) Successful in 1m12s
tests / clippy (push) Successful in 1m12s
tests / pre-commit (push) Successful in 1m14s
tests / build (push) Successful in 1m30s
tests / test (push) Successful in 1m44s

This commit is contained in:
2024-06-17 17:06:59 +02:00
parent 3781785a50
commit 0bee437e53
6 changed files with 56 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
use crate::state::{State, StateRef}; use crate::metadata::MetadataMap;
use crate::state::{Settings, State, StateRef};
use async_trait::async_trait; use async_trait::async_trait;
use songbird::input::Compose;
use songbird::{Event, EventContext, EventHandler, TrackEvent}; use songbird::{Event, EventContext, EventHandler, TrackEvent};
use std::ops::Sub; use std::ops::Sub;
use std::time::Duration; use std::time::Duration;
@@ -29,12 +29,21 @@ pub(crate) async fn loop_queue(
return Ok(()); return Ok(());
}; };
let looping = if let Some(mut settings) = state.guild_settings.get_mut(&guild_id) { 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; settings.loop_queue = !settings.loop_queue;
settings.loop_queue println!("loop_queue: {}", settings.loop_queue);
} else { });
false
}; let looping = state
.guild_settings
.get(&guild_id)
.expect("Cannot get loop state")
.loop_queue;
if let Some(call_lock) = state.songbird.get(guild_id) { if let Some(call_lock) = state.songbird.get(guild_id) {
let mut call = call_lock.lock().await; let mut call = call_lock.lock().await;
@@ -47,10 +56,11 @@ pub(crate) async fn loop_queue(
); );
} }
let mut message = "I'm not looping anymore!".to_string(); let message = if looping {
if looping { "I'm now looping the current queue!".to_string()
message = "I'm now looping the current queue!".to_string(); } else {
} "I'm not looping anymore!".to_string()
};
let interaction_response_data = InteractionResponseDataBuilder::new() let interaction_response_data = InteractionResponseDataBuilder::new()
.content(message) .content(message)
@@ -91,29 +101,28 @@ impl EventHandler for TrackEndNotifier {
return None; return None;
}; };
let (_, track_handle) = track_list.first()?; let (_, track_handle) = track_list.first()?;
if let Some(yt) = self if let Some(call_lock) = self.state.songbird.get(self.guild_id) {
.state let mut call = call_lock.lock().await;
.tracks
.get(&self.guild_id) // get metadata from finished track
.unwrap() let old_typemap_lock = track_handle.typemap().read().await;
.get(&track_handle.uuid()) let old_metadata = old_typemap_lock.get::<MetadataMap>().unwrap();
{
let mut src = yt.clone(); // enqueue track
if let Ok(metadata) = src.aux_metadata().await { let handle = call.enqueue_with_preload(
if let Some(call_lock) = self.state.songbird.get(self.guild_id) { old_metadata.src.clone().into(),
let mut call = call_lock.lock().await; old_metadata.duration.map(|duration| -> Duration {
call.enqueue_with_preload( if duration.as_secs() > 5 {
src.into(), duration.sub(Duration::from_secs(5))
metadata.duration.map(|duration| -> Duration { } else {
if duration.as_secs() > 5 { duration
duration.sub(Duration::from_secs(5)) }
} else { }),
duration );
}
}), // insert metadata into new track
); let mut new_typemap = handle.typemap().write().await;
} new_typemap.insert::<MetadataMap>(old_metadata.clone());
}
} }
None None
} }

View File

@@ -138,13 +138,13 @@ pub(crate) async fn play(
tracing::debug!("track: {:?}", track); tracing::debug!("track: {:?}", track);
let url = track.url.clone().or(track.original_url.clone()).ok_or("")?; let url = track.url.clone().or(track.original_url.clone()).ok_or("")?;
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone()); let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
let s = src.clone(); let src_copy = src.clone();
let track: Track = src.clone().into(); let track: Track = src_copy.into();
state //state
.tracks // .tracks
.entry(guild_id) // .entry(guild_id)
.or_default() // .or_default()
.insert(track.uuid, s); // .insert(track.uuid, src);
if let Ok(metadata) = src.aux_metadata().await { if let Ok(metadata) = src.aux_metadata().await {
debug!("metadata: {:?}", metadata); debug!("metadata: {:?}", metadata);
@@ -167,6 +167,7 @@ pub(crate) async fn play(
title: metadata.title, title: metadata.title,
duration: metadata.duration, duration: metadata.duration,
url, url,
src,
}); });
} }
} }

View File

@@ -27,7 +27,7 @@ pub(crate) async fn skip(
} }
let interaction_response_data = InteractionResponseDataBuilder::new() let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Skipped the next track") .content("Skipped a track")
.build(); .build();
let response = InteractionResponse { let response = InteractionResponse {

View File

@@ -78,7 +78,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
songbird, songbird,
standby: Standby::new(), standby: Standby::new(),
guild_settings: Default::default(), guild_settings: Default::default(),
tracks: Default::default(),
}), }),
) )
}; };

View File

@@ -1,10 +1,12 @@
use songbird::typemap::TypeMapKey; use songbird::{input::YoutubeDl, typemap::TypeMapKey};
use std::time::Duration; use std::time::Duration;
#[derive(Clone)]
pub(crate) struct Metadata { pub(crate) struct Metadata {
pub(crate) title: Option<String>, pub(crate) title: Option<String>,
pub(crate) duration: Option<Duration>, pub(crate) duration: Option<Duration>,
pub(crate) url: String, pub(crate) url: String,
pub(crate) src: YoutubeDl,
} }
pub(crate) struct MetadataMap; pub(crate) struct MetadataMap;

View File

@@ -1,11 +1,10 @@
use dashmap::DashMap; use dashmap::DashMap;
use songbird::{input::YoutubeDl, Songbird}; use songbird::Songbird;
use std::sync::Arc; use std::sync::Arc;
use twilight_cache_inmemory::InMemoryCache; use twilight_cache_inmemory::InMemoryCache;
use twilight_http::Client as HttpClient; use twilight_http::Client as HttpClient;
use twilight_model::id::{marker::GuildMarker, Id}; use twilight_model::id::{marker::GuildMarker, Id};
use twilight_standby::Standby; use twilight_standby::Standby;
use uuid::Uuid;
pub(crate) type State = Arc<StateRef>; pub(crate) type State = Arc<StateRef>;
@@ -21,5 +20,4 @@ pub(crate) struct StateRef {
pub(crate) songbird: Songbird, pub(crate) songbird: Songbird,
pub(crate) standby: Standby, pub(crate) standby: Standby,
pub(crate) guild_settings: DashMap<Id<GuildMarker>, Settings>, pub(crate) guild_settings: DashMap<Id<GuildMarker>, Settings>,
pub(crate) tracks: DashMap<Id<GuildMarker>, DashMap<Uuid, YoutubeDl>>,
} }