Add dad jokes
+ fmt + clippy
This commit is contained in:
parent
5a65fdf54d
commit
79f023a225
@ -12,11 +12,27 @@ pub struct Album;
|
||||
#[only_in(guilds)]
|
||||
#[min_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 {
|
||||
let album_name = args.parse::<String>()?;
|
||||
args.advance();
|
||||
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 global_data = data.get_mut::<GlobalData>().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("<name>")]
|
||||
async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let album_name = args.parse::<String>()?;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
37
src/discord/joke.rs
Normal file
37
src/discord/joke.rs
Normal 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(())
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user