Moved insults and compliments into the db

+ Fmt
This commit is contained in:
Joey Hines 2023-01-19 20:19:58 -07:00
parent ac6bd108da
commit f9776218e0
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
7 changed files with 122 additions and 63 deletions

52
Cargo.lock generated
View File

@ -417,9 +417,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.86" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579" checksum = "b61a7545f753a88bcbe0a70de1fcc0221e10bfc752f576754fa91e663db1622e"
dependencies = [ dependencies = [
"cc", "cc",
"cxxbridge-flags", "cxxbridge-flags",
@ -429,9 +429,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx-build" name = "cxx-build"
version = "1.0.86" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70" checksum = "f464457d494b5ed6905c63b0c4704842aba319084a0a3561cdc1359536b53200"
dependencies = [ dependencies = [
"cc", "cc",
"codespan-reporting", "codespan-reporting",
@ -444,15 +444,15 @@ dependencies = [
[[package]] [[package]]
name = "cxxbridge-flags" name = "cxxbridge-flags"
version = "1.0.86" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c" checksum = "43c7119ce3a3701ed81aca8410b9acf6fc399d2629d057b87e2efa4e63a3aaea"
[[package]] [[package]]
name = "cxxbridge-macro" name = "cxxbridge-macro"
version = "1.0.86" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5" checksum = "65e07508b90551e610910fa648a1878991d367064997a596135b86df30daf07e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1763,11 +1763,11 @@ dependencies = [
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.13" version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9"
dependencies = [ dependencies = [
"base64 0.13.1", "base64 0.21.0",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
@ -1800,6 +1800,7 @@ dependencies = [
"url", "url",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-streams",
"web-sys", "web-sys",
"webpki-roots", "webpki-roots",
"winreg", "winreg",
@ -1938,9 +1939,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.7.0" version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c" checksum = "645926f31b250a2dca3c232496c2d898d91036e45ca0e97e0e2390c54e11be36"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"core-foundation", "core-foundation",
@ -1951,9 +1952,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework-sys" name = "security-framework-sys"
version = "2.6.1" version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" checksum = "31c9bb296072e961fcbd8853511dd39c2d8be2deb1e17c6860b1d30732b323b4"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -2276,9 +2277,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]] [[package]]
name = "symphonia-core" name = "symphonia-core"
version = "0.5.1" version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199a6417cd4115bac79289b64b859358ea050b7add0ceb364dc991f628c5b347" checksum = "6b9567e2d8a5f866b2f94f5d366d811e0c6826babcff6d37de9e1a6690d38869"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bitflags", "bitflags",
@ -2691,9 +2692,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.8" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" checksum = "0046be40136ef78dc325e0edefccf84ccddacd0afcc1ca54103fa3c61bbdab1d"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
@ -2888,6 +2889,19 @@ version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "wasm-streams"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bbae3363c08332cadccd13b67db371814cd214c2524020932f0804b8cf7c078"
dependencies = [
"futures-util",
"js-sys",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.60" version = "0.3.60"

View File

@ -1,7 +1,6 @@
use crate::error::Error; use crate::error::Error;
use crate::imgur; use crate::imgur;
use crate::imgur::Image; use crate::imgur::Image;
use crate::insult_compliment::InsultComplimentTemplate;
use crate::inventory::InventoryManager; use crate::inventory::InventoryManager;
use config::{Config, File}; use config::{Config, File};
use j_db::database::Database; use j_db::database::Database;
@ -51,10 +50,6 @@ pub struct BotConfig {
pub albums: Vec<AlbumConfig>, pub albums: Vec<AlbumConfig>,
pub motivation: MotivationConfig, pub motivation: MotivationConfig,
pub insults: Vec<InsultComplimentTemplate>,
pub compliments: Vec<InsultComplimentTemplate>,
} }
impl BotConfig { impl BotConfig {

View File

@ -1,8 +1,7 @@
use crate::error::Error; use crate::error::Error;
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
use crate::models::random::RandomConfig; use crate::models::random::RandomConfig;
use crate::{command, group, GlobalData, BAD_APPLE}; use crate::{command, group, GlobalData, BAD_APPLE};
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use reqwest::Client; use reqwest::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::client::Context; use serenity::client::Context;
@ -170,7 +169,7 @@ pub async fn list_random(ctx: &Context, msg: &Message, args: Args) -> CommandRes
let random = RandomConfig::get_random(&global_data.db, &random_name)?; let random = RandomConfig::get_random(&global_data.db, &random_name)?;
let dm_channel = msg.author.id.create_dm_channel(&ctx.http).await?; let dm_channel = msg.author.id.create_dm_channel(&ctx.http).await?;
if let Some(random) = random { if let Some(random) = random {
let mut msg_builder = MessageBuilder::new(); let mut msg_builder = MessageBuilder::new();
msg_builder.push_line(format!("All possible responses for {}:", random_name)); msg_builder.push_line(format!("All possible responses for {}:", random_name));
@ -187,7 +186,7 @@ pub async fn list_random(ctx: &Context, msg: &Message, args: Args) -> CommandRes
} }
if !msg_builder.0.is_empty() { if !msg_builder.0.is_empty() {
dm_channel.say(&ctx.http, msg_builder.build()).await?; dm_channel.say(&ctx.http, msg_builder.build()).await?;
} }
} else { } else {
msg.reply( msg.reply(
@ -264,10 +263,10 @@ async fn insult(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let global = data.get::<GlobalData>().unwrap(); let global = data.get::<GlobalData>().unwrap();
let selection = if msg.content.as_str().starts_with("!insult") { let pool = if msg.content.as_str().starts_with("!insult") {
&global.cfg.insults ResponseType::Insult
} else if msg.content.as_str().starts_with("!compliment") { } else if msg.content.as_str().starts_with("!compliment") {
&global.cfg.compliments ResponseType::Compliment
} else { } else {
msg.reply(&ctx, "The h*ck did you just say to me??").await?; msg.reply(&ctx, "The h*ck did you just say to me??").await?;
return Ok(()); return Ok(());
@ -275,13 +274,14 @@ async fn insult(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let target = args.rest(); let target = args.rest();
let output = selection let output = RandomResponseTemplate::get_random_response(&global.db, pool, target)?;
.iter()
.choose(&mut thread_rng())
.unwrap()
.render(target)?;
msg.reply(&ctx.http, output).await?; if let Some(output) = output {
msg.reply(&ctx.http, output).await?;
} else {
msg.reply(&ctx, format!("No {}s, mr freeman??", msg.content))
.await?;
}
Ok(()) Ok(())
} }

View File

@ -1,25 +0,0 @@
use crate::error::Error;
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tera::{Context, Tera};
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct InsultComplimentTemplate {
pub template: String,
pub word_bank: HashMap<String, Vec<String>>,
}
impl InsultComplimentTemplate {
pub fn render(&self, target: &str) -> Result<String, Error> {
let mut context = Context::new();
context.insert("target", target);
for (key, words) in &self.word_bank {
context.insert(key, &words.choose(&mut thread_rng()).unwrap());
}
Ok(Tera::one_off(&self.template, &context, false)?)
}
}

View File

@ -2,7 +2,6 @@ mod config;
mod discord; mod discord;
mod error; mod error;
mod imgur; mod imgur;
mod insult_compliment;
mod inventory; mod inventory;
mod models; mod models;
mod user; mod user;

View File

@ -0,0 +1,75 @@
use crate::error::Error;
use j_db::database::Database;
use j_db::model::JdbModel;
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tera::{Context, Tera};
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub enum ResponseType {
Insult,
Compliment,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct RandomResponseTemplate {
id: Option<u64>,
pub response_type: ResponseType,
pub template: String,
pub word_bank: HashMap<String, Vec<String>>,
}
impl RandomResponseTemplate {
pub fn render(&self, target: &str) -> Result<String, Error> {
let mut context = Context::new();
context.insert("target", target);
for (key, words) in &self.word_bank {
context.insert(key, &words.choose(&mut thread_rng()).unwrap());
}
Ok(Tera::one_off(&self.template, &context, false)?)
}
pub fn get_all_responses_for_type(
db: &Database,
resp_type: ResponseType,
) -> Result<Vec<Self>, Error> {
Ok(db
.filter(|_, random_response: &RandomResponseTemplate| {
random_response.response_type == resp_type
})?
.collect())
}
pub fn get_random_response(
db: &Database,
resp_type: ResponseType,
target: &str,
) -> Result<Option<String>, Error> {
let templates = Self::get_all_responses_for_type(db, resp_type)?;
let template = templates.choose(&mut thread_rng());
if let Some(template) = template {
Ok(Some(template.render(target)?))
} else {
Ok(None)
}
}
}
impl JdbModel for RandomResponseTemplate {
fn id(&self) -> Option<u64> {
self.id
}
fn set_id(&mut self, id: u64) {
self.id = Some(id)
}
fn tree() -> String {
"RandomResponseTemplate".to_string()
}
}

View File

@ -1 +1,2 @@
pub mod insult_compliment;
pub mod random; pub mod random;