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

View File

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

View File

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

View File

@@ -78,7 +78,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
songbird,
standby: Standby::new(),
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;
#[derive(Clone)]
pub(crate) struct Metadata {
pub(crate) title: Option<String>,
pub(crate) duration: Option<Duration>,
pub(crate) url: String,
pub(crate) src: YoutubeDl,
}
pub(crate) struct MetadataMap;

View File

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