Add other db tables

This commit is contained in:
Benjamin Sherriff
2023-10-03 12:11:48 -04:00
parent 6c8a7ceefc
commit 95ede3291e
38 changed files with 247 additions and 26 deletions

View File

@@ -8,6 +8,10 @@ readme = "README.md"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
[dependencies] [dependencies]
actix-web = "4.4.0"
actix-rt = "2.9.0"
actix-web-httpauth = "0.8.1"
chrono = { version = "0.4.31", features = ["serde"] }
dotenv = "0.15.0" dotenv = "0.15.0"
serde_json = "1.0.107" serde_json = "1.0.107"
log = "0.4.20" log = "0.4.20"

View File

@@ -0,0 +1 @@
DROP TABLE races;

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS races (
id INTEGER GENERATED ALWAYS AS IDENTITY,
name TEXT NOT NULL,
size TEXT NOT NULL,
source TEXT NOT NULL,
data JSON NOT NULL
);

View File

@@ -0,0 +1 @@
DROP TABLE classes;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS classes (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE feats;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS feats (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE options_features;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS options_features (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE backgrounds;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS backgrounds (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE items;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS items (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE spells;

View File

@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS spells (
id INTEGER GENERATED ALWAYS AS IDENTITY,
name TEXT NOT NULL,
school TEXT NOT NULL,
level TEXT NOT NULL,
ritual BOOLEAN DEFAULT FALSE,
casting_time TEXT NOT NULL,
range TEXT NOT NULL,
components_verbal BOOLEAN DEFAULT FALSE,
components_somatic BOOLEAN DEFAULT FALSE,
components_material BOOLEAN DEFAULT FALSE,
components_materials_needed TEXT
duration TEXT NOT NULL,
classes TEXT[] NOT NULL,
sources TEXT[] NOT NULL,
tags TEXT[]
description TEXT NOT NULL
);

View File

@@ -0,0 +1 @@
DROP TABLE conditions;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS conditions (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -0,0 +1 @@
DROP TABLE bestiary;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS bestiary (
id INTEGER GENERATED ALWAYS AS IDENTITY
);

View File

@@ -12,8 +12,7 @@ use serenity::model::channel::Message;
use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType}; use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType};
use serenity::prelude::*; use serenity::prelude::*;
use crate::db; use crate::db::{connection, NewMessageDB, MessageDB};
use crate::messages::{NewMessageDB, MessageDB};
pub struct OAI { pub struct OAI {
pub client: reqwest::Client, pub client: reqwest::Client,
@@ -189,7 +188,7 @@ impl OAI {
pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) { pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
debug!("Generating response for message: {}", msg.content); debug!("Generating response for message: {}", msg.content);
let mut connection = db::connection().unwrap(); let mut connection = connection().unwrap();
let guild_id = msg.guild_id.unwrap(); let guild_id = msg.guild_id.unwrap();
let channel_id = msg.channel_id; let channel_id = msg.channel_id;
@@ -200,13 +199,13 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
let parsed_content = msg.content.replace(bot_mention.as_str(), ""); let parsed_content = msg.content.replace(bot_mention.as_str(), "");
// Setup the request messages // Setup the request messages
let result: Result<Vec<MessageDB>, diesel::result::Error> = crate::schema::messages::table let result: Result<Vec<MessageDB>, diesel::result::Error> = crate::db::schema::messages::table
.select(MessageDB::as_select()) .select(MessageDB::as_select())
.filter((crate::schema::messages::guild_id.eq(guild_id.0 as i64)) .filter((crate::db::schema::messages::guild_id.eq(guild_id.0 as i64))
.and(crate::schema::messages::channel_id.eq(channel_id.0 as i64)) .and(crate::db::schema::messages::channel_id.eq(channel_id.0 as i64))
.and(crate::schema::messages::user_id.eq(author_id.0 as i64)) .and(crate::db::schema::messages::user_id.eq(author_id.0 as i64))
) )
.order(crate::schema::messages::created.asc()) .order(crate::db::schema::messages::created.asc())
.limit(oai.max_context_questions) .limit(oai.max_context_questions)
.load(&mut connection); .load(&mut connection);
@@ -284,7 +283,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
if !r.choices.is_empty() { if !r.choices.is_empty() {
let res = r.choices[0].message.content.clone(); let res = r.choices[0].message.content.clone();
// Insert the message into the messages database table // Insert the message into the messages database table
if let Err(err) = insert_into(crate::schema::messages::table).values(NewMessageDB { if let Err(err) = insert_into(crate::db::schema::messages::table).values(NewMessageDB {
id: &r.id, id: &r.id,
guild_id: guild_id.0 as i64, guild_id: guild_id.0 as i64,
channel_id: response_channel.0 as i64, channel_id: response_channel.0 as i64,

View File

0
src/db/bestiary/mod.rs Normal file
View File

0
src/db/classes/mod.rs Normal file
View File

0
src/db/conditions/mod.rs Normal file
View File

0
src/db/feats/mod.rs Normal file
View File

0
src/db/items/mod.rs Normal file
View File

View File

@@ -1,6 +1,6 @@
use diesel::prelude::*; use diesel::prelude::*;
use crate::schema::messages; use crate::db::schema::messages;
#[derive(Queryable, Selectable)] #[derive(Queryable, Selectable)]
#[diesel(table_name = messages)] #[diesel(table_name = messages)]

View File

@@ -6,6 +6,21 @@ use log::{error, info};
use r2d2; use r2d2;
use std::env; use std::env;
mod backgrounds;
mod bestiary;
mod classes;
mod conditions;
mod feats;
mod items;
mod messages;
mod options;
mod races;
mod spells;
mod users;
pub mod schema;
pub use messages::*;
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>; pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;

0
src/db/options/mod.rs Normal file
View File

0
src/db/races/mod.rs Normal file
View File

35
src/db/schema.rs Normal file
View File

@@ -0,0 +1,35 @@
diesel::table! {
messages (id) {
id -> Text,
guild_id -> BigInt,
channel_id -> BigInt,
user_id -> BigInt,
created -> BigInt,
model -> Text,
request -> Text,
response -> Text,
request_tags -> Array<Text>,
response_tags -> Array<Text>,
}
}
diesel::table! {
spells (id) {
id -> Integer,
name -> Text,
school -> Text,
level -> Integer,
ritual -> Bool,
casting_time -> Text,
range -> Text,
components_verbal -> Bool,
components_somatic -> Bool,
components_material -> Bool,
components_materials_needed -> Nullable<Text>,
duration -> Text,
classes -> Array<Text>,
sources -> Array<Text>,
tags -> Array<Text>,
description -> Text
}
}

3
src/db/spells/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod model;
pub use model::*;

120
src/db/spells/model.rs Normal file
View File

@@ -0,0 +1,120 @@
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use crate::db::schema::spells;
#[derive(Queryable, QueryableByName)]
#[diesel(table_name = spells)]
pub struct QuerySpell {
pub id: i32,
pub name: String,
pub school: String,
pub level: i32,
pub ritual: bool,
pub casting_time: String,
pub range: String,
pub components_verbal: bool,
pub components_somatic: bool,
pub components_material: bool,
pub components_materials_needed: Option<String>,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub tags: Vec<String>,
pub description: String
}
#[derive(Insertable, AsChangeset)]
#[diesel(table_name = spells)]
pub struct InsertSpell {
pub name: String,
pub school: String,
pub level: i32,
pub ritual: bool,
pub casting_time: String,
pub range: String,
pub components_verbal: bool,
pub components_somatic: bool,
pub components_material: bool,
pub components_materials_needed: Option<String>,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub tags: Vec<String>,
pub description: String
}
#[derive(Serialize, Deserialize)]
pub struct Spell {
pub name: String,
pub school: String,
pub level: i32,
pub ritual: bool,
pub casting_time: String,
pub range: String,
pub components: Components,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub description: String
}
#[derive(Serialize, Deserialize)]
pub struct Components {
pub verbal: bool,
pub somatic: bool,
pub material: bool,
pub materials_needed: Option<String>
}
impl Spell {
/// Convert spell to insertable struct
pub fn to_insert(self) -> InsertSpell {
return InsertSpell {
name: self.name,
school: self.school,
level: self.level,
ritual: self.ritual,
casting_time: self.casting_time,
range: self.range,
components_verbal: self.components.verbal,
components_somatic: self.components.somatic,
components_material: self.components.material,
components_materials_needed: self.components.materials_needed,
duration: self.duration,
classes: self.classes,
sources: self.sources,
tags: vec![],
description: self.description
}
}
/// Convert query struct to spell
pub fn from_query(query: QuerySpell) -> Self {
return Self {
name: query.name,
school: query.school,
level: query.level,
ritual: query.ritual,
casting_time: query.casting_time,
range: query.range,
components: Components {
verbal: query.components_verbal,
somatic: query.components_somatic,
material: query.components_material,
materials_needed: query.components_materials_needed
},
duration: query.duration,
classes: query.classes,
sources: query.sources,
description: query.description
}
}
/// Convert file to spell
pub fn from_file(file: String) -> Self {
let data = std::fs::read_to_string(file).unwrap();
let spell: Spell = serde_json::from_str(&data).unwrap();
return spell;
}
}

3
src/db/users/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod model;
pub use model::*;

3
src/db/users/model.rs Normal file
View File

@@ -0,0 +1,3 @@
pub struct User {
pub id: i32
}

View File

@@ -22,8 +22,6 @@ use songbird::SerenityInit;
mod commands; mod commands;
mod error_handler; mod error_handler;
mod db; mod db;
mod messages;
mod schema;
struct Handler { struct Handler {
// Open AI Config // Open AI Config
oai: Option<commands::oai::OAI> oai: Option<commands::oai::OAI>

View File

@@ -1,14 +0,0 @@
diesel::table! {
messages (id) {
id -> Text,
guild_id -> BigInt,
channel_id -> BigInt,
user_id -> BigInt,
created -> BigInt,
model -> Text,
request -> Text,
response -> Text,
request_tags -> Array<Text>,
response_tags -> Array<Text>,
}
}