summaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorseth <[email protected]>2023-11-27 21:37:52 -0500
committerseth <[email protected]>2023-12-01 07:12:49 -0500
commitdb52e639b85d79bed870020aec7a045851ca5ee3 (patch)
treeb5895e3c219260e07d39149fa2f2215f8c9b95aa /src/utils.rs
parent47b69d937ed944aaa41fa80661cdfa9ec72246ca (diff)
feat: add reactboard
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 28cacaa..af079ff 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -2,8 +2,9 @@ use crate::{colors, consts, Context, Error};
use log::*;
use once_cell::sync::Lazy;
-use poise::serenity_prelude::GuildId;
+use poise::serenity_prelude as serenity;
use rand::seq::SliceRandom;
+use serenity::{CreateEmbed, GuildId, Message};
use url::Url;
pub fn parse_snowflake_from_env<T, F: Fn(u64) -> T>(key: &str, f: F) -> Option<T> {
@@ -81,3 +82,60 @@ pub async fn send_url_as_embed(ctx: Context<'_>, url: String) -> Result<(), Erro
Ok(())
}
+
+pub async fn resolve_message_to_embed(ctx: &serenity::Context, msg: &Message) -> CreateEmbed {
+ let truncation_point = floor_char_boundary(&msg.content, 700);
+ let truncated_content = if msg.content.len() <= truncation_point {
+ msg.content.to_string()
+ } else {
+ format!("{}...", &msg.content[..truncation_point])
+ };
+
+ let color = msg
+ .member(ctx)
+ .await
+ .ok()
+ .and_then(|m| m.highest_role_info(&ctx.cache))
+ .and_then(|(role, _)| role.to_role_cached(&ctx.cache))
+ .map(|role| role.colour);
+
+ let attached_image = msg
+ .attachments
+ .iter()
+ .filter(|a| {
+ a.content_type
+ .as_ref()
+ .filter(|ct| ct.contains("image/"))
+ .is_some()
+ })
+ .map(|a| &a.url)
+ .next();
+
+ let attachments_len = msg.attachments.len();
+
+ let mut embed = msg
+ .embeds
+ .first()
+ .map(|embed| CreateEmbed::from(embed.clone()))
+ .unwrap_or_default();
+
+ embed.author(|author| author.name(&msg.author.name).icon_url(&msg.author.face()));
+
+ if let Some(color) = color {
+ embed.color(color);
+ }
+
+ if let Some(attachment) = attached_image {
+ embed.image(attachment);
+ }
+
+ if attachments_len > 1 {
+ embed.footer(|footer| {
+ // yes it will say '1 attachments' no i do not care
+ footer.text(format!("{} attachments", attachments_len))
+ });
+ }
+
+ embed.description(truncated_content);
+ embed
+}