Skip to content

Commit

Permalink
Remove original embeds if link replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
evanc577 committed Jan 19, 2024
1 parent 7cf4762 commit 3f42f82
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
37 changes: 34 additions & 3 deletions src/link_embed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,7 +18,24 @@ impl ReplacedLink {
}
}

pub async fn reply_link_embeds(http: Arc<Http>, 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<Http>,
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
Expand All @@ -32,6 +49,20 @@ pub async fn reply_link_embeds(http: Arc<Http>, 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(())
}
2 changes: 1 addition & 1 deletion src/link_embed/tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn tweet_links(text: &str) -> Vec<ReplacedLink> {
.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()
}
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -121,11 +121,14 @@ async fn on_event<U, E>(

// 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}");
}
}
_ => {}
}
Expand Down
7 changes: 5 additions & 2 deletions src/patchbot_forwarder/forwarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3f42f82

Please sign in to comment.