From c40744e158a808076cbd2d16f4db88239fff2b93 Mon Sep 17 00:00:00 2001 From: tumGER Date: Wed, 30 Aug 2023 01:33:57 +0200 Subject: [PATCH] Add Download Button and interactions --- Cargo.lock | 6 +-- Cargo.toml | 2 +- sphene/src/main.rs | 93 ++++++++++++++++++++++++++++++++---------- thorium/src/twitter.rs | 3 -- 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9587c70..35d3e77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1443,7 +1443,7 @@ dependencies = [ [[package]] name = "sphene" -version = "6.0.0" +version = "6.1.0" dependencies = [ "dotenv", "regex", @@ -1558,7 +1558,7 @@ dependencies = [ [[package]] name = "thorium" -version = "6.0.0" +version = "6.1.0" dependencies = [ "reqwest", "scraper", @@ -1611,7 +1611,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "titanite" -version = "6.0.0" +version = "6.1.0" dependencies = [ "dotenv", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 0c4c0d7..7ad4078 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,5 @@ members = [ resolver = "2" [workspace.package] -version = "6.0.0" +version = "6.1.0" authors = ["AnnsAnn"] \ No newline at end of file diff --git a/sphene/src/main.rs b/sphene/src/main.rs index e3136b2..e3dab10 100644 --- a/sphene/src/main.rs +++ b/sphene/src/main.rs @@ -91,28 +91,73 @@ impl EventHandler for Handler { let component = interaction.as_message_component().unwrap().clone(); let command = component.data.values.get(0).unwrap(); - - // Make the Discord API happy no matter what :) - component - .create_interaction_response(&ctx.http, |r| { - r.kind(InteractionResponseType::DeferredUpdateMessage) - }) - .await - .unwrap(); - - if command == "version" || command == "menu" { - return; - } - let msg = &component.message; + if !msg.author.bot { return; } let user = &component.user.id.to_string(); // Check whether user is correct - if !msg.content.contains(user) { + if !msg.content.contains(user) + || command == "version" + || command == "download" + || command == "menu" + { + let content = if command == "version" { + "โ˜๏ธ The Source Code can be found at: https://github.com/AnnsAnna/sphene".to_string() + } else if command == "menu" { + "๐Ÿ•บ https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string() + } else if command == "download" { + let extracted_url = self + .regex_pattern + .find_iter(&msg.content) + .next() + .unwrap() + .as_str() + .to_string(); + + let url = if twitter::UrlType::from_string(&extracted_url) + != twitter::UrlType::Unknown + { + twitter::get_media_from_url( + twitter::convert_url_lazy(extracted_url, twitter::UrlType::Vxtwitter).await, + ) + .await + } else { + bluesky::get_media_from_url( + bluesky::convert_url_lazy(extracted_url, bluesky::UrlType::FixBluesky) + .await, + ) + .await + }; + + if url != "0" { + format!("โฌ Your Download URL is: <{}>", url) + } else { + "โš ๏ธ No Download URL found!".to_string() + } + } else { + "โš ๏ธ You are not the author of this message!".to_string() + }; + + component + .create_interaction_response(&ctx.http, |r| { + r.kind(InteractionResponseType::ChannelMessageWithSource) + .interaction_response_data(|m| m.content(content).ephemeral(true)) + }) + .await + .unwrap(); + return; + } else { + // Make the Discord API happy :) + component + .create_interaction_response(&ctx.http, |r| { + r.kind(InteractionResponseType::DeferredUpdateMessage) + }) + .await + .unwrap(); } if command == "remove" { @@ -195,7 +240,8 @@ impl EventHandler for Handler { } async fn ready(&self, ctx: Context, ready: Ready) { - ctx.set_activity(Activity::watching("out for embeds ๐Ÿ•ต๏ธ")).await; + ctx.set_activity(Activity::watching("out for embeds ๐Ÿ•ต๏ธ")) + .await; println!("{} is connected!", ready.user.name); } } @@ -220,27 +266,30 @@ async fn main() { "version", ) .to_owned(); + let download_option = CreateSelectMenuOption::new("โฌ Download Media", "download").to_owned(); let twitter_options: Vec = vec![ - default_option.clone(), + download_option.clone(), CreateSelectMenuOption::new("๐Ÿ”„๏ธ Change to: VXTwitter", twitter::VXTWITTER_URL).to_owned(), CreateSelectMenuOption::new("๐Ÿ”„๏ธ Change to: FXTwitter", twitter::FXTWITTER_URL).to_owned(), - CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Image Only: VXTwitter", "direct_vx").to_owned(), - CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Image Only: FXTwitter", "direct_fx").to_owned(), + CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Media Only: VXTwitter", "direct_vx").to_owned(), + CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Media Only: FXTwitter", "direct_fx").to_owned(), CreateSelectMenuOption::new("๐Ÿคจ Show original Twitter URL", twitter::TWITTER_URL) .to_owned(), remove_option.clone(), version_option.clone(), + default_option.clone(), ]; let bluesky_options: Vec = vec![ - default_option, + download_option, CreateSelectMenuOption::new("๐Ÿ”„๏ธ Change to: Psky", bluesky::PSKY_URL).to_owned(), CreateSelectMenuOption::new("๐Ÿ”„๏ธ Change to: FixBluesky", bluesky::FIXBLUESKY_URL).to_owned(), - CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Image Only", "direct_fxbsky").to_owned(), + CreateSelectMenuOption::new("๐Ÿ–ผ๏ธ Media Only", "direct_fxbsky").to_owned(), CreateSelectMenuOption::new("โ˜๏ธ Show original Bluesky URL", bluesky::BLUESKY_URL).to_owned(), - remove_option.clone(), - version_option.clone(), + remove_option, + version_option, + default_option, ]; let regex_pattern = Regex::new(REGEX_URL_EXTRACTOR).unwrap(); diff --git a/thorium/src/twitter.rs b/thorium/src/twitter.rs index 33f0f36..c545fa2 100644 --- a/thorium/src/twitter.rs +++ b/thorium/src/twitter.rs @@ -74,7 +74,6 @@ struct APIFXResponse { pub async fn get_media_from_url(mut url: String) -> String { let source = UrlType::from_string(&url); - println!("Source: {:?}", url); if source == UrlType::Vxtwitter { url = url.replace(VXTWITTER_URL, VXTWITTER_API_URL); @@ -139,8 +138,6 @@ pub async fn get_media_from_url(mut url: String) -> String { url.push(','); } url.pop(); - - println!("URL: {}", url); } url }