Added insults and compliments

+ clippy + fmt
This commit is contained in:
Joey Hines 2022-12-30 22:33:48 -06:00
parent 65ea879ffa
commit 7c9cb2a81f
5 changed files with 67 additions and 1 deletions

View File

@ -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<RandomConfig>,
pub wallet_manager: WalletManager,
pub insults: Vec<InsultComplimentTemplate>,
pub compliments: Vec<InsultComplimentTemplate>,
}
impl BotConfig {

View File

@ -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::<GlobalData>().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(())
}

View File

@ -34,6 +34,12 @@ impl From<serenity::Error> for Error {
}
}
impl From<tera::Error> 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 {

25
src/insult_compliment.rs Normal file
View File

@ -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<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,6 +2,7 @@ mod config;
mod discord;
mod error;
mod imgur;
mod insult_compliment;
mod wallet;
use crate::config::{Args, BotConfig, Channel, GlobalData};