Added ability to pick albums for motivation

+ clippy + fmt
This commit is contained in:
Joey Hines 2023-09-16 10:18:47 -06:00
parent 295f2704bc
commit 8ec84ff946
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
3 changed files with 32 additions and 9 deletions

View File

@ -59,12 +59,22 @@ pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>,
#[command] #[command]
#[description("Let's give you motivation")] #[description("Let's give you motivation")]
async fn motivation(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { 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 motivation = let album_name = if args.is_empty() {
MotivationConfig::generate_motivation(&global_data.db, &global_data.cfg.img_path, "white") None
} else {
Some(args.parse::<String>()?)
};
let motivation = MotivationConfig::generate_motivation(
&global_data.db,
&global_data.cfg.img_path,
"white",
album_name,
)
.await?; .await?;
let image = create_motivation_image(motivation).await?; let image = create_motivation_image(motivation).await?;

View File

@ -322,6 +322,7 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
&global_data.db, &global_data.db,
&global_data.cfg.img_path, &global_data.cfg.img_path,
"gold", "gold",
None,
) )
.await .await
.unwrap(); .unwrap();

View File

@ -1,6 +1,7 @@
use crate::album_manager::Album; use crate::album_manager::Album;
use crate::error::Error; use crate::error::Error;
use j_db::database::Database; use j_db::database::Database;
use j_db::error::JDbError;
use j_db::model::JdbModel; use j_db::model::JdbModel;
use rand::prelude::{IteratorRandom, SliceRandom}; use rand::prelude::{IteratorRandom, SliceRandom};
use rand::thread_rng; use rand::thread_rng;
@ -80,11 +81,21 @@ impl MotivationConfig {
db: &Database, db: &Database,
base_path: &Path, base_path: &Path,
border_color: &str, border_color: &str,
album_name: Option<String>,
) -> Result<Motivation, Error> { ) -> Result<Motivation, Error> {
let motivation = Self::get_motivation_config(db)?; let motivation = Self::get_motivation_config(db)?;
let mut images = Vec::new(); let mut images = Vec::new();
if let Some(album_name) = album_name {
let album = Album::find_album_by_name_or_alias(db, &album_name)?;
if let Some(mut album) = album {
images.append(&mut album.images)
} else {
return Err(Error::from(JDbError::NotFound));
}
} else {
for album in motivation.album { for album in motivation.album {
let album = db.get::<Album>(album); let album = db.get::<Album>(album);
@ -93,6 +104,7 @@ impl MotivationConfig {
images.append(&mut album_images); images.append(&mut album_images);
} }
} }
}
let image = images let image = images
.choose(&mut thread_rng()) .choose(&mut thread_rng())