diff options
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/commands/random.rs | 29 | ||||
| -rw-r--r-- | src/utils.rs | 33 |
4 files changed, 45 insertions, 19 deletions
@@ -1500,6 +1500,7 @@ dependencies = [ "reqwest", "serde", "tokio", + "url", ] [[package]] @@ -23,3 +23,4 @@ reqwest = { version = "0.11.22", default-features = false, features = [ ] } serde = "1.0.192" tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] } +url = { version = "2.4.1", features = ["serde"] } diff --git a/src/commands/random.rs b/src/commands/random.rs index 6c61fdc..bc34928 100644 --- a/src/commands/random.rs +++ b/src/commands/random.rs @@ -23,29 +23,22 @@ pub async fn lore(ctx: Context<'_>) -> Result<(), Error> { /// get a random teawie #[poise::command(prefix_command, slash_command)] pub async fn teawie(ctx: Context<'_>) -> Result<(), Error> { - match api::guzzle::get_random_teawie().await { - Ok(resp) => { - ctx.say(resp).await?; - Ok(()) - } - Err(why) => { - ctx.say("i'm too lazy to send a selfie").await?; - Err(why) - } + if let Ok(url) = api::guzzle::get_random_teawie().await { + utils::send_url_as_embed(ctx, url).await + } else { + ctx.say("i'm too lazy to send a selfie right now :(") + .await?; + Ok(()) } } /// get a random shiggy #[poise::command(prefix_command, slash_command)] pub async fn shiggy(ctx: Context<'_>) -> Result<(), Error> { - match api::shiggy::get_random_shiggy().await { - Ok(resp) => { - ctx.say(resp).await?; - Ok(()) - } - Err(why) => { - ctx.say("i can't get a shiggy right now :(").await?; - Err(why) - } + if let Ok(url) = api::shiggy::get_random_shiggy().await { + utils::send_url_as_embed(ctx, url).await + } else { + ctx.say("i couldn't get a shiggy right now :(").await?; + Ok(()) } } diff --git a/src/utils.rs b/src/utils.rs index 780ecc9..f7f78de 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,10 @@ -use crate::{consts, Error}; +use crate::{colors, consts, Context, Error}; +use log::*; use once_cell::sync::Lazy; use poise::serenity_prelude::GuildId; use rand::seq::SliceRandom; +use url::Url; pub fn parse_snowflake_from_env<T, F: Fn(u64) -> T>(key: &str, f: F) -> Option<T> { std::env::var(key).ok().and_then(|v| v.parse().map(&f).ok()) @@ -50,3 +52,32 @@ pub fn is_guild_allowed(gid: GuildId) -> bool { ALLOWED_GUILDS.contains(&gid) } + +pub async fn send_url_as_embed(ctx: Context<'_>, url: String) -> Result<(), Error> { + match Url::parse(&url) { + Ok(parsed) => { + let title = parsed + .path_segments() + .unwrap() + .last() + .unwrap_or_else(|| "wie") + .replace("%20", " "); + + ctx.send(|c| { + c.embed(|e| { + e.title(title) + .image(&url) + .url(url) + .color(colors::Colors::Blue) + }) + }) + .await?; + } + Err(why) => { + error!("failed to parse url {}! {}", url, why); + ctx.say("i can't get that for you right now :(").await?; + } + } + + Ok(()) +} |
