From 333d6d8baa558bace3d499172d4a74d68a5cf715 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 23 Dec 2019 11:57:12 -0500 Subject: [PATCH] images command working --- youmubot/src/commands/fun/images.rs | 101 ++++++++++++++++++++++++++++ youmubot/src/commands/fun/mod.rs | 4 +- youmubot/src/main.rs | 1 + 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 youmubot/src/commands/fun/images.rs diff --git a/youmubot/src/commands/fun/images.rs b/youmubot/src/commands/fun/images.rs new file mode 100644 index 0000000..bfd2229 --- /dev/null +++ b/youmubot/src/commands/fun/images.rs @@ -0,0 +1,101 @@ +use crate::http::HTTP; +use reqwest::Client as HTTPClient; +use serde::Deserialize; +use serenity::framework::standard::CommandError as Error; +use serenity::prelude::*; +use serenity::{ + framework::standard::{ + macros::{check, command}, + Args, CheckResult, CommandOptions, CommandResult, Reason, + }, + model::channel::{Channel, Message}, +}; +use std::string::ToString; + +#[command] +#[checks(nsfw)] +#[description = "šŸ–¼ļø Find an image with a given tag on Danbooru[nsfw]!"] +#[min_args(1)] +#[bucket("images")] +pub fn nsfw(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { + message_command(ctx, msg, args, Rating::Explicit) +} + +#[command] +#[description = "šŸ–¼ļø Find an image with a given tag on Danbooru[safe]!"] +#[min_args(1)] +#[bucket("images")] +pub fn image(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { + message_command(ctx, msg, args, Rating::Safe) +} + +#[check] +#[name = "nsfw"] +fn nsfw_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult { + let channel = msg.channel_id.to_channel(&ctx).unwrap(); + if !(match channel { + Channel::Guild(guild_channel) => guild_channel.read().nsfw, + _ => true, + }) { + CheckResult::Failure(Reason::User("šŸ˜£ YOU FREAKING PERVERT!!!".to_owned())) + } else { + CheckResult::Success + } +} + +fn message_command(ctx: &mut Context, msg: &Message, args: Args, rating: Rating) -> CommandResult { + let tags = args.remains().unwrap_or("touhou"); + let http = ctx.data.read(); + let http = http.get::().unwrap(); + let image = get_image(http, rating, tags)?; + match image { + None => msg.reply(&ctx, "šŸ–¼ļø No image found...\nšŸ’” Tip: In danbooru, character names follow Japanese standards (last name before first name), so **Hakurei Reimu** might give you an image while **Reimu Hakurei** won't."), + Some(url) => msg.reply( + &ctx, + format!("šŸ–¼ļø Here's the image you requested!\n\n{}", url), + ), + }?; + Ok(()) +} + +// Gets an image URL. +fn get_image(client: &HTTPClient, rating: Rating, tags: &str) -> Result, Error> { + // Fix the tags: change whitespaces to + + let tags = tags.split_whitespace().collect::>().join("_"); + let req = client + .get(&format!( + "https://danbooru.donmai.us/posts.json?tags=rating:{}+{}", + rating.to_string(), + tags + )) + .query(&[("limit", "1"), ("random", "true")]) + .build()?; + println!("{:?}", req.url()); + let response: Vec = client.execute(req)?.json()?; + Ok(response + .into_iter() + .next() + .map(|v| format!("https://danbooru.donmai.us/posts/{}", v.id))) +} + +#[derive(Deserialize, Debug)] +struct PostResponse { + id: u64, +} + +#[derive(Copy, Clone, Debug)] +enum Rating { + Explicit, + Safe, +} + +impl ToString for Rating { + fn to_string(&self) -> String { + use Rating::*; + match self { + Explicit => "explicit", + Safe => "safe", + } + .to_owned() + } +} diff --git a/youmubot/src/commands/fun/mod.rs b/youmubot/src/commands/fun/mod.rs index f1645b5..1d68806 100644 --- a/youmubot/src/commands/fun/mod.rs +++ b/youmubot/src/commands/fun/mod.rs @@ -12,9 +12,11 @@ use serenity::{ utils::MessageBuilder, }; +mod images; mod names; mod votes; +use images::*; use votes::VOTE_COMMAND; group!({ @@ -22,7 +24,7 @@ group!({ options: { description: "Random commands", }, - commands: [roll, pick, name, vote], + commands: [roll, pick, name, vote, image, nsfw], }); #[command] diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index 95e3ea0..70f8ac6 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -114,6 +114,7 @@ fn setup_framework(mut client: Client) -> Client { .bucket("voting", |c| { c.delay(120 /* 2 minutes */).time_span(120).limit(1) }) + .bucket("images", |c| c.time_span(60).limit(2)) // groups here .group(&commands::ADMIN_GROUP) .group(&commands::FUN_GROUP),