aboutsummaryrefslogtreecommitdiff
path: root/src/commands/pony.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/pony.rs')
-rw-r--r--src/commands/pony.rs137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/commands/pony.rs b/src/commands/pony.rs
new file mode 100644
index 0000000..75b4238
--- /dev/null
+++ b/src/commands/pony.rs
@@ -0,0 +1,137 @@
+use crate::{Context, Error};
+use poise::serenity_prelude as serenity;
+
+use serde::Deserialize;
+use std::collections::HashMap;
+
+#[derive(Deserialize)]
+pub struct PonyResponse {
+ pub pony: Pony,
+}
+
+#[derive(Deserialize)]
+pub struct Pony {
+ pub id: u64,
+ pub derpiId: Option<u64>,
+ pub tags: Vec<String>,
+ pub sourceURL: Option<String>,
+ pub height: u64,
+ pub width: u64,
+ pub aspectRatio: f64,
+ pub mimeType: String,
+ pub originalFormat: String,
+
+ pub representations: PonyRepresentation,
+}
+
+#[derive(Deserialize)]
+pub struct PonyRepresentation {
+ pub full: String,
+ pub tall: String,
+ pub large: String,
+ pub medium: String,
+ pub small: String,
+ pub thumb: String,
+ pub thumbSmall: String,
+ pub thumbTiny: String,
+}
+
+/// Retrieves a random SFW pony image
+#[poise::command(slash_command, prefix_command)]
+pub async fn randpony(ctx: Context<'_>) -> Result<(), Error> {
+ let mut response_msg = ctx.say("Fetching a random pony image, please wait!").await?;
+
+ let client = reqwest::Client::new();
+ let res = reqwest::get("https://theponyapi.com/api/v1/pony/random")
+ .await?
+ .json::<PonyResponse>()
+ .await?;
+
+ match response_msg.unwrap().message().await {
+ Ok(mut msg) => {
+ msg.edit(&ctx.discord(), |m| {
+ m.content("");
+ m.embed(|e| {
+ e.title("Pony!");
+ e.image(res.pony.representations.full.clone());
+ let res_tags = get_tags_as_string(res.pony.tags);
+ e.field("Tags", format!("{:?}", res_tags), true);
+ e.field("Image URL", res.pony.representations.full.clone(), true);
+ if let Some(url) = res.pony.sourceURL {
+ e.field("Source", url, true);
+ } else {
+ e.field("Source", "None found", true);
+ }
+ e
+ });
+ m
+ }).await?;
+ },
+ Err(e) => {
+ ctx.say("Error editing message").await?;
+ },
+ };
+
+ Ok(())
+}
+
+/// Get a random pony image based on a set of tags
+///
+/// Usage:
+/// ~tpony <tags>
+/// Example:
+/// ~tpony twilight sparkle,fluttershy
+#[poise::command(slash_command, prefix_command)]
+pub async fn tpony(ctx: Context<'_>,
+ #[description = "List of tags"]
+ #[rest]
+ tags: String,
+ ) -> Result<(), Error> {
+ let mut response_msg = ctx.say(format!("Fetching pony image based on tags: {:?}", tags.clone())).await?;
+
+ let res = reqwest::get(format!("https://theponyapi.com/api/v1/pony/random?q={}", tags).as_str())
+ .await?
+ .json::<PonyResponse>()
+ .await?;
+
+ match response_msg.unwrap().message().await {
+ Ok(mut msg) => {
+ msg.edit(&ctx.discord(), |m| {
+ m.content("");
+ m.embed(|e| {
+ e.title("Pony!");
+ e.image(res.pony.representations.full.clone());
+ let res_tags = get_tags_as_string(res.pony.tags);
+ e.field("Tags", format!("{:?}", res_tags), true);
+ e.field("Image URL", res.pony.representations.full.clone(), true);
+ if let Some(url) = res.pony.sourceURL {
+ e.field("Source", url, true);
+ } else {
+ e.field("Source", "None found", true);
+ }
+ e
+ });
+ m
+ }).await?;
+ },
+ Err(e) => {
+ ctx.say("Error editing message").await?;
+ },
+ };
+
+
+ Ok(())
+}
+
+fn get_tags_as_string(tags: Vec<String>) -> String {
+ let mut response = String::from(tags.get(0).unwrap());
+ for (i, s) in tags.iter().enumerate() {
+ if i > 0 {
+ response.push_str(format!(", {}", s).as_str());
+ } else {
+ continue;
+ }
+ }
+
+ response
+}