summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruku <[email protected]>2023-10-13 11:11:03 +0200
committerseth <[email protected]>2023-10-13 13:05:37 -0400
commit05e0831f7926dd4afda8e72c6ea5aa7a466b6bea (patch)
treea6e3a4b8c8c636e9d60b9c1b980e909d9b306d9d
parentd8d0c15a1a6f3abd9915eb2ac6807d72aa4f3171 (diff)
add random_shiggy command
-rw-r--r--src/commands/mod.rs1
-rw-r--r--src/commands/random_shiggy.rs40
-rw-r--r--src/main.rs2
3 files changed, 43 insertions, 0 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index fb7ee4b..1640707 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -3,4 +3,5 @@ pub mod bottom;
pub mod convert;
pub mod copypasta;
pub mod random_lore;
+pub mod random_shiggy;
pub mod random_teawie;
diff --git a/src/commands/random_shiggy.rs b/src/commands/random_shiggy.rs
new file mode 100644
index 0000000..b1f1511
--- /dev/null
+++ b/src/commands/random_shiggy.rs
@@ -0,0 +1,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!")
+}
diff --git a/src/main.rs b/src/main.rs
index 8a3aa94..51f0f3c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -111,6 +111,7 @@ impl EventHandler for Handler {
.await
}
"random_lore" => commands::random_lore::run(&command.data.options),
+ "random_shiggy" => commands::random_shiggy::run(&command.data.options).await,
"random_teawie" => commands::random_teawie::run(&command.data.options).await,
_ => "not implemented :(".to_string(),
};
@@ -145,6 +146,7 @@ impl EventHandler for Handler {
.create_application_command(commands::bottom::register)
.create_application_command(commands::convert::register)
.create_application_command(commands::random_lore::register)
+ .create_application_command(commands::random_shiggy::register)
.create_application_command(commands::random_teawie::register)
})
.await;