Added data lock for audio configs (need to wire into volume command still)

This commit is contained in:
2023-07-25 22:44:36 -04:00
parent 8fae4022ab
commit 91d8a8f42c
5 changed files with 126 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;
use log::debug;
@@ -16,6 +17,18 @@ pub mod skip;
pub mod stop;
pub mod volume;
#[derive(Clone, Debug)]
pub struct AudioConfigs;
impl TypeMapKey for AudioConfigs {
type Value = Arc<RwLock<HashMap<GuildId, AudioConfig>>>;
}
#[derive(Clone, Debug)]
pub struct AudioConfig {
pub volume: f32
}
/// Joins a Discord voice channel.
///
/// # Arguments
@@ -135,7 +148,7 @@ pub async fn edit_response(ctx: &Context, command: &ApplicationCommandInteractio
///
/// # Returns
/// Result<Metadata, SongbirdError> - Ok if the song was added successfully, Err if there was an error.
pub async fn add_song(call: Arc<Mutex<Call>>, url: &str, lazy: bool) -> Result<Metadata, SongbirdError> {
pub async fn add_song(call: Arc<Mutex<Call>>, url: &str, lazy: bool, audio_config: Option<&AudioConfig>) -> Result<Metadata, SongbirdError> {
let source = if is_valid_url(url) {
Restartable::ytdl(url.to_owned(), lazy).await?
} else {
@@ -144,7 +157,10 @@ pub async fn add_song(call: Arc<Mutex<Call>>, url: &str, lazy: bool) -> Result<M
let mut handler = call.lock().await;
let track: Input = source.into();
let metadata = *track.metadata.clone();
handler.enqueue_source(track);
let track_handle = handler.enqueue_source(track);
if let Some(ac) = audio_config {
let _ = track_handle.set_volume(ac.volume);
}
Ok(metadata)
}