Volume command works, fixed OAI create thread missing initial comment

This commit is contained in:
2023-07-26 12:33:46 -04:00
parent 91d8a8f42c
commit f4076ce9c3
4 changed files with 83 additions and 40 deletions

View File

@@ -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 {