Moved motivation config to the database

+ More refactoring
+ clippy + fmt
This commit is contained in:
2023-01-20 16:00:27 -07:00
parent 8d35d350ad
commit 9f5d68f4b2
7 changed files with 189 additions and 72 deletions
-6
View File
@@ -117,12 +117,6 @@ pub async fn give_coin(
let global_data = data.get_mut::<GlobalData>().unwrap();
User::give_funds(&global_data.db, user, number_of_coins as u32)?;
global_data
.cfg
.save(&global_data.args.cfg_path)
.await
.unwrap();
}
Ok(should_get_coin)
-5
View File
@@ -136,11 +136,6 @@ pub async fn after(
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
global_data
.cfg
.save(&global_data.args.cfg_path)
.await
.expect("Error saving config");
global_data.db.db.flush_async().await.unwrap();
}
+68 -42
View File
@@ -1,59 +1,30 @@
use crate::imgur::{get_album_images, Image};
use crate::models::motivation::{Motivation, MotivationConfig};
use crate::{command, group, GlobalData};
use magick_rust::{DrawingWand, MagickWand, PixelWand};
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use serenity::client::Context;
use serenity::framework::standard::{Args, CommandError, CommandResult};
use serenity::model::channel::{AttachmentType, Message};
use std::borrow::Cow;
#[group]
#[commands(motivation)]
#[commands(
motivation,
motivation_add_album,
motivation_add_action,
motivation_add_goal
)]
pub struct Motivate;
pub async fn create_image(
global_data: &GlobalData,
border_color: &str,
) -> Result<Vec<u8>, CommandError> {
let album = &global_data
.cfg
.motivation
.album
.iter()
.choose(&mut thread_rng())
.unwrap();
pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, CommandError> {
let motivation_image_blob = reqwest::get(&motivation.image_url).await?.bytes().await?;
let images = get_album_images(&global_data.cfg.imgur_client_id, album).await?;
let motivation_image_link: &Image = images.iter().choose(&mut thread_rng()).unwrap();
let motivation_image_blob = reqwest::get(&motivation_image_link.link)
.await?
.bytes()
.await?;
let action = global_data
.cfg
.motivation
.action
.iter()
.choose(&mut thread_rng())
.unwrap();
let goal = global_data
.cfg
.motivation
.goal
.iter()
.choose(&mut thread_rng())
.unwrap();
let motivation = format!("{} {}", action, goal);
let text = format!("{} {}", motivation.action, motivation.goal);
let mut wand = MagickWand::new();
let mut border_wand = PixelWand::new();
wand.read_image_blob(motivation_image_blob)?;
border_wand.set_color(border_color)?;
border_wand.set_color(&motivation.border_color)?;
let width = wand.get_image_width();
let border = width / 100;
wand.border_image(
@@ -82,7 +53,7 @@ pub async fn create_image(
text_wand.set_fill_color(&text_color_wand);
text_wand.set_font_size(0.07 * (wand.get_image_width() as f64));
text_wand.set_text_alignment(magick_rust::bindings::AlignType_CenterAlign);
wand.annotate_image(&text_wand, text_pos_x, text_pos_y, 0.0, &motivation)?;
wand.annotate_image(&text_wand, text_pos_x, text_pos_y, 0.0, &text)?;
Ok(wand.write_image_blob("png")?)
}
@@ -93,7 +64,14 @@ async fn motivation(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let image = create_image(global_data, "white").await?;
let motivation = MotivationConfig::generate_motivation(
&global_data.db,
&global_data.cfg.imgur_client_id,
"white",
)
.await?;
let image = create_motivation_image(motivation).await?;
msg.channel_id
.send_message(&ctx.http, |m| {
@@ -107,3 +85,51 @@ async fn motivation(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
Ok(())
}
#[command]
#[only_in(guilds)]
#[description("Add imgur album to the motivation generator")]
async fn motivation_add_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let album = args.parse::<String>()?;
MotivationConfig::add_album(&global_data.db, &album)?;
msg.reply(&ctx.http, "Done ;)").await?;
Ok(())
}
#[command]
#[only_in(guilds)]
#[description("Add an action to the motivation generator")]
async fn motivation_add_action(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let action = args.rest();
MotivationConfig::add_action(&global_data.db, action)?;
msg.reply(&ctx.http, "Done ;)").await?;
Ok(())
}
#[command]
#[only_in(guilds)]
#[description("Add goal to the motivation generator")]
async fn motivation_add_goal(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let goal = args.rest();
MotivationConfig::add_goal(&global_data.db, goal)?;
msg.reply(&ctx.http, "Done ;)").await?;
Ok(())
}
+10 -2
View File
@@ -1,6 +1,7 @@
use crate::discord::motivate::create_image;
use crate::discord::motivate::create_motivation_image;
use crate::error::Error;
use crate::inventory::{InventoryError, ItemData, ItemType, Operation};
use crate::models::motivation::MotivationConfig;
use crate::user::{User, UserError};
use crate::{command, group, GlobalData};
use rand::prelude::SliceRandom;
@@ -253,7 +254,14 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
tokio::fs::create_dir(&global_data.cfg.nft_path).await?
}
let nft = create_image(global_data, "gold").await.unwrap();
let nft_motivation = MotivationConfig::generate_motivation(
&global_data.db,
&global_data.cfg.imgur_client_id,
"gold",
)
.await
.unwrap();
let nft = create_motivation_image(nft_motivation).await.unwrap();
let mut hasher = DefaultHasher::new();
hasher.write(&nft);
let nft_hash = hasher.finish();