Add other db tables
This commit is contained in:
@@ -8,6 +8,10 @@ readme = "README.md"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[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"
|
||||
serde_json = "1.0.107"
|
||||
log = "0.4.20"
|
||||
|
||||
1
migrations/000001_create_races/down.sql
Normal file
1
migrations/000001_create_races/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE races;
|
||||
7
migrations/000001_create_races/up.sql
Normal file
7
migrations/000001_create_races/up.sql
Normal 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
|
||||
);
|
||||
1
migrations/000002_create_classes/down.sql
Normal file
1
migrations/000002_create_classes/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE classes;
|
||||
3
migrations/000002_create_classes/up.sql
Normal file
3
migrations/000002_create_classes/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS classes (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000003_create_feats/down.sql
Normal file
1
migrations/000003_create_feats/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE feats;
|
||||
3
migrations/000003_create_feats/up.sql
Normal file
3
migrations/000003_create_feats/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS feats (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000004_create_options_features/down.sql
Normal file
1
migrations/000004_create_options_features/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE options_features;
|
||||
3
migrations/000004_create_options_features/up.sql
Normal file
3
migrations/000004_create_options_features/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS options_features (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000005_create_backgrounds/down.sql
Normal file
1
migrations/000005_create_backgrounds/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE backgrounds;
|
||||
3
migrations/000005_create_backgrounds/up.sql
Normal file
3
migrations/000005_create_backgrounds/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS backgrounds (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000006_create_items/down.sql
Normal file
1
migrations/000006_create_items/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE items;
|
||||
3
migrations/000006_create_items/up.sql
Normal file
3
migrations/000006_create_items/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS items (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000007_create_spells/down.sql
Normal file
1
migrations/000007_create_spells/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE spells;
|
||||
18
migrations/000007_create_spells/up.sql
Normal file
18
migrations/000007_create_spells/up.sql
Normal 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
|
||||
);
|
||||
1
migrations/000008_create_conditions/down.sql
Normal file
1
migrations/000008_create_conditions/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE conditions;
|
||||
3
migrations/000008_create_conditions/up.sql
Normal file
3
migrations/000008_create_conditions/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS conditions (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
1
migrations/000009_create_bestiary/down.sql
Normal file
1
migrations/000009_create_bestiary/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE bestiary;
|
||||
3
migrations/000009_create_bestiary/up.sql
Normal file
3
migrations/000009_create_bestiary/up.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE IF NOT EXISTS bestiary (
|
||||
id INTEGER GENERATED ALWAYS AS IDENTITY
|
||||
);
|
||||
@@ -12,8 +12,7 @@ use serenity::model::channel::Message;
|
||||
use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType};
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::db;
|
||||
use crate::messages::{NewMessageDB, MessageDB};
|
||||
use crate::db::{connection, NewMessageDB, MessageDB};
|
||||
|
||||
pub struct OAI {
|
||||
pub client: reqwest::Client,
|
||||
@@ -189,7 +188,7 @@ impl OAI {
|
||||
|
||||
pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
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 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(), "");
|
||||
|
||||
// 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())
|
||||
.filter((crate::schema::messages::guild_id.eq(guild_id.0 as i64))
|
||||
.and(crate::schema::messages::channel_id.eq(channel_id.0 as i64))
|
||||
.and(crate::schema::messages::user_id.eq(author_id.0 as i64))
|
||||
.filter((crate::db::schema::messages::guild_id.eq(guild_id.0 as i64))
|
||||
.and(crate::db::schema::messages::channel_id.eq(channel_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)
|
||||
.load(&mut connection);
|
||||
|
||||
@@ -284,7 +283,7 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
if !r.choices.is_empty() {
|
||||
let res = r.choices[0].message.content.clone();
|
||||
// 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,
|
||||
guild_id: guild_id.0 as i64,
|
||||
channel_id: response_channel.0 as i64,
|
||||
|
||||
0
src/db/backgrounds/mod.rs
Normal file
0
src/db/backgrounds/mod.rs
Normal file
0
src/db/bestiary/mod.rs
Normal file
0
src/db/bestiary/mod.rs
Normal file
0
src/db/classes/mod.rs
Normal file
0
src/db/classes/mod.rs
Normal file
0
src/db/conditions/mod.rs
Normal file
0
src/db/conditions/mod.rs
Normal file
0
src/db/feats/mod.rs
Normal file
0
src/db/feats/mod.rs
Normal file
0
src/db/items/mod.rs
Normal file
0
src/db/items/mod.rs
Normal file
@@ -1,6 +1,6 @@
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::schema::messages;
|
||||
use crate::db::schema::messages;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = messages)]
|
||||
@@ -6,6 +6,21 @@ use log::{error, info};
|
||||
use r2d2;
|
||||
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>>;
|
||||
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
|
||||
|
||||
0
src/db/options/mod.rs
Normal file
0
src/db/options/mod.rs
Normal file
0
src/db/races/mod.rs
Normal file
0
src/db/races/mod.rs
Normal file
35
src/db/schema.rs
Normal file
35
src/db/schema.rs
Normal 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
3
src/db/spells/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod model;
|
||||
|
||||
pub use model::*;
|
||||
120
src/db/spells/model.rs
Normal file
120
src/db/spells/model.rs
Normal 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
3
src/db/users/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod model;
|
||||
|
||||
pub use model::*;
|
||||
3
src/db/users/model.rs
Normal file
3
src/db/users/model.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub struct User {
|
||||
pub id: i32
|
||||
}
|
||||
@@ -22,8 +22,6 @@ use songbird::SerenityInit;
|
||||
mod commands;
|
||||
mod error_handler;
|
||||
mod db;
|
||||
mod messages;
|
||||
mod schema;
|
||||
struct Handler {
|
||||
// Open AI Config
|
||||
oai: Option<commands::oai::OAI>
|
||||
|
||||
@@ -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>,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user