Add dad jokes

+ fmt + clippy
This commit is contained in:
Joey Hines 2022-11-14 18:59:35 -07:00
parent 5a65fdf54d
commit 79f023a225
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
5 changed files with 75 additions and 3 deletions

View File

@ -12,11 +12,27 @@ pub struct Album;
#[only_in(guilds)] #[only_in(guilds)]
#[min_args(2)] #[min_args(2)]
#[max_args(2)] #[max_args(2)]
#[description("Add an imgur album command.")]
#[usage("<name> <album link>")]
async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let album_name = args.parse::<String>()?; let album_name = args.parse::<String>()?;
args.advance(); args.advance();
let album_id = args.parse::<String>()?; let album_id = args.parse::<String>()?;
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 mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap(); let global_data = data.get_mut::<GlobalData>().unwrap();
@ -41,6 +57,8 @@ async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[max_args(1)] #[max_args(1)]
#[description("Remove an imgur album command.")]
#[usage("<name>")]
async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let album_name = args.parse::<String>()?; let album_name = args.parse::<String>()?;
@ -67,6 +85,7 @@ async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult
#[command] #[command]
#[aliases("albums")] #[aliases("albums")]
#[description("List all album commands.")]
#[only_in(guilds)] #[only_in(guilds)]
async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let data = ctx.data.read().await; let data = ctx.data.read().await;

View File

@ -12,6 +12,7 @@ pub struct Color;
#[example("#35BB1D")] #[example("#35BB1D")]
#[example("0x35BB1D")] #[example("0x35BB1D")]
#[example("53 187 29")] #[example("53 187 29")]
#[description("Set your name color.")]
#[min_args(1)] #[min_args(1)]
#[max_args(3)] #[max_args(3)]
#[only_in(guilds)] #[only_in(guilds)]
@ -79,6 +80,7 @@ async fn set_color(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
#[command] #[command]
#[max_args(0)] #[max_args(0)]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Remove your name color.")]
async fn remove_color(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { async fn remove_color(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let guild = msg.guild(&ctx.cache).unwrap(); let guild = msg.guild(&ctx.cache).unwrap();

37
src/discord/joke.rs Normal file
View File

@ -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(())
}

View File

@ -1,6 +1,7 @@
pub mod album; pub mod album;
pub mod celeryman; pub mod celeryman;
pub mod color; pub mod color;
pub mod joke;
use crate::error::Error; use crate::error::Error;
use crate::{help, hook, imgur, GlobalData}; use crate::{help, hook, imgur, GlobalData};
@ -18,6 +19,10 @@ use std::collections::HashSet;
pub struct Handler; 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] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, new_message: Message) { async fn message(&self, ctx: Context, new_message: Message) {
@ -47,14 +52,22 @@ impl EventHandler for Handler {
#[hook] #[hook]
pub async fn after( pub async fn after(
_ctx: &Context, ctx: &Context,
_msg: &Message, msg: &Message,
command_name: &str, command_name: &str,
command_result: CommandResult, command_result: CommandResult,
) { ) {
match command_result { match command_result {
Ok(()) => println!("Processed command '{}'", command_name), 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();
}
} }
} }

View File

@ -33,6 +33,7 @@ async fn main() {
.group(&discord::color::COLOR_GROUP) .group(&discord::color::COLOR_GROUP)
.group(&discord::album::ALBUM_GROUP) .group(&discord::album::ALBUM_GROUP)
.group(&discord::celeryman::CELERYMAN_GROUP) .group(&discord::celeryman::CELERYMAN_GROUP)
.group(&discord::joke::JOKE_GROUP)
.unrecognised_command(unrecognised_command_hook) .unrecognised_command(unrecognised_command_hook)
.help(&discord::MY_HELP) .help(&discord::MY_HELP)
.after(discord::after); .after(discord::after);