diff --git a/docker-compose.yml b/docker-compose.yml index 782558d..40d94b0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,8 @@ services: - VERSION=${SIREN_VERSION:-latest} volumes: - ./app:/siren + env_file: + - .env environment: DISCORD_TOKEN: ${DISCORD_TOKEN} RUST_LOG: ${RUST_LOG} @@ -25,6 +27,8 @@ services: db: image: postgres:latest container_name: siren_db + env_file: + - .env environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} diff --git a/src/commands/audio/mod.rs b/src/commands/audio/mod.rs index 6d78d1d..cd60d28 100644 --- a/src/commands/audio/mod.rs +++ b/src/commands/audio/mod.rs @@ -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>>; +} + +#[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 - Ok if the song was added successfully, Err if there was an error. -pub async fn add_song(call: Arc>, url: &str, lazy: bool) -> Result { +pub async fn add_song(call: Arc>, url: &str, lazy: bool, audio_config: Option<&AudioConfig>) -> Result { 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>, url: &str, lazy: bool) -> Result().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); diff --git a/src/commands/audio/volume.rs b/src/commands/audio/volume.rs index e69de29..202894b 100644 --- a/src/commands/audio/volume.rs +++ b/src/commands/audio/volume.rs @@ -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) + }) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9e681a8..71e3616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -use std::collections::HashSet; +use std::collections::{HashSet, HashMap}; use std::env; +use std::sync::Arc; -use commands::audio::create_response; +use commands::audio::{create_response, AudioConfig, AudioConfigs}; use diesel::r2d2::{Pool, ConnectionManager}; use diesel::pg::PgConnection; @@ -44,7 +45,6 @@ impl EventHandler for Handler { } None => {} } - } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { @@ -55,6 +55,7 @@ impl EventHandler for Handler { "pause" => commands::audio::pause::run(&ctx, &command).await, "resume" => commands::audio::resume::run(&ctx, &command).await, "skip" => commands::audio::skip::run(&ctx, &command).await, + "volume" => commands::audio::volume::run(&ctx, &command).await, _ => { let content: String = match command.data.name.as_str() { "ping" => commands::ping::run(&command.data.options), @@ -74,6 +75,14 @@ impl EventHandler for Handler { warn!("No ready guilds found"); } for guild in ready.guilds { + let audio_config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected AudioConfigs in TypeMap.").clone() + }; + { + let mut audio_configs = audio_config_lock.write().await; + let _ = audio_configs.insert(guild.id, AudioConfig { volume: 1.0 }); + } let commands = guild.id.set_application_commands(&ctx.http, |commands| { commands.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::ping::register(command) }) .create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::play::register(command) }) @@ -81,6 +90,7 @@ impl EventHandler for Handler { .create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::pause::register(command) }) .create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::resume::register(command) }) .create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::skip::register(command) }) + .create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::volume::register(command) }) }).await; match commands { Ok(c) => info!("Registered {} commands for guild {}", c.len(), guild.id.0), @@ -122,7 +132,7 @@ async fn main() { Ok(token) => { info!("Loaded OpenAI token"); Handler { - oai: Some(commands::oai::OAI { client: reqwest::Client::new(), base_url: "https://api.openai.com/v1".to_string(), max_attempts: 5, token , max_context_questions: 10 }), + oai: Some(commands::oai::OAI { client: reqwest::Client::new(), base_url: "https://api.openai.com/v1".to_string(), max_attempts: 5, token , max_context_questions: 15 }), pool } } @@ -140,6 +150,11 @@ async fn main() { .await .expect("Error creating client"); + { + let mut data = client.data.write().await; + data.insert::(Arc::new(RwLock::new(HashMap::default()))); + } + if let Err(why) = client.start_autosharded().await { error!("An error occurred while running the client: {:?}", why); }