Moved motivation config to the database
+ More refactoring + clippy + fmt
This commit is contained in:
parent
8d35d350ad
commit
9f5d68f4b2
@ -21,13 +21,6 @@ pub struct Args {
|
|||||||
pub cfg_path: PathBuf,
|
pub cfg_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
pub struct MotivationConfig {
|
|
||||||
pub album: Vec<String>,
|
|
||||||
pub action: Vec<String>,
|
|
||||||
pub goal: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
pub struct BotConfig {
|
pub struct BotConfig {
|
||||||
pub bot_token: String,
|
pub bot_token: String,
|
||||||
@ -37,8 +30,6 @@ pub struct BotConfig {
|
|||||||
pub nft_path: PathBuf,
|
pub nft_path: PathBuf,
|
||||||
pub db_path: PathBuf,
|
pub db_path: PathBuf,
|
||||||
pub admins: Vec<UserId>,
|
pub admins: Vec<UserId>,
|
||||||
|
|
||||||
pub motivation: MotivationConfig,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BotConfig {
|
impl BotConfig {
|
||||||
@ -49,14 +40,6 @@ impl BotConfig {
|
|||||||
|
|
||||||
cfg.try_deserialize()
|
cfg.try_deserialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn save(&self, config_path: &Path) -> Result<(), tokio::io::Error> {
|
|
||||||
let output_str = toml::to_string_pretty(&self).unwrap();
|
|
||||||
|
|
||||||
tokio::fs::write(config_path, output_str).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@ -117,12 +117,6 @@ pub async fn give_coin(
|
|||||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||||
|
|
||||||
User::give_funds(&global_data.db, user, number_of_coins as u32)?;
|
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)
|
Ok(should_get_coin)
|
||||||
|
|||||||
@ -136,11 +136,6 @@ pub async fn after(
|
|||||||
|
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
let global_data = data.get::<GlobalData>().unwrap();
|
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();
|
global_data.db.db.flush_async().await.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,59 +1,30 @@
|
|||||||
use crate::imgur::{get_album_images, Image};
|
use crate::models::motivation::{Motivation, MotivationConfig};
|
||||||
use crate::{command, group, GlobalData};
|
use crate::{command, group, GlobalData};
|
||||||
use magick_rust::{DrawingWand, MagickWand, PixelWand};
|
use magick_rust::{DrawingWand, MagickWand, PixelWand};
|
||||||
use rand::prelude::IteratorRandom;
|
|
||||||
use rand::thread_rng;
|
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
use serenity::framework::standard::{Args, CommandError, CommandResult};
|
use serenity::framework::standard::{Args, CommandError, CommandResult};
|
||||||
use serenity::model::channel::{AttachmentType, Message};
|
use serenity::model::channel::{AttachmentType, Message};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[group]
|
#[group]
|
||||||
#[commands(motivation)]
|
#[commands(
|
||||||
|
motivation,
|
||||||
|
motivation_add_album,
|
||||||
|
motivation_add_action,
|
||||||
|
motivation_add_goal
|
||||||
|
)]
|
||||||
pub struct Motivate;
|
pub struct Motivate;
|
||||||
|
|
||||||
pub async fn create_image(
|
pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, CommandError> {
|
||||||
global_data: &GlobalData,
|
let motivation_image_blob = reqwest::get(&motivation.image_url).await?.bytes().await?;
|
||||||
border_color: &str,
|
|
||||||
) -> Result<Vec<u8>, CommandError> {
|
|
||||||
let album = &global_data
|
|
||||||
.cfg
|
|
||||||
.motivation
|
|
||||||
.album
|
|
||||||
.iter()
|
|
||||||
.choose(&mut thread_rng())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let images = get_album_images(&global_data.cfg.imgur_client_id, album).await?;
|
let text = format!("{} {}", motivation.action, motivation.goal);
|
||||||
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 mut wand = MagickWand::new();
|
let mut wand = MagickWand::new();
|
||||||
let mut border_wand = PixelWand::new();
|
let mut border_wand = PixelWand::new();
|
||||||
wand.read_image_blob(motivation_image_blob)?;
|
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 width = wand.get_image_width();
|
||||||
let border = width / 100;
|
let border = width / 100;
|
||||||
wand.border_image(
|
wand.border_image(
|
||||||
@ -82,7 +53,7 @@ pub async fn create_image(
|
|||||||
text_wand.set_fill_color(&text_color_wand);
|
text_wand.set_fill_color(&text_color_wand);
|
||||||
text_wand.set_font_size(0.07 * (wand.get_image_width() as f64));
|
text_wand.set_font_size(0.07 * (wand.get_image_width() as f64));
|
||||||
text_wand.set_text_alignment(magick_rust::bindings::AlignType_CenterAlign);
|
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")?)
|
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 data = ctx.data.read().await;
|
||||||
let global_data = data.get::<GlobalData>().unwrap();
|
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
|
msg.channel_id
|
||||||
.send_message(&ctx.http, |m| {
|
.send_message(&ctx.http, |m| {
|
||||||
@ -107,3 +85,51 @@ async fn motivation(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
|
|||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use crate::discord::motivate::create_image;
|
use crate::discord::motivate::create_motivation_image;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::inventory::{InventoryError, ItemData, ItemType, Operation};
|
use crate::inventory::{InventoryError, ItemData, ItemType, Operation};
|
||||||
|
use crate::models::motivation::MotivationConfig;
|
||||||
use crate::user::{User, UserError};
|
use crate::user::{User, UserError};
|
||||||
use crate::{command, group, GlobalData};
|
use crate::{command, group, GlobalData};
|
||||||
use rand::prelude::SliceRandom;
|
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?
|
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();
|
let mut hasher = DefaultHasher::new();
|
||||||
hasher.write(&nft);
|
hasher.write(&nft);
|
||||||
let nft_hash = hasher.finish();
|
let nft_hash = hasher.finish();
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
pub mod album;
|
pub mod album;
|
||||||
pub mod insult_compliment;
|
pub mod insult_compliment;
|
||||||
|
pub mod motivation;
|
||||||
pub mod random;
|
pub mod random;
|
||||||
|
|||||||
110
src/models/motivation.rs
Normal file
110
src/models/motivation.rs
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use crate::imgur::get_album_images;
|
||||||
|
use j_db::database::Database;
|
||||||
|
use j_db::model::JdbModel;
|
||||||
|
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||||
|
use rand::thread_rng;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct MotivationConfig {
|
||||||
|
id: Option<u64>,
|
||||||
|
pub album: Vec<String>,
|
||||||
|
pub action: Vec<String>,
|
||||||
|
pub goal: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JdbModel for MotivationConfig {
|
||||||
|
fn id(&self) -> Option<u64> {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_id(&mut self, id: u64) {
|
||||||
|
self.id = Some(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree() -> String {
|
||||||
|
"Motivation".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_unique(&self, _: &Self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MotivationConfig {
|
||||||
|
pub fn get_motivation_config(db: &Database) -> Result<Self, Error> {
|
||||||
|
Ok(db
|
||||||
|
.filter(|_, _: &MotivationConfig| true)?
|
||||||
|
.next()
|
||||||
|
.unwrap_or(Self {
|
||||||
|
id: None,
|
||||||
|
album: vec![],
|
||||||
|
action: vec![],
|
||||||
|
goal: vec![],
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_album(db: &Database, album: &str) -> Result<(), Error> {
|
||||||
|
let mut motivation = Self::get_motivation_config(db)?;
|
||||||
|
motivation.album.push(album.to_string());
|
||||||
|
|
||||||
|
db.insert(motivation)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_action(db: &Database, action: &str) -> Result<(), Error> {
|
||||||
|
let mut motivation = Self::get_motivation_config(db)?;
|
||||||
|
motivation.action.push(action.to_string());
|
||||||
|
|
||||||
|
db.insert(motivation)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_goal(db: &Database, goal: &str) -> Result<(), Error> {
|
||||||
|
let mut motivation = Self::get_motivation_config(db)?;
|
||||||
|
motivation.goal.push(goal.to_string());
|
||||||
|
|
||||||
|
db.insert(motivation)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn generate_motivation(
|
||||||
|
db: &Database,
|
||||||
|
imgur_client_id: &str,
|
||||||
|
border_color: &str,
|
||||||
|
) -> Result<Motivation, Error> {
|
||||||
|
let motivation = Self::get_motivation_config(db)?;
|
||||||
|
|
||||||
|
let mut images = Vec::new();
|
||||||
|
|
||||||
|
for album in motivation.album {
|
||||||
|
let mut album_images = get_album_images(imgur_client_id, &album).await?;
|
||||||
|
images.append(&mut album_images);
|
||||||
|
}
|
||||||
|
|
||||||
|
let image = images.choose(&mut thread_rng()).unwrap();
|
||||||
|
|
||||||
|
let action = motivation.action.iter().choose(&mut thread_rng()).unwrap();
|
||||||
|
|
||||||
|
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
|
||||||
|
|
||||||
|
Ok(Motivation {
|
||||||
|
image_url: image.link.clone(),
|
||||||
|
action: action.to_string(),
|
||||||
|
goal: goal.to_string(),
|
||||||
|
border_color: border_color.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct Motivation {
|
||||||
|
pub image_url: String,
|
||||||
|
pub action: String,
|
||||||
|
pub goal: String,
|
||||||
|
pub border_color: String,
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user