Add in motivation commands

This commit is contained in:
Joey Hines 2025-03-22 19:22:06 -06:00
parent 8b9d1da206
commit 14a4316871
Signed by: joeyahines
GPG Key ID: 38BA6F25C94C9382
4 changed files with 39 additions and 45 deletions

View File

@ -7,6 +7,7 @@ mod emoji_race;
mod fren_coin; mod fren_coin;
mod joke; mod joke;
mod little_fren; mod little_fren;
mod motivate;
use crate::config::GlobalData; use crate::config::GlobalData;
use crate::error::Error; use crate::error::Error;
@ -56,12 +57,8 @@ async fn event_handler(
} }
serenity::FullEvent::Message { new_message } => { serenity::FullEvent::Message { new_message } => {
if new_message.content.starts_with("!") { if new_message.content.starts_with("!") {
if find_command( let command = new_message.content.replace("!", "");
&framework.options.commands, if find_command(&framework.options.commands, &command, true, &mut Vec::new())
&new_message.content,
true,
&mut Vec::new(),
)
.is_none() .is_none()
{ {
handle_unrecognised_commands(ctx, new_message, data).await?; 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_medicine(),
little_fren::give_water(), little_fren::give_water(),
little_fren::play(), little_fren::play(),
motivate::motivation(),
motivate::motivation_add_album(),
], ],
event_handler: |ctx, event, framework, data| { event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data)) Box::pin(event_handler(ctx, event, framework, data))

View File

@ -1,19 +1,13 @@
use crate::album_manager::AlbumQuery; use crate::album_manager::AlbumQuery;
use crate::discord::Context;
use crate::error::Error;
use crate::models::motivation::{Motivation, MotivationConfig}; use crate::models::motivation::{Motivation, MotivationConfig};
use crate::{command, group, GlobalData};
use magick_rust::{DrawingWand, MagickWand, PixelWand}; use magick_rust::{DrawingWand, MagickWand, PixelWand};
use poise::serenity_prelude::builder::{CreateAttachment, CreateMessage};
use reqwest::Client; 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; use std::borrow::Cow;
#[group] pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, Error> {
#[commands(motivation, motivation_add_album)]
pub struct Motivate;
pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, CommandError> {
let client = Client::new(); let client = Client::new();
let motivation_image_blob = client let motivation_image_blob = client
@ -63,21 +57,14 @@ pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>,
Ok(wand.write_image_blob("jpg")?) Ok(wand.write_image_blob("jpg")?)
} }
#[command] #[poise::command(prefix_command, category = "Motivation")]
#[description("Let's give you motivation")] pub async fn motivation(
async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult { ctx: Context<'_>,
let data = ctx.data.read().await; #[description = "Album to use for the motivation"] album_name: Option<String>,
let global_data = data.get::<GlobalData>().unwrap(); ) -> Result<(), Error> {
let album_name = if args.is_empty() {
None
} else {
Some(args.parse::<String>()?)
};
let motivation = MotivationConfig::generate_motivation( let motivation = MotivationConfig::generate_motivation(
&global_data.db, &ctx.data().db,
&global_data.picox, &ctx.data().picox,
"white", "white",
album_name, album_name,
) )
@ -85,9 +72,9 @@ async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let image = create_motivation_image(motivation).await?; let image = create_motivation_image(motivation).await?;
msg.channel_id ctx.channel_id()
.send_message( .send_message(
&ctx.http, &ctx,
CreateMessage::new() CreateMessage::new()
.content("Today's motivation") .content("Today's motivation")
.add_file(CreateAttachment::bytes( .add_file(CreateAttachment::bytes(
@ -100,14 +87,13 @@ async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
Ok(()) Ok(())
} }
#[command] #[poise::command(prefix_command, category = "Motivation")]
#[description("Add imgur album to the motivation generator")] pub async fn motivation_add_album(
async fn motivation_add_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult { ctx: Context<'_>,
let data = ctx.data.read().await; #[description = "Album to add to the motivation pool"] album: String,
let global_data = data.get::<GlobalData>().unwrap(); ) -> Result<(), Error> {
let albums = ctx
let album = args.parse::<String>()?; .data()
let albums = global_data
.picox .picox
.query_album(AlbumQuery { .query_album(AlbumQuery {
album_name: Some(album), 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() { let album = if let Some(album) = albums.first().cloned() {
album album
} else { } else {
msg.reply(&ctx.http, "Fake album tbh, try again!").await?; ctx.reply("Fake album tbh, try again!").await?;
return Ok(()); 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(()) Ok(())
} }

View File

@ -1,4 +1,5 @@
use crate::user; use crate::user;
use magick_rust::MagickError;
use serde::ser::StdError; use serde::ser::StdError;
use std::fmt::{write, Display, Formatter}; use std::fmt::{write, Display, Formatter};
@ -15,6 +16,7 @@ pub enum Error {
ReqwestError(reqwest::Error), ReqwestError(reqwest::Error),
ParseFailure, ParseFailure,
RaasError(String), RaasError(String),
MagickError(magick_rust::MagickError),
} }
impl StdError for Error {} impl StdError for Error {}
@ -55,6 +57,12 @@ impl From<reqwest::Error> for Error {
} }
} }
impl From<magick_rust::MagickError> for Error {
fn from(value: MagickError) -> Self {
Self::MagickError(value)
}
}
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -67,6 +75,7 @@ impl Display for Error {
Error::ReqwestError(e) => write!(f, "Reqwest Error: {}", e), Error::ReqwestError(e) => write!(f, "Reqwest Error: {}", e),
Error::ParseFailure => write!(f, "Failed to parse something or other"), Error::ParseFailure => write!(f, "Failed to parse something or other"),
Error::RaasError(msg) => write!(f, "Got error from RaaS: {}", msg), Error::RaasError(msg) => write!(f, "Got error from RaaS: {}", msg),
Error::MagickError(err) => write!(f, "ImageMagick error: {}", err),
} }
} }
} }

View File

@ -98,7 +98,7 @@ impl MotivationConfig {
let action = actions.get_response(db)?.unwrap_or_default(); let action = actions.get_response(db)?.unwrap_or_default();
let goal = goals.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 { Ok(Motivation {
image, image,