[no description]
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs58
1 files changed, 52 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index bcced29..add9ba9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,6 @@
#![allow(incomplete_features)]
#![feature(
impl_trait_in_fn_trait_return,
- try_blocks,
try_blocks_heterogeneous,
deref_patterns
)]
@@ -15,6 +14,7 @@ use anyhow::Result;
use fimg::scale::{Lanczos3, Nearest};
use fimg::{Image, WritePng};
use gif::Frame;
+use klipy::{Klipy, MediaItem, Sizes};
use poise::CreateReply;
use poise::samples::register_globally;
use regex::Regex;
@@ -129,7 +129,8 @@ pub async fn spawn() {
static TENOR: LazyLock<Regex> =
LazyLock::new(|| Regex::new("https?://tenor.com/view/.+-[0-9]+").unwrap());
-
+static KLIPY: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new("https://klipy.com/gifs/(.+)").unwrap());
static DISCORD: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"https:\/\/(?:cdn|media)\.discordapp\.(?:com|net)\/attachments\/\d+\/\d+\/(?<name>[.\w-]+)\.(?<ext>\w+)(?<tracking>\?[a-z]+=[0-9a-f]+(?:&[a-z]+=[0-9a-f]+)*)?",
@@ -196,7 +197,7 @@ async fn handle_message(
}
}
println!("hi");
- if let Some(x) = (retenor(&new_message.content).await?) {
+ if let Some(x) = retenor(&new_message.content).await? {
return Ok(Some(x));
}
if new_message.embeds.is_empty() {
@@ -212,13 +213,18 @@ async fn handle_message(
);
}
}
- for embed in dbg!(new_message.embeds.clone()) {
- let Some(u) = dbg!(embed.url) else { continue };
- if let Some(x) = embed.provider
+ for embed in new_message.embeds.clone() {
+ let Some(u) = embed.url else { continue };
+ if let Some(x) = &embed.provider
&& let Some("Tenor") = x.name
&& let Some(x) = retenor(&u).await?
{
return Ok(Some(x));
+ } else if let Some(x) = &embed.provider
+ && let Some("Klipy") = x.name
+ && let Some(x) = reklip(c.http(), u).await?
+ {
+ return Ok(Some(x));
} else if let Some(x) =
embed.image.or(embed.thumbnail.map(|x| unsafe { transmute(x) }))
&& x.height.is_none_or(|x| x > 160)
@@ -371,6 +377,46 @@ async fn retenor(u: &str) -> anyhow::Result<Option<CreateAttachment>> {
}
Ok(None)
}
+static KLIENT: LazyLock<Klipy> = LazyLock::new(|| {
+ klipy::Klipy::new(
+ "DaNOFv9lT9zKrNgF30FUpgE4xVex5fQJ86grPq557NzMCzGRNiuYJwlL88yBEFRU",
+ )
+});
+async fn reklip(
+ c: &Http,
+ u: String,
+) -> anyhow::Result<Option<CreateAttachment>> {
+ let a = if let Some(x) = KLIPY.captures(&u)
+ && let Some(slug) = x.get(1)
+ && let [
+ klipy::Item::Content(MediaItem {
+ ref slug,
+ file: Sizes { ref hd, ref md, ref sm, ref xs },
+ ..
+ }),
+ ] = KLIENT.gifs().items([slug.as_str()]).await?.data
+ && let fmts = [hd, md, sm, xs].into_iter().flatten()
+ && let Some(klipy::File { url, .. }) =
+ fmts.clone().filter_map(|x| x.gif.as_ref()).next()
+ {
+ let a = if let Some(klipy::File { url, .. }) = fmts
+ .filter_map(|x| x.gif.as_ref())
+ .find(|x| x.height <= 160 && x.height >= 110)
+ {
+ println!("easy");
+ CreateAttachment::url(c, url).await?
+ } else {
+ let gif = reqwest::get(url).await?.bytes().await?;
+ let (dec, vec) = regif(&gif)?;
+ let vec = vec(dec)?;
+ CreateAttachment::bytes(vec, format!("g{slug}.gif"))
+ };
+ Some(a)
+ } else {
+ None
+ };
+ Ok(a)
+}
async fn rediscord(
u: &str,