diff --git a/src/api.rs b/src/api.rs index 57687b0..273e71c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -24,7 +24,10 @@ use tokio::{net::TcpListener, sync::RwLock}; use tower_http::trace::TraceLayer; use tracing::info; -use crate::{config::CONFIG, utils::axum::AxumResult}; +use crate::{ + config::CONFIG, + utils::{self, axum::AxumResult}, +}; #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct ValfiskPresenceData { @@ -158,7 +161,7 @@ async fn route_kofi_webhook( .color(0xffd43b); if let Some(message) = data.message { - embed = embed.field("Message", message[..1024].to_owned(), false); + embed = embed.field("Message", utils::truncate(&message, 1024), false); } channel diff --git a/src/handlers/log.rs b/src/handlers/log.rs index 1ecf421..38470a2 100644 --- a/src/handlers/log.rs +++ b/src/handlers/log.rs @@ -7,7 +7,7 @@ use poise::serenity_prelude::{self as serenity, Mentionable as _}; use eyre::Result; -use crate::{config::CONFIG, storage::log::MessageLog, Data}; +use crate::{config::CONFIG, storage::log::MessageLog, utils, Data}; pub async fn handle_message( ctx: &serenity::Context, @@ -74,10 +74,10 @@ pub async fn edit( "Previous content", prev_content .clone() - .map_or_else(|| "*Unknown*".to_owned(), |s| s[..1024].to_owned()), + .map_or_else(|| "*Unknown*".to_owned(), |s| utils::truncate(&s, 1024)), false, ) - .field("New content", &new_content[..1024], false) + .field("New content", utils::truncate(new_content, 1024), false) .field("Author", format_user(author.as_ref()), false) .color(0xffd43b) .timestamp(timestamp); @@ -153,7 +153,7 @@ pub async fn delete( .field("Channel", channel.mention().to_string(), false) .field( "Content", - content.map_or_else(|| "*Unknown*".to_owned(), |s| s[..1024].to_owned()), + content.map_or_else(|| "*Unknown*".to_owned(), |s| utils::truncate(&s, 1024)), false, ) .field("Author", format_user(author.as_ref()), false) diff --git a/src/handlers/safe_browsing.rs b/src/handlers/safe_browsing.rs index b69e444..f49c9bc 100644 --- a/src/handlers/safe_browsing.rs +++ b/src/handlers/safe_browsing.rs @@ -9,7 +9,7 @@ use std::sync::LazyLock; use eyre::Result; use super::log::format_user; -use crate::{config::CONFIG, Data}; +use crate::{config::CONFIG, utils, Data}; static URL_REGEX: LazyLock = LazyLock::new(|| { Regex::new( @@ -63,7 +63,7 @@ pub async fn handle( .title("Safe Browsing") .field("Channel", message.channel_id.mention().to_string(), false) .field("Author", format_user(Some(&message.author.id)), false) - .field("Content", &content[..1024], false) + .field("Content", utils::truncate(&content, 1024), false) .field( "URLs", matches diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ab06c23..f6196ba 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,3 +8,9 @@ pub mod serenity; mod pluralize; pub use pluralize::Pluralize; + +pub fn truncate(s: &str, new_len: usize) -> String { + let mut s = s.to_owned(); + s.truncate(new_len); + s +}