Files
payphone/src/groups/moderation/embeds.rs
T
2026-03-07 19:27:40 -03:00

101 lines
3.5 KiB
Rust

use time::UtcDateTime;
use twilight_model::channel::message::Embed;
use twilight_util::builder::embed::{
EmbedAuthorBuilder, EmbedBuilder, EmbedFieldBuilder, ImageSource,
};
use crate::constants::{COLOR_RED, COLOR_YELLOW};
use crate::database::models::{UserIncident, UserIncidentType};
use crate::utils::ellipse;
fn format_timestamp(timestamp: UtcDateTime) -> String {
format!(
"<t:{}> (<t:{}:R>)",
timestamp.unix_timestamp(),
timestamp.unix_timestamp()
)
}
pub fn incident_embed(incident: &UserIncident) -> Embed {
if let Some(message) = &incident.message {
let mut builder = EmbedBuilder::new()
.color(match incident.r#type {
UserIncidentType::Ban => COLOR_RED,
UserIncidentType::Warning => COLOR_YELLOW,
})
.author(
EmbedAuthorBuilder::new(&message.author_name)
.icon_url(ImageSource::url(&message.author_avatar_url).unwrap()),
)
.title("Message Incident")
.thumbnail(ImageSource::url(message.author_avatar_url.clone()).unwrap())
.field(EmbedFieldBuilder::new("Message", ellipse(message.content.clone(), 100)).build())
.field(EmbedFieldBuilder::new("Author Name", message.author_name.clone()).build())
.field(
EmbedFieldBuilder::new(
"Author Avatar",
format!("[Click to open avatar](<{}>)", &message.author_avatar_url),
)
.build(),
)
.field(
EmbedFieldBuilder::new("Created At", format_timestamp(incident.created_at)).build(),
)
.field(EmbedFieldBuilder::new(
"Created By",
incident
.created_by_id
.map(|id| format!("<@{id}>"))
.unwrap_or("Automatic Moderator".into()),
));
if incident.r#type == UserIncidentType::Ban {
builder = builder.field(
EmbedFieldBuilder::new(
"Expires At",
incident
.expires_at
.map(format_timestamp)
.unwrap_or("*Never*".into()),
)
.build(),
);
}
builder.build()
} else {
let mut builder = EmbedBuilder::new()
.color(match incident.r#type {
UserIncidentType::Ban => COLOR_RED,
UserIncidentType::Warning => COLOR_YELLOW,
})
.title("Manual Incident")
.field(EmbedFieldBuilder::new("Reason", incident.reason.clone()))
.field(
EmbedFieldBuilder::new("Created At", format_timestamp(incident.created_at)).build(),
)
.field(EmbedFieldBuilder::new(
"Created By",
incident
.created_by_id
.map(|id| format!("<@{id}>"))
.unwrap_or("Automatic Moderator".into()),
));
if incident.r#type == UserIncidentType::Ban {
builder = builder.field(
EmbedFieldBuilder::new(
"Expires At",
incident
.expires_at
.map(format_timestamp)
.unwrap_or("*Never*".into()),
)
.build(),
);
}
builder.build()
}
}