Moved insults and compliments into the db
+ Fmt
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
use crate::error::Error;
|
||||
use crate::imgur;
|
||||
use crate::imgur::Image;
|
||||
use crate::insult_compliment::InsultComplimentTemplate;
|
||||
use crate::inventory::InventoryManager;
|
||||
use config::{Config, File};
|
||||
use j_db::database::Database;
|
||||
@@ -51,10 +50,6 @@ pub struct BotConfig {
|
||||
pub albums: Vec<AlbumConfig>,
|
||||
|
||||
pub motivation: MotivationConfig,
|
||||
|
||||
pub insults: Vec<InsultComplimentTemplate>,
|
||||
|
||||
pub compliments: Vec<InsultComplimentTemplate>,
|
||||
}
|
||||
|
||||
impl BotConfig {
|
||||
|
||||
+13
-13
@@ -1,8 +1,7 @@
|
||||
use crate::error::Error;
|
||||
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
|
||||
use crate::models::random::RandomConfig;
|
||||
use crate::{command, group, GlobalData, BAD_APPLE};
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::thread_rng;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
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 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 {
|
||||
let mut msg_builder = MessageBuilder::new();
|
||||
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() {
|
||||
dm_channel.say(&ctx.http, msg_builder.build()).await?;
|
||||
dm_channel.say(&ctx.http, msg_builder.build()).await?;
|
||||
}
|
||||
} else {
|
||||
msg.reply(
|
||||
@@ -264,10 +263,10 @@ async fn insult(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let data = ctx.data.read().await;
|
||||
let global = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let selection = if msg.content.as_str().starts_with("!insult") {
|
||||
&global.cfg.insults
|
||||
let pool = if msg.content.as_str().starts_with("!insult") {
|
||||
ResponseType::Insult
|
||||
} else if msg.content.as_str().starts_with("!compliment") {
|
||||
&global.cfg.compliments
|
||||
ResponseType::Compliment
|
||||
} else {
|
||||
msg.reply(&ctx, "The h*ck did you just say to me??").await?;
|
||||
return Ok(());
|
||||
@@ -275,13 +274,14 @@ async fn insult(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
|
||||
let target = args.rest();
|
||||
|
||||
let output = selection
|
||||
.iter()
|
||||
.choose(&mut thread_rng())
|
||||
.unwrap()
|
||||
.render(target)?;
|
||||
let output = RandomResponseTemplate::get_random_response(&global.db, pool, 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(())
|
||||
}
|
||||
|
||||
@@ -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)?)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ mod config;
|
||||
mod discord;
|
||||
mod error;
|
||||
mod imgur;
|
||||
mod insult_compliment;
|
||||
mod inventory;
|
||||
mod models;
|
||||
mod user;
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod insult_compliment;
|
||||
pub mod random;
|
||||
|
||||
Reference in New Issue
Block a user