From b09b09453a8f718cfb8b99a5aebb5e5b0cbaeffb Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Fri, 7 Jul 2023 11:39:33 -0400 Subject: [PATCH 01/10] Fixed dockerfile, put in temporary fix to create table in docker --- Dockerfile | 15 ++++++------ docker-compose.yml | 6 ++++- src/commands/oai.rs | 2 +- src/database/mod.rs | 59 +++++++++++++++++++++++++++++---------------- 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3f9b54d..62edc3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,22 @@ FROM rust:1.67 as builder WORKDIR /siren -RUN apt-get update && apt-get install -y cmake && apt-get auto-remove -y ADD src ./src/ ADD Cargo.toml ./ -RUN cargo build --release --bin siren +RUN apt-get update && apt-get install -y cmake && \ + cargo build --release --bin siren FROM debian:bullseye-slim as packages WORKDIR /packages -RUN apt-get update && apt-get install -y libopus-dev libpq5 libpq-dev curl tar xz-utils -RUN curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux > yt-dlp && \ - chmod +x yt-dlp -RUN curl -L https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz > ffmpeg.tar.xz && \ +RUN apt-get update && apt-get install -y curl tar xz-utils && \ + curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux > yt-dlp && \ + chmod +x yt-dlp && \ + curl -L https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz > ffmpeg.tar.xz && \ tar -xJf ffmpeg.tar.xz --wildcards */bin/ffmpeg --transform='s/^.*\///' && rm ffmpeg.tar.xz FROM debian:bullseye-slim as runtime WORKDIR /siren +RUN apt-get update && apt-get install -y libopus-dev libpq5 libpq-dev && apt-get auto-remove -y COPY --from=builder /siren/target/release/siren /usr/local/bin/siren COPY --from=packages /packages /usr/bin -# ADD migrations ./migrations/ +# ADD migrations ./ CMD ["siren"] diff --git a/docker-compose.yml b/docker-compose.yml index fdfc898..7d86f7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,11 @@ services: environment: DISCORD_TOKEN: ${DISCORD_TOKEN} RUST_LOG: ${RUST_LOG} - DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB} + OPENAI_API_KEY: ${OPENAI_API_KEY} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_HOST: db depends_on: - db restart: unless-stopped diff --git a/src/commands/oai.rs b/src/commands/oai.rs index ba976f5..d6ff8fd 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -205,7 +205,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P .and(crate::database::schema::messages::channel_id.eq(channel_id.0 as i64)) .and(crate::database::schema::messages::user_id.eq(author_id.0 as i64)) ) - .order(crate::database::schema::messages::created.desc()) + .order(crate::database::schema::messages::created.asc()) .limit(oai.max_context_questions) .load(&mut connection); diff --git a/src/database/mod.rs b/src/database/mod.rs index 33e9753..1673f85 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,5 +1,5 @@ use std::env; -use std::path::Path; +// use std::path::Path; use diesel::RunQueryDsl; use diesel::r2d2::{Pool, ConnectionManager}; @@ -11,34 +11,51 @@ pub mod schema; pub fn run_migrations(pool: &Pool>) { let mut connection = pool.get().unwrap(); - let migrations_dir = Path::new("./migrations"); - let migrations = std::fs::read_dir(&migrations_dir).unwrap(); - - for migration in migrations { - if migration.as_ref().unwrap().file_type().unwrap().is_dir() { - let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap(); - - for migration_path in migration_paths { - if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") { - let path = &migration_path.unwrap().path(); - let contents = std::fs::read_to_string(path).expect("Unable to read from file"); - if let Err(err) = diesel::sql_query(&contents).execute(&mut connection) { - error!("Could not run migration: {}", err); - } else { - info!("Successfully ran migration: {}", path.display()); - } - } - } - } + if let Err(err) = diesel::sql_query("CREATE TABLE IF NOT EXISTS messages ( + id TEXT PRIMARY KEY NOT NULL, + guild_id BIGINT NOT NULL, + channel_id BIGINT NOT NULL, + user_id BIGINT NOT NULL, + created BIGINT NOT NULL, + model TEXT NOT NULL, + request TEXT NOT NULL, + response TEXT NOT NULL, + request_tags TEXT[] NOT NULL, + response_tags TEXT[] NOT NULL + )").execute(&mut connection) { + error!("Could not create messages table: {}", err); + } else { + info!("Successfully created messages table"); } + // let migrations_dir = Path::new("./migrations"); + // let migrations = std::fs::read_dir(&migrations_dir).unwrap(); + + // for migration in migrations { + // if migration.as_ref().unwrap().file_type().unwrap().is_dir() { + // let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap(); + + // for migration_path in migration_paths { + // if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") { + // let path = &migration_path.unwrap().path(); + // let contents = std::fs::read_to_string(path).expect("Unable to read from file"); + // if let Err(err) = diesel::sql_query(&contents).execute(&mut connection) { + // error!("Could not run migration: {}", err); + // } else { + // info!("Successfully ran migration: {}", path.display()); + // } + // } + // } + // } + // } } pub fn establish_connection() -> Pool> { let database_user = env::var("POSTGRES_USER").expect("Expected a user in the environment"); let database_password = env::var("POSTGRES_PASSWORD").expect("Expected a password in the environment"); let database_name = env::var("POSTGRES_DB").expect("Expected a database name in the environment"); + let database_host = env::var("POSTGRES_HOST").unwrap_or("localhost".to_string()); - let database_url = format!("postgres://{}:{}@localhost/{}", database_user, database_password, database_name); + let database_url = format!("postgres://{}:{}@{}/{}", database_user, database_password, database_host, database_name); let manager = ConnectionManager::::new(database_url); Pool::builder().build(manager).expect("Failed to create pool.") } \ No newline at end of file From 7d94f0732944d9804c79591dd8651bc89b25e685 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Fri, 7 Jul 2023 12:24:04 -0400 Subject: [PATCH 02/10] Siren OAI creates thread if possible for conversations --- docker-compose.yml | 4 ++-- src/commands/oai.rs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7d86f7b..782558d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,13 +2,13 @@ version: '3' services: siren: - image: siren:${SIREN_VERSION} + image: siren:${SIREN_VERSION:-latest} container_name: siren build: context: . dockerfile: ./Dockerfile args: - - VERSION=${SIREN_VERSION} + - VERSION=${SIREN_VERSION:-latest} volumes: - ./app:/siren environment: diff --git a/src/commands/oai.rs b/src/commands/oai.rs index d6ff8fd..83b1161 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -9,6 +9,7 @@ use log::{error, debug, trace, warn}; use serde::{Serialize, Deserialize}; use serde_json::Value; use serenity::model::channel::Message; +use serenity::model::prelude::ChannelType; use serenity::prelude::*; use crate::database::models::{NewMessageDB, MessageDB}; @@ -288,7 +289,18 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P // Stop the typing indicator and send the response typing.stop(); - if let Err(why) = msg.channel_id.say(&ctx.http, response).await { - error!("Cannot send message: {}", why); - } + match msg.channel_id.create_public_thread(&ctx.http, msg.id, |thread| { + thread.name("Siren Response").kind(ChannelType::PublicThread) + }).await { + Ok(c) => { + if let Err(why) = c.say(&ctx.http, response).await { + error!("Cannot send message: {}", why); + } + } + Err(_) => { + if let Err(why) = channel_id.say(&ctx.http, response).await { + error!("Cannot send message: {}", why); + } + } + }; } From c36c03ded5502e7e9909ea4e7429cc87f55bebf9 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Fri, 7 Jul 2023 15:39:12 -0400 Subject: [PATCH 03/10] Working on thread titles --- Cargo.toml | 6 +++++- Dockerfile | 11 +++++++++++ src/commands/oai.rs | 24 +++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd3ba7e..1cdd0af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,4 +38,8 @@ features = ["json", "rustls-tls"] [dependencies.diesel] version = "2.1.0" default-features = false -features = ["postgres", "32-column-tables", "serde_json", "r2d2", "with-deprecated"] \ No newline at end of file +features = ["postgres", "32-column-tables", "serde_json", "r2d2", "with-deprecated"] + +[dependencies.rust-bert] +version = "0.21.0" +features = ["download-libtorch"] \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 62edc3f..c39cdb8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,10 +13,21 @@ RUN apt-get update && apt-get install -y curl tar xz-utils && \ curl -L https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz > ffmpeg.tar.xz && \ tar -xJf ffmpeg.tar.xz --wildcards */bin/ffmpeg --transform='s/^.*\///' && rm ffmpeg.tar.xz +# FROM debian:bullseye-slim as libraries +# WORKDIR /libraries +# RUN apt-get update && apt-get install -y unzip && \ +# curl -L https://download.pytorch.org/libtorch/cu117/libtorch-cxx11-abi-shared-with-deps-2.0.1%2Bcu117.zip > libtorch.zip && \ +# unzip libtorch.zip && rm libtorch.zip + FROM debian:bullseye-slim as runtime WORKDIR /siren RUN apt-get update && apt-get install -y libopus-dev libpq5 libpq-dev && apt-get auto-remove -y COPY --from=builder /siren/target/release/siren /usr/local/bin/siren COPY --from=packages /packages /usr/bin +# COPY --from=libraries /libraries /usr/lib + +# ARG LIBTORCH=/usr/lib/libtorch +# ARG LD_LIBRARY_PATH=${LIBTORCH}/lib:${LD_LIBRARY_PATH} + # ADD migrations ./ CMD ["siren"] diff --git a/src/commands/oai.rs b/src/commands/oai.rs index 83b1161..9341476 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -290,7 +290,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P // Stop the typing indicator and send the response typing.stop(); match msg.channel_id.create_public_thread(&ctx.http, msg.id, |thread| { - thread.name("Siren Response").kind(ChannelType::PublicThread) + thread.name(truncate(&parsed_content, 99)).kind(ChannelType::PublicThread) }).await { Ok(c) => { if let Err(why) = c.say(&ctx.http, response).await { @@ -304,3 +304,25 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P } }; } + +fn truncate(s: &str, max_chars: usize) -> &str { + match s.char_indices().nth(max_chars) { + None => s, + Some((idx, _)) => &s[..idx], + } +} + +// fn summarization_model() -> Result { +// let config_resource = RemoteResource::from_pretrained(T5ConfigResources::T5_SMALL); +// let vocab_resource = RemoteResource::from_pretrained(T5VocabResources::T5_SMALL); +// let weights_resource = RemoteResource::from_pretrained(T5ModelResources::T5_SMALL); + +// let summarization_config = SummarizationConfig::new( +// ModelType::T5, +// ModelResource::Torch(Box::new(weights_resource)), +// config_resource, +// vocab_resource, +// None +// ); +// return SummarizationModel::new(summarization_config); +// } From 086a059fdbf60bb762588d6ba751b4a96c463f2c Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Fri, 7 Jul 2023 16:00:19 -0400 Subject: [PATCH 04/10] Upgraded dockerfile rust to 1.70.0 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c39cdb8..0af2576 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.67 as builder +FROM rust:1.70.0 as builder WORKDIR /siren ADD src ./src/ ADD Cargo.toml ./ From 8fae4022ab1d57aaba0216bab1e769d517a38c93 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Fri, 7 Jul 2023 18:23:30 -0400 Subject: [PATCH 05/10] Removed rust-bert --- Cargo.toml | 6 +----- src/commands/oai.rs | 15 --------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cdd0af..bd3ba7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,8 +38,4 @@ features = ["json", "rustls-tls"] [dependencies.diesel] version = "2.1.0" default-features = false -features = ["postgres", "32-column-tables", "serde_json", "r2d2", "with-deprecated"] - -[dependencies.rust-bert] -version = "0.21.0" -features = ["download-libtorch"] \ No newline at end of file +features = ["postgres", "32-column-tables", "serde_json", "r2d2", "with-deprecated"] \ No newline at end of file diff --git a/src/commands/oai.rs b/src/commands/oai.rs index 9341476..c93a357 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -311,18 +311,3 @@ fn truncate(s: &str, max_chars: usize) -> &str { Some((idx, _)) => &s[..idx], } } - -// fn summarization_model() -> Result { -// let config_resource = RemoteResource::from_pretrained(T5ConfigResources::T5_SMALL); -// let vocab_resource = RemoteResource::from_pretrained(T5VocabResources::T5_SMALL); -// let weights_resource = RemoteResource::from_pretrained(T5ModelResources::T5_SMALL); - -// let summarization_config = SummarizationConfig::new( -// ModelType::T5, -// ModelResource::Torch(Box::new(weights_resource)), -// config_resource, -// vocab_resource, -// None -// ); -// return SummarizationModel::new(summarization_config); -// } From 91d8a8f42c17f95563fa9ba626fac126335be748 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Tue, 25 Jul 2023 22:44:36 -0400 Subject: [PATCH 06/10] Added data lock for audio configs (need to wire into volume command still) --- docker-compose.yml | 4 ++ src/commands/audio/mod.rs | 20 ++++++++- src/commands/audio/play.rs | 9 ++++- src/commands/audio/volume.rs | 78 ++++++++++++++++++++++++++++++++++++ src/main.rs | 23 +++++++++-- 5 files changed, 126 insertions(+), 8 deletions(-) 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); } From f4076ce9c3c29343cc6fc67300c3bb22c0d6968b Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 26 Jul 2023 12:33:46 -0400 Subject: [PATCH 07/10] Volume command works, fixed OAI create thread missing initial comment --- src/commands/audio/play.rs | 4 +-- src/commands/audio/volume.rs | 41 ++++++++++++---------- src/commands/oai.rs | 66 +++++++++++++++++++++++++----------- src/main.rs | 12 ++++++- 4 files changed, 83 insertions(+), 40 deletions(-) diff --git a/src/commands/audio/play.rs b/src/commands/audio/play.rs index b0a4fe1..386e6a2 100644 --- a/src/commands/audio/play.rs +++ b/src/commands/audio/play.rs @@ -73,8 +73,8 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) { 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); - if let Err(why) = edit_response(&ctx, &command, format!("Added song to queue: {}", track_title)).await { + debug!("Added track: {}", track_title); + if let Err(why) = edit_response(&ctx, &command, format!("Added track to queue: {}", track_title)).await { error!("Failed to edit response message: {}", why); } let mut handler = handler_lock.lock().await; diff --git a/src/commands/audio/volume.rs b/src/commands/audio/volume.rs index 202894b..4ef8501 100644 --- a/src/commands/audio/volume.rs +++ b/src/commands/audio/volume.rs @@ -1,19 +1,19 @@ -use log::{debug, error, warn}; +use log::{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}; +use super::{get_songbird, create_response, edit_response, AudioConfigs, AudioConfig}; 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(), + Some(v) => match v.as_i64() { + Some(p) => std::cmp::min(100, std::cmp::max(0, p)), None => { - warn!("Missing volume option"); + warn!("Unable to get volume option as a string"); if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await { error!("Failed to create response message: {}", why); } @@ -21,7 +21,7 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) { } } None => { - warn!("Missing volume option"); + warn!("Missing volume option value"); if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await { error!("Failed to create response message: {}", why); } @@ -37,6 +37,9 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) { } }; + // Format volume to f32 bound between 0.0 and 1.0 + let bound_volume = volume as f32 / 100.0; + // Create the initial response if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await { error!("Failed to create response message: {}", why); @@ -52,27 +55,31 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) { return; } }; + 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; + *audio_configs.entry(guild_id).or_insert(AudioConfig { volume: 1.0 }) = AudioConfig { volume: bound_volume }; + } 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); - } + for (_, track_handle) in handler.queue().current_queue().iter().enumerate() { + let _ = track_handle.set_volume(bound_volume); } } + if let Err(why) = edit_response(&ctx, &command, format!("Setting the volume to {}", volume)).await { + error!("Failed to set the volume: {}", 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) + .description("Volume between 0 and 100") + .kind(serenity::model::prelude::command::CommandOptionType::Integer) .required(true) }) } \ No newline at end of file diff --git a/src/commands/oai.rs b/src/commands/oai.rs index c93a357..fbc55b9 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -8,8 +8,9 @@ use log::{error, debug, trace, warn}; use serde::{Serialize, Deserialize}; use serde_json::Value; +use serenity::model::Permissions; use serenity::model::channel::Message; -use serenity::model::prelude::ChannelType; +use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType}; use serenity::prelude::*; use crate::database::models::{NewMessageDB, MessageDB}; @@ -214,7 +215,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P Ok(r) => { let mut previous_message = "".to_string(); for message in r { - previous_message = format!("{}\nYou: {}\n Siren: {}", previous_message, message.request, message.response); + previous_message = format!("{}You: {}\n Siren: {}\n", previous_message, message.request, message.response); } Some(ChatCompletionMessage { role: GPTRole::User, content: previous_message }) } @@ -232,7 +233,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P ]; if let Some(mut previous) = previous_messages { - previous.content = format!("{}\nYou: {}\nSiren: ", previous.content, parsed_content); + previous.content = format!("{}You: {}\nSiren: ", previous.content, parsed_content); messages.push(previous); } else { messages.push(ChatCompletionMessage { @@ -254,16 +255,38 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P frequency_penalty: Some(0.0), user: Some(msg.author.name.clone()) }; + + // Get the thread channel ID + let response_channel = match msg.channel_id.create_private_thread(&ctx.http, |thread| { + thread.name(truncate(&parsed_content, 99)).kind(ChannelType::PublicThread) + }).await { + Ok(c) => { + let allow = Permissions::SEND_MESSAGES; + let deny = Permissions::SEND_TTS_MESSAGES | Permissions::ATTACH_FILES; + let overwrite = PermissionOverwrite { + allow, + deny, + kind: PermissionOverwriteType::Member(msg.author.id), + }; + let _ = c.create_permission(&ctx.http, &overwrite).await; + c.id + } + Err(_) => { + channel_id + } + }; + + // Get the OAI response and store message/response into the database let response = match oai.get_request(request).await { Ok(r) => { debug!("Processing response received from OpenAI"); if !r.choices.is_empty() { - // Insert the message into the messages database table let res = r.choices[0].message.content.clone(); + // Insert the message into the messages database table if let Err(err) = insert_into(crate::database::schema::messages::table).values(NewMessageDB { id: &r.id, guild_id: guild_id.0 as i64, - channel_id: channel_id.0 as i64, + channel_id: response_channel.0 as i64, user_id: author_id.0 as i64, created: r.created, model: &model, @@ -287,22 +310,25 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P }; debug!("Writing response: \"{}\"", response); - // Stop the typing indicator and send the response typing.stop(); - match msg.channel_id.create_public_thread(&ctx.http, msg.id, |thread| { - thread.name(truncate(&parsed_content, 99)).kind(ChannelType::PublicThread) - }).await { - Ok(c) => { - if let Err(why) = c.say(&ctx.http, response).await { - error!("Cannot send message: {}", why); - } - } - Err(_) => { - if let Err(why) = channel_id.say(&ctx.http, response).await { - error!("Cannot send message: {}", why); - } - } - }; + if let Err(why) = response_channel.say(&ctx.http, response).await { + error!("Cannot send message: {}", why); + } + + // match msg.channel_id.create_public_thread(&ctx.http, msg.id, |thread| { + // thread.name(truncate(&parsed_content, 99)).kind(ChannelType::PublicThread) + // }).await { + // Ok(c) => { + // if let Err(why) = c.say(&ctx.http, response).await { + // error!("Cannot send message: {}", why); + // } + // } + // Err(_) => { + // if let Err(why) = channel_id.say(&ctx.http, response).await { + // error!("Cannot send message: {}", why); + // } + // } + // }; } fn truncate(s: &str, max_chars: usize) -> &str { diff --git a/src/main.rs b/src/main.rs index 71e3616..4d1c851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,17 @@ impl EventHandler for Handler { Some(oai) => { match msg.mentions_me(&ctx.http).await { Ok(mentioned) => { - if mentioned { + let bot_in_thread = match msg.channel_id.get_thread_members(&ctx.http).await { + Ok(t) => { + match t.iter().find(|t| t.user_id.unwrap().0 == ctx.cache.current_user_id().0) { + Some(_) => true, + None => false + } + } + Err(_) => false + }; + // let has_bot = msg.channel_id.get_thread_members(&ctx.http).await.unwrap().contains(ctx.cache.current_user_id().0); + if mentioned || bot_in_thread { commands::oai::generate_response(&ctx, &msg, oai, &self.pool).await; } } From da2dc6b3baddc47a18602b1a8c2cef7696db8628 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 26 Jul 2023 19:35:13 +0000 Subject: [PATCH 08/10] Updated Makefile with help command --- Makefile | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5207d97..1138d49 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ #!make SHELL := /bin/bash + include .env include .version export $(shell sed 's/=.*//' .env) @@ -9,20 +10,25 @@ SIREN_IMAGES = $(shell docker images 'siren' -a -q) .PHONY: help build test up down exec clean -build: +help: ## Help command + @echo + @cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + @echo + +build: ## Build the docker image docker build -t siren:${SIREN_VERSION} . -test: +test: ## Run the docker app as a container docker run --env-file .env -it --rm --name siren siren:${SIREN_VERSION} -up: +up: ## Start the app docker compose up -d -down: +down: ## Stop the app docker compose down -exec: +exec: ## Enter running docker container docker exec -it siren bash -clean: +clean: ## Cleanup docker images docker rmi $(SIREN_IMAGES) From 5bec27677e232ef568dab0a8ef04e8b56360bf2c Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 26 Jul 2023 16:05:03 -0400 Subject: [PATCH 09/10] Moved typing to proper channel --- src/commands/oai.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/oai.rs b/src/commands/oai.rs index fbc55b9..1576be2 100644 --- a/src/commands/oai.rs +++ b/src/commands/oai.rs @@ -189,7 +189,6 @@ impl OAI { pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &Pool>) { debug!("Generating response for message: {}", msg.content); - let typing = msg.channel_id.start_typing(&ctx.http).unwrap(); let guild_id = msg.guild_id.unwrap(); let channel_id = msg.channel_id; @@ -276,6 +275,8 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &P } }; + let typing = response_channel.start_typing(&ctx.http).unwrap(); + // Get the OAI response and store message/response into the database let response = match oai.get_request(request).await { Ok(r) => { From dc44c47df1c8d90dcf424863c90c861987141c40 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 26 Jul 2023 18:36:35 -0400 Subject: [PATCH 10/10] Updated version to 0.2.3 --- .version | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.version b/.version index cdca5d4..4f1b91d 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -SIREN_VERSION=0.2.2 \ No newline at end of file +SIREN_VERSION=0.2.3 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index bd3ba7e..be626fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "siren" -version = "0.2.2" +version = "0.2.3" edition = "2021" authors = ["Ben Sherriff "] repository = "https://github.com/bensherriff/siren"