From 79f023a225c94fc46cc18cba196b5e72d62011c8 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 14 Nov 2022 18:59:35 -0700 Subject: [PATCH] Add dad jokes + fmt + clippy --- src/discord/album.rs | 19 +++++++++++++++++++ src/discord/color.rs | 2 ++ src/discord/joke.rs | 37 +++++++++++++++++++++++++++++++++++++ src/discord/mod.rs | 19 ++++++++++++++++--- src/main.rs | 1 + 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/discord/joke.rs diff --git a/src/discord/album.rs b/src/discord/album.rs index a5fd8ad..f05fd05 100644 --- a/src/discord/album.rs +++ b/src/discord/album.rs @@ -12,11 +12,27 @@ pub struct Album; #[only_in(guilds)] #[min_args(2)] #[max_args(2)] +#[description("Add an imgur album command.")] +#[usage(" ")] async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let album_name = args.parse::()?; args.advance(); let album_id = args.parse::()?; + let album_id: String = if album_id.contains("imgur") { + let parts: Vec<&str> = album_id.split("/a/").collect(); + + if parts.len() == 2 { + parts[1].to_string() + } else { + msg.reply(&ctx.http, "Invalid imgur album, check your link.") + .await?; + return Ok(()); + } + } else { + album_id + }; + let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); @@ -41,6 +57,8 @@ async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul #[command] #[only_in(guilds)] #[max_args(1)] +#[description("Remove an imgur album command.")] +#[usage("")] async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let album_name = args.parse::()?; @@ -67,6 +85,7 @@ async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult #[command] #[aliases("albums")] +#[description("List all album commands.")] #[only_in(guilds)] async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let data = ctx.data.read().await; diff --git a/src/discord/color.rs b/src/discord/color.rs index 233a0e0..0dbc295 100644 --- a/src/discord/color.rs +++ b/src/discord/color.rs @@ -12,6 +12,7 @@ pub struct Color; #[example("#35BB1D")] #[example("0x35BB1D")] #[example("53 187 29")] +#[description("Set your name color.")] #[min_args(1)] #[max_args(3)] #[only_in(guilds)] @@ -79,6 +80,7 @@ async fn set_color(ctx: &Context, msg: &Message, args: Args) -> CommandResult { #[command] #[max_args(0)] #[only_in(guilds)] +#[description("Remove your name color.")] async fn remove_color(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let guild = msg.guild(&ctx.cache).unwrap(); diff --git a/src/discord/joke.rs b/src/discord/joke.rs new file mode 100644 index 0000000..0c046fd --- /dev/null +++ b/src/discord/joke.rs @@ -0,0 +1,37 @@ +use crate::{command, group}; +use reqwest::Client; +use serde::{Deserialize, Serialize}; +use serenity::client::Context; +use serenity::framework::standard::{Args, CommandResult}; +use serenity::model::channel::Message; + +#[derive(Clone, Serialize, Deserialize)] +struct DadJoke { + pub id: String, + pub joke: String, + pub status: i32, +} + +#[group] +#[commands(dad_joke)] +pub struct Joke; + +#[command] +#[only_in(guilds)] +#[aliases("dad")] +#[description("Ask your dad")] +async fn dad_joke(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { + let client = Client::new(); + + let joke: DadJoke = client + .get("https://icanhazdadjoke.com/") + .header("Accept", "application/json") + .send() + .await? + .json() + .await?; + + msg.reply(&ctx.http, joke.joke).await?; + + Ok(()) +} diff --git a/src/discord/mod.rs b/src/discord/mod.rs index 1825b80..01fd4f6 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -1,6 +1,7 @@ pub mod album; pub mod celeryman; pub mod color; +pub mod joke; use crate::error::Error; use crate::{help, hook, imgur, GlobalData}; @@ -18,6 +19,10 @@ use std::collections::HashSet; pub struct Handler; +static ERROR_MSG: &str = + "OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our + headquarters are working VEWY HAWD to fix this!"; + #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, new_message: Message) { @@ -47,14 +52,22 @@ impl EventHandler for Handler { #[hook] pub async fn after( - _ctx: &Context, - _msg: &Message, + ctx: &Context, + msg: &Message, command_name: &str, command_result: CommandResult, ) { match command_result { Ok(()) => println!("Processed command '{}'", command_name), - Err(why) => println!("Command '{}' returned error {:?}", command_name, why), + Err(why) => { + println!("Command '{}' returned error {:?}", command_name, why); + msg.reply( + &ctx.http, + format!("{} (Error in command {})", ERROR_MSG, command_name), + ) + .await + .unwrap(); + } } } diff --git a/src/main.rs b/src/main.rs index c876092..72518e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ async fn main() { .group(&discord::color::COLOR_GROUP) .group(&discord::album::ALBUM_GROUP) .group(&discord::celeryman::CELERYMAN_GROUP) + .group(&discord::joke::JOKE_GROUP) .unrecognised_command(unrecognised_command_hook) .help(&discord::MY_HELP) .after(discord::after);