Added ability to pick albums for motivation
+ clippy + fmt
This commit is contained in:
parent
295f2704bc
commit
8ec84ff946
@ -59,12 +59,22 @@ pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>,
|
||||
|
||||
#[command]
|
||||
#[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 global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let motivation =
|
||||
MotivationConfig::generate_motivation(&global_data.db, &global_data.cfg.img_path, "white")
|
||||
let album_name = if args.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(args.parse::<String>()?)
|
||||
};
|
||||
|
||||
let motivation = MotivationConfig::generate_motivation(
|
||||
&global_data.db,
|
||||
&global_data.cfg.img_path,
|
||||
"white",
|
||||
album_name,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let image = create_motivation_image(motivation).await?;
|
||||
|
||||
@ -322,6 +322,7 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
|
||||
&global_data.db,
|
||||
&global_data.cfg.img_path,
|
||||
"gold",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use crate::album_manager::Album;
|
||||
use crate::error::Error;
|
||||
use j_db::database::Database;
|
||||
use j_db::error::JDbError;
|
||||
use j_db::model::JdbModel;
|
||||
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||
use rand::thread_rng;
|
||||
@ -80,11 +81,21 @@ impl MotivationConfig {
|
||||
db: &Database,
|
||||
base_path: &Path,
|
||||
border_color: &str,
|
||||
album_name: Option<String>,
|
||||
) -> Result<Motivation, Error> {
|
||||
let motivation = Self::get_motivation_config(db)?;
|
||||
|
||||
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 {
|
||||
let album = db.get::<Album>(album);
|
||||
|
||||
@ -93,6 +104,7 @@ impl MotivationConfig {
|
||||
images.append(&mut album_images);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let image = images
|
||||
.choose(&mut thread_rng())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user