summaryrefslogtreecommitdiff
path: root/src/commands/random_shiggy.rs
blob: b1f1511578126fa2dbdf1ea1f3155b60e8425211 (plain)
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
use crate::api::guzzle::REQWEST_CLIENT;
use reqwest::StatusCode;
use serde::Deserialize;
use serenity::builder::CreateApplicationCommand;
use serenity::model::prelude::application_command::CommandDataOption;

const URL: &str = "https://safebooru.donmai.us/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url";
const ERROR_MSG: &str = "couldn't get a shiggy";

#[derive(Deserialize)]
struct SafebooruResponse {
	file_url: String,
}

pub async fn run(_: &[CommandDataOption]) -> String {
	let resp = match REQWEST_CLIENT
		.execute(REQWEST_CLIENT.get(URL).build().unwrap())
		.await
	{
		Ok(r) => r,
		Err(e) => return format!("{} ({:?})", ERROR_MSG, e),
	};

	match resp.status() {
		StatusCode::OK => match resp.json::<SafebooruResponse>().await {
			Ok(sr) => sr.file_url,
			Err(e) => format!("{} ({:?})", ERROR_MSG, e),
		},
		other => {
			println!("{}", resp.text().await.unwrap());
			format!("{} ({:?})", ERROR_MSG, other)
		}
	}
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
	command
		.name("random_shiggy")
		.description("get a random shiggy!")
}