~phate/rsPhate

ref: 15191e34eee9b6d0b5cd4b50ba7153e3482b330e rsPhate/src/commands/neko.rs -rw-r--r-- 1.1 KiB
15191e34ValleyKnight add neko command, remove rofl dependency 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use log::{error, info};
use serde::Deserialize;
use serenity::{
    framework::standard::{macros::command, Args, CommandResult},
    model::channel::Message,
    prelude::*
};
use std::fmt;

#[derive(Deserialize)]
struct Image {
    url: String,
}

impl fmt::Display for Image {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.url)
    }
}

#[command]
#[description = "Post a random neko based on the category."]
#[usage = "category"]
#[example = "fox_girl"]
#[example = "hentai"]
#[example = "neko"]
#[example = "nsfw_neko_gif"]
#[example = "Random_hentai_gif"]
#[example = "smug"]
#[example = "waifu"]
#[num_args(1)]
fn neko(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
    let cat = args.single::<String>()?;
    let url = format!("https://nekos.life/api/v2/img/{}", cat);
    let body: Image = reqwest::blocking::get(&url)?.json()?;
    let message = body.to_string();
    info!("message = {}", message.trim());

    if let Err(why) = msg.channel_id.say(&ctx.http, &message) {
        error!("Could not send image because: {}", why);
    }

    Ok(())
}