diff --git a/src/discord/mod.rs b/src/discord/mod.rs index dad88b8..5d3564e 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -7,6 +7,7 @@ mod emoji_race; mod fren_coin; mod joke; mod little_fren; +mod motivate; use crate::config::GlobalData; use crate::error::Error; @@ -56,13 +57,9 @@ async fn event_handler( } serenity::FullEvent::Message { new_message } => { if new_message.content.starts_with("!") { - if find_command( - &framework.options.commands, - &new_message.content, - true, - &mut Vec::new(), - ) - .is_none() + let command = new_message.content.replace("!", ""); + if find_command(&framework.options.commands, &command, true, &mut Vec::new()) + .is_none() { handle_unrecognised_commands(ctx, new_message, data).await?; } @@ -201,6 +198,8 @@ pub async fn run_bot(global_data: GlobalData) { little_fren::give_medicine(), little_fren::give_water(), little_fren::play(), + motivate::motivation(), + motivate::motivation_add_album(), ], event_handler: |ctx, event, framework, data| { Box::pin(event_handler(ctx, event, framework, data)) diff --git a/src/discord/motivate.rs b/src/discord/motivate.rs index a3f7bfd..0c4e19e 100644 --- a/src/discord/motivate.rs +++ b/src/discord/motivate.rs @@ -1,19 +1,13 @@ use crate::album_manager::AlbumQuery; +use crate::discord::Context; +use crate::error::Error; use crate::models::motivation::{Motivation, MotivationConfig}; -use crate::{command, group, GlobalData}; use magick_rust::{DrawingWand, MagickWand, PixelWand}; +use poise::serenity_prelude::builder::{CreateAttachment, CreateMessage}; use reqwest::Client; -use serenity::builder::{CreateAttachment, CreateMessage}; -use serenity::client::Context; -use serenity::framework::standard::{Args, CommandError, CommandResult}; -use serenity::model::channel::Message; use std::borrow::Cow; -#[group] -#[commands(motivation, motivation_add_album)] -pub struct Motivate; - -pub async fn create_motivation_image(motivation: Motivation) -> Result, CommandError> { +pub async fn create_motivation_image(motivation: Motivation) -> Result, Error> { let client = Client::new(); let motivation_image_blob = client @@ -63,21 +57,14 @@ pub async fn create_motivation_image(motivation: Motivation) -> Result, Ok(wand.write_image_blob("jpg")?) } -#[command] -#[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_name = if args.is_empty() { - None - } else { - Some(args.parse::()?) - }; - +#[poise::command(prefix_command, category = "Motivation")] +pub async fn motivation( + ctx: Context<'_>, + #[description = "Album to use for the motivation"] album_name: Option, +) -> Result<(), Error> { let motivation = MotivationConfig::generate_motivation( - &global_data.db, - &global_data.picox, + &ctx.data().db, + &ctx.data().picox, "white", album_name, ) @@ -85,9 +72,9 @@ async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let image = create_motivation_image(motivation).await?; - msg.channel_id + ctx.channel_id() .send_message( - &ctx.http, + &ctx, CreateMessage::new() .content("Today's motivation") .add_file(CreateAttachment::bytes( @@ -100,14 +87,13 @@ async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult { Ok(()) } -#[command] -#[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::().unwrap(); - - let album = args.parse::()?; - let albums = global_data +#[poise::command(prefix_command, category = "Motivation")] +pub async fn motivation_add_album( + ctx: Context<'_>, + #[description = "Album to add to the motivation pool"] album: String, +) -> Result<(), Error> { + let albums = ctx + .data() .picox .query_album(AlbumQuery { album_name: Some(album), @@ -117,13 +103,13 @@ async fn motivation_add_album(ctx: &Context, msg: &Message, args: Args) -> Comma let album = if let Some(album) = albums.first().cloned() { album } else { - msg.reply(&ctx.http, "Fake album tbh, try again!").await?; + ctx.reply("Fake album tbh, try again!").await?; return Ok(()); }; - MotivationConfig::add_album(&global_data.db, album.id)?; + MotivationConfig::add_album(&ctx.data().db, album.id)?; - msg.reply(&ctx.http, "Done ;)").await?; + ctx.reply("Done ;)").await?; Ok(()) } diff --git a/src/error.rs b/src/error.rs index a17b6f9..be87c09 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,5 @@ use crate::user; +use magick_rust::MagickError; use serde::ser::StdError; use std::fmt::{write, Display, Formatter}; @@ -15,6 +16,7 @@ pub enum Error { ReqwestError(reqwest::Error), ParseFailure, RaasError(String), + MagickError(magick_rust::MagickError), } impl StdError for Error {} @@ -55,6 +57,12 @@ impl From for Error { } } +impl From for Error { + fn from(value: MagickError) -> Self { + Self::MagickError(value) + } +} + impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { @@ -67,6 +75,7 @@ impl Display for Error { Error::ReqwestError(e) => write!(f, "Reqwest Error: {}", e), Error::ParseFailure => write!(f, "Failed to parse something or other"), Error::RaasError(msg) => write!(f, "Got error from RaaS: {}", msg), + Error::MagickError(err) => write!(f, "ImageMagick error: {}", err), } } } diff --git a/src/models/motivation.rs b/src/models/motivation.rs index 6c68e72..30011a2 100644 --- a/src/models/motivation.rs +++ b/src/models/motivation.rs @@ -98,7 +98,7 @@ impl MotivationConfig { let action = actions.get_response(db)?.unwrap_or_default(); let goal = goals.get_response(db)?.unwrap_or_default(); - let image = picox.get_image_by_id(*image).await.unwrap(); + let image = picox.get_image_by_id(*image).await?; Ok(Motivation { image,