use crate::imgur::{get_album_images, Image}; 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, CommandResult}; use serenity::model::channel::{AttachmentType, Message}; use std::borrow::Cow; #[group] #[commands(motivation)] pub struct Motivate; #[command] #[only_in(guilds)] #[description("Let's give you motivation")] async fn motivation(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let data = ctx.data.read().await; let global_data = data.get::().unwrap(); 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 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 image = { let mut wand = MagickWand::new(); let mut border_wand = PixelWand::new(); wand.read_image_blob(motivation_image_blob)?; border_wand.set_color("white")?; let width = wand.get_image_width(); let border = width / 100; wand.border_image( &border_wand, border, border, magick_rust::bindings::CompositeOperator_OverCompositeOp, )?; border_wand.set_color("black")?; let width = wand.get_image_width(); let border = width * 20 / 100; wand.border_image( &border_wand, border, border, magick_rust::bindings::CompositeOperator_OverCompositeOp, )?; let text_pos_x = wand.get_image_width() as f64 / 2.0; let text_pos_y = wand.get_image_height() as f64 - (wand.get_image_height() as f64 * 0.05); let mut text_wand = DrawingWand::new(); let mut text_color_wand = PixelWand::new(); text_color_wand.set_color("white")?; 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.write_image_blob("png")? }; msg.channel_id .send_message(&ctx.http, |m| { m.content("Today's motivation") .add_file(AttachmentType::Bytes { data: Cow::from(image), filename: "motivate.png".to_string(), }) }) .await?; Ok(()) }