summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/commands/random.rs29
-rw-r--r--src/utils.rs33
4 files changed, 45 insertions, 19 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5211b99..d5324d3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1500,6 +1500,7 @@ dependencies = [
"reqwest",
"serde",
"tokio",
+ "url",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 677d567..0d1c122 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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(())
+}