Added data lock for audio configs (need to wire into volume command still)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use songbird::EventHandler;
|
||||
|
||||
use crate::commands::audio::{join, leave, add_song, get_songbird};
|
||||
use crate::commands::audio::{join, leave, add_song, get_songbird, AudioConfigs};
|
||||
|
||||
use super::{create_response, edit_response};
|
||||
|
||||
@@ -65,7 +65,12 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty).await {
|
||||
let audio_config = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<AudioConfigs>().expect("Expected AudioConfigs in TypeMap.").clone()
|
||||
};
|
||||
let ac = audio_config.read().await;
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, ac.get(&guild_id)).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added song: {}", track_title);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
use log::{debug, error, warn};
|
||||
|
||||
use serenity::prelude::*;
|
||||
use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
|
||||
use super::{get_songbird, create_response, edit_response};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
// Get the volume
|
||||
let volume = match command.data.options.get(0) {
|
||||
Some(t) => match &t.value {
|
||||
Some(v) => match v.as_str() {
|
||||
Some(s) => s.to_owned(),
|
||||
None => {
|
||||
warn!("Missing volume option");
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing volume option");
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing volume option");
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Create the initial response
|
||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
return;
|
||||
}
|
||||
|
||||
let guild_id = match command.guild_id {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
if let Err(why) = edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
let manager = get_songbird(ctx).await;
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
if let Err(err) = handler.queue().skip() {
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to change volume: {}", err)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
} else {
|
||||
debug!("Setting the volume to {}", volume);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Setting volume to {}", volume)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("volume").description("Set the audio player volume").create_option(|option| { option
|
||||
.name("volume")
|
||||
.description("The new volume level")
|
||||
.kind(serenity::model::prelude::command::CommandOptionType::Number)
|
||||
.required(true)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user