diff --git a/src/config.rs b/src/config.rs index 40e3733..57cd038 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use crate::error::Error; use crate::error::Error::NoAlbumFound; use crate::imgur; use crate::imgur::Image; +use crate::insult_compliment::InsultComplimentTemplate; use crate::wallet::WalletManager; use config::{Config, File}; use rand::prelude::SliceRandom; @@ -47,6 +48,10 @@ pub struct BotConfig { pub randoms: Vec, pub wallet_manager: WalletManager, + + pub insults: Vec, + + pub compliments: Vec, } impl BotConfig { diff --git a/src/discord/joke.rs b/src/discord/joke.rs index 9d2bf8d..4f84444 100644 --- a/src/discord/joke.rs +++ b/src/discord/joke.rs @@ -12,7 +12,7 @@ use std::collections::HashMap; use std::time::Duration; #[group] -#[commands(dad_joke, roll, bad_apple)] +#[commands(dad_joke, roll, bad_apple, insult)] pub struct Joke; #[derive(Clone, Serialize, Deserialize)] @@ -165,3 +165,32 @@ async fn bad_apple(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { Ok(()) } + +#[command] +#[aliases("compliment")] +#[only_in(guilds)] +async fn insult(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let data = ctx.data.read().await; + let global = data.get::().unwrap(); + + let selection = if msg.content.as_str().starts_with("!insult") { + &global.cfg.insults + } else if msg.content.as_str().starts_with("!compliment") { + &global.cfg.compliments + } else { + msg.reply(&ctx, "The h*ck did you just say to me??").await?; + return Ok(()); + }; + + let target = args.rest(); + + let output = selection + .iter() + .choose(&mut thread_rng()) + .unwrap() + .render(target)?; + + msg.reply(&ctx.http, output).await?; + + Ok(()) +} diff --git a/src/error.rs b/src/error.rs index 0d4fd20..a6b61bb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,6 +34,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: tera::Error) -> Self { + Self::TeraError(err) + } +} + impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { diff --git a/src/insult_compliment.rs b/src/insult_compliment.rs new file mode 100644 index 0000000..d863c87 --- /dev/null +++ b/src/insult_compliment.rs @@ -0,0 +1,25 @@ +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>, +} + +impl InsultComplimentTemplate { + pub fn render(&self, target: &str) -> Result { + 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)?) + } +} diff --git a/src/main.rs b/src/main.rs index 6c848ea..72465ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod config; mod discord; mod error; mod imgur; +mod insult_compliment; mod wallet; use crate::config::{Args, BotConfig, Channel, GlobalData};