Add in motivation commands
This commit is contained in:
parent
8b9d1da206
commit
14a4316871
@ -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,12 +57,8 @@ 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(),
|
||||
)
|
||||
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))
|
||||
|
||||
@ -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<Vec<u8>, CommandError> {
|
||||
pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, Error> {
|
||||
let client = Client::new();
|
||||
|
||||
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")?)
|
||||
}
|
||||
|
||||
#[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::<GlobalData>().unwrap();
|
||||
|
||||
let album_name = if args.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(args.parse::<String>()?)
|
||||
};
|
||||
|
||||
#[poise::command(prefix_command, category = "Motivation")]
|
||||
pub async fn motivation(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Album to use for the motivation"] album_name: Option<String>,
|
||||
) -> 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::<GlobalData>().unwrap();
|
||||
|
||||
let album = args.parse::<String>()?;
|
||||
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(())
|
||||
}
|
||||
|
||||
@ -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<reqwest::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<magick_rust::MagickError> 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user