From 3f42f822943ef3a587534cf615738134b972954e Mon Sep 17 00:00:00 2001 From: Evan Chang Date: Fri, 19 Jan 2024 14:45:33 -0500 Subject: [PATCH] Remove original embeds if link replaced --- src/link_embed/mod.rs | 37 ++++++++++++++++++++++++++--- src/link_embed/tweet.rs | 2 +- src/main.rs | 11 +++++---- src/patchbot_forwarder/forwarder.rs | 7 ++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/link_embed/mod.rs b/src/link_embed/mod.rs index d2d77bf..9f9150a 100644 --- a/src/link_embed/mod.rs +++ b/src/link_embed/mod.rs @@ -4,7 +4,7 @@ mod tweet; use std::sync::Arc; use itertools::Itertools; -use poise::serenity_prelude::{Http, Message}; +use poise::serenity_prelude::{Http, Message, SerenityError}; #[derive(PartialEq, Eq, PartialOrd, Ord)] struct ReplacedLink { @@ -18,7 +18,24 @@ impl ReplacedLink { } } -pub async fn reply_link_embeds(http: Arc, message: Message) { +#[derive(Debug)] +pub struct LinkEmbedError { + context: String, + inner: SerenityError, +} + +impl std::fmt::Display for LinkEmbedError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "context: {}, {}", self.context, self.inner) + } +} + +impl std::error::Error for LinkEmbedError {} + +pub async fn reply_link_embeds( + http: Arc, + mut message: Message, +) -> Result<(), LinkEmbedError> { let tweet_links = tweet::tweet_links(&message.content); let reddit_links = reddit::reddit_links(&message.content).await; let reply_content = tweet_links @@ -32,6 +49,20 @@ pub async fn reply_link_embeds(http: Arc, message: Message) { "Replying with updated link in {}", message.channel_id.as_u64() ); - message.reply(&http, reply_content).await.unwrap(); + message + .reply(&http, reply_content) + .await + .map_err(|e| LinkEmbedError { + context: "Send message".into(), + inner: e, + })?; + message + .suppress_embeds(http) + .await + .map_err(|e| LinkEmbedError { + context: "Suppress original embeds".into(), + inner: e, + })?; } + Ok(()) } diff --git a/src/link_embed/tweet.rs b/src/link_embed/tweet.rs index f35c8c4..fadb8a3 100644 --- a/src/link_embed/tweet.rs +++ b/src/link_embed/tweet.rs @@ -13,7 +13,7 @@ pub fn tweet_links(text: &str) -> Vec { .map(|m| (m.start(), m.as_str())) .map(|(start, tweet)| ReplacedLink { start, - link: format!("https://fxtwitter.com/{}", tweet).into(), + link: format!("https://vxtwitter.com/{}", tweet).into(), }) .collect() } diff --git a/src/main.rs b/src/main.rs index 4ea650a..8b6b08f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ mod config; +mod link_embed; mod message; mod music; mod package_update; mod patchbot_forwarder; -mod link_embed; use std::collections::HashMap; use std::sync::{Arc, Mutex}; @@ -121,11 +121,14 @@ async fn on_event( // Forward patchbot messages let db_uri = data.db_uri.as_str(); - let _ = forward(db_uri, http.clone(), message.clone()).await; + if let Err(e) = forward(db_uri, http.clone(), message.clone()).await { + eprintln!("Error forwarding patchbot message: {e}"); + } // Add embed preview for Tweets and Reddit links - let _ = reply_link_embeds(http.clone(), message.clone()) - .await; + if let Err(e) = reply_link_embeds(http.clone(), message.clone()).await { + eprintln!("Error sending link embed: {e}"); + } } _ => {} } diff --git a/src/patchbot_forwarder/forwarder.rs b/src/patchbot_forwarder/forwarder.rs index a5e1b0d..ace2440 100644 --- a/src/patchbot_forwarder/forwarder.rs +++ b/src/patchbot_forwarder/forwarder.rs @@ -25,8 +25,11 @@ pub async fn forward( } else { false } - }) - .ok_or(PatchbotForwardError::InvalidEmbeds)?; + }); + if embed.is_none() { + return Ok(()); + }; + let embed = embed.unwrap(); let embed_author = embed .author .clone()