diff options
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 60 |
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 +} |
