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,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::<AudioConfigs>().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::<AudioConfigs>(Arc::new(RwLock::new(HashMap::default())));
}
if let Err(why) = client.start_autosharded().await {
error!("An error occurred while running the client: {:?}", why);
}