From a33ede96ccbf150028d99f015ef391cb45726cb9 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Wed, 21 Oct 2026 16:07:09 +0200 Subject: [PATCH] feat: refresh ticket panel and verify embed in place on boot (idempotent) --- src/feature/ticket/handler/mod.rs | 55 ++--------------------------- src/feature/ticket/service/mod.rs | 1 + src/feature/ticket/service/panel.rs | 45 +++++++++++++++++++++++ src/feature/verify/handler.rs | 49 +++++++++++++------------ src/util/brand.rs | 27 +++++++++++++- 5 files changed, 101 insertions(+), 76 deletions(-) create mode 100644 src/feature/ticket/service/panel.rs diff --git a/src/feature/ticket/handler/mod.rs b/src/feature/ticket/handler/mod.rs index c20ea82..a05c0a7 100644 --- a/src/feature/ticket/handler/mod.rs +++ b/src/feature/ticket/handler/mod.rs @@ -4,17 +4,16 @@ pub mod claim; pub mod create; pub mod interaction; +pub use crate::feature::ticket::service::panel::ensure_ticket_panel; pub use interaction::on_select; use serenity::all::{ - ChannelId, Context, CreateActionRow, CreateEmbed, CreateEmbedFooter, CreateSelectMenu, - CreateSelectMenuKind, CreateSelectMenuOption, GuildId, + Context, CreateActionRow, CreateEmbed, CreateEmbedFooter, CreateSelectMenu, + CreateSelectMenuKind, CreateSelectMenuOption, }; -use tracing::info; pub const SUPPORT_CATEGORY: u64 = 1547213779224363008; pub const TICKET_LOGS: u64 = 1509503717127356446; -const TICKET_PANEL_CHANNEL: u64 = 1509503682914549861; /// Canonical topics - single source of truth. pub struct Topic { @@ -73,54 +72,6 @@ pub fn panel_select_row() -> CreateActionRow { CreateActionRow::SelectMenu(menu) } -/// Ensures panel exists in dedicated channel. Idempotent. -pub async fn ensure_ticket_panel( - http: &serenity::http::Http, - guild_id: GuildId, -) -> anyhow::Result<()> { - let channel_id = - resolve_panel_channel(http, guild_id).await.unwrap_or(ChannelId::new(TICKET_PANEL_CHANNEL)); - let msgs = channel_id.messages(http, serenity::all::GetMessages::new().limit(20)).await?; - let has_panel = msgs.iter().any(|m| { - m.author.id.get() == 1527966600173457408 - && m.components.iter().any(|row| { - row.components.iter().any(|c| match c { - serenity::all::ActionRowComponent::SelectMenu(sm) => { - sm.custom_id.as_deref() == Some("ticket_topic") - } - _ => false, - }) - }) - }); - if has_panel { - info!(channel=%channel_id, "ticket panel already exists"); - return Ok(()); - } - channel_id - .send_message( - http, - serenity::all::CreateMessage::new() - .add_file(crate::util::brand::Banner::Ticket.attachment()) - .embed(panel_embed()) - .components(vec![panel_select_row()]), - ) - .await?; - info!(channel=%channel_id, "ticket panel created on boot"); - Ok(()) -} - -async fn resolve_panel_channel( - http: &serenity::http::Http, - guild_id: GuildId, -) -> Option { - if let Ok(channels) = guild_id.channels(http).await { - if let Some((id, _)) = channels.iter().find(|(_, c)| c.name.contains("open-ticket")) { - return Some(*id); - } - } - None -} - /// Handles ticket buttons (claim/close) + legacy create buttons. pub async fn on_button(ctx: Context, interaction: serenity::all::ComponentInteraction) { match interaction.data.custom_id.as_str() { diff --git a/src/feature/ticket/service/mod.rs b/src/feature/ticket/service/mod.rs index 68c0e6a..27aaf3d 100644 --- a/src/feature/ticket/service/mod.rs +++ b/src/feature/ticket/service/mod.rs @@ -1,6 +1,7 @@ //! Ticket service pub mod count; pub mod forum; +pub mod panel; use serenity::all::{ChannelId, CreateAttachment, CreateMessage, Http, MessageId}; use sqlx::SqlitePool; use std::sync::OnceLock; diff --git a/src/feature/ticket/service/panel.rs b/src/feature/ticket/service/panel.rs new file mode 100644 index 0000000..d74eb11 --- /dev/null +++ b/src/feature/ticket/service/panel.rs @@ -0,0 +1,45 @@ +//! Ticket panel lifecycle on boot: find the bot's panel, refresh it in place or post it. + +use crate::feature::ticket::handler::{panel_embed, panel_select_row}; +use crate::util::brand::{BOT_USER_ID, Banner}; +use serenity::all::{ + ActionRowComponent, ChannelId, CreateMessage, EditMessage, GetMessages, GuildId, Http, Message, +}; +use tracing::info; + +const TICKET_PANEL_CHANNEL: u64 = 1509503682914549861; + +fn is_panel(m: &Message) -> bool { + m.author.id.get() == BOT_USER_ID + && m.components.iter().flat_map(|row| &row.components).any(|c| { + matches!(c, ActionRowComponent::SelectMenu(sm) if sm.custom_id.as_deref() == Some("ticket_topic")) + }) +} + +/// Ensures exactly one panel exists; an outdated one is re-rendered in place. Idempotent. +pub async fn ensure_ticket_panel(http: &Http, guild_id: GuildId) -> anyhow::Result<()> { + let channel_id = + resolve_panel_channel(http, guild_id).await.unwrap_or(ChannelId::new(TICKET_PANEL_CHANNEL)); + let msgs = channel_id.messages(http, GetMessages::new().limit(20)).await?; + if let Some(m) = msgs.iter().find(|m| is_panel(m)) { + let edit = EditMessage::new().embed(panel_embed()).components(vec![panel_select_row()]); + if Banner::Ticket.refresh(http, m, edit).await? { + info!(channel = %channel_id, msg = %m.id, "ticket panel updated in place"); + } else { + info!(channel = %channel_id, msg = %m.id, "ticket panel already up to date"); + } + return Ok(()); + } + let msg = CreateMessage::new() + .add_file(Banner::Ticket.attachment()) + .embed(panel_embed()) + .components(vec![panel_select_row()]); + channel_id.send_message(http, msg).await?; + info!(channel = %channel_id, "ticket panel created on boot"); + Ok(()) +} + +async fn resolve_panel_channel(http: &Http, guild_id: GuildId) -> Option { + let channels = guild_id.channels(http).await.ok()?; + channels.iter().find(|(_, c)| c.name.contains("open-ticket")).map(|(id, _)| *id) +} diff --git a/src/feature/verify/handler.rs b/src/feature/verify/handler.rs index e135f1a..8f53442 100644 --- a/src/feature/verify/handler.rs +++ b/src/feature/verify/handler.rs @@ -1,6 +1,7 @@ //! Reaction handler - Verified + language roles (see ARCHITECTURE.md). //! English docs, no `sleep` without `// SAFETY:`. No dead code. +use crate::util::brand::{BOT_USER_ID, Banner}; use serenity::all::{ChannelId, Context, CreateEmbed, CreateMessage, Reaction}; use tracing::{info, warn}; @@ -87,38 +88,40 @@ pub async fn on_reaction_add(ctx: Context, reaction: Reaction) { } } -/// Ensures verification embed exists once on boot. Call from `ready`. -/// - Fetches last 20 messages in #verify, looks for bot's `Verification` embed. -/// - If not found, posts new embed with ✅ and reacts. -/// - Tracks only that message ID implicitly via channel check (or store in DB later). +fn boot_embed() -> CreateEmbed { + CreateEmbed::new() + .title("Verification") + .description( + "Click ✅ to get access to the server.\n\nChat is locked here - reaction only.", + ) + .image(Banner::Verify.url()) + .color(crate::util::brand::ACCENT) +} + +/// Ensures one verification embed exists on boot. Call from `ready`. +/// - Looks for the bot's `Verification` embed in the last 20 messages of #verify. +/// - Found but outdated: re-rendered in place (reactions stay); up to date: left alone. +/// - Not found: posts a new embed and reacts with ✅. pub async fn ensure_verify_message( http: &serenity::http::Http, channel_id: ChannelId, ) -> anyhow::Result<()> { let msgs = channel_id.messages(&http, serenity::all::GetMessages::new().limit(20)).await?; - let has_verify = msgs.iter().any(|m| { - m.author.id.get() == 1527966600173457408 + let existing = msgs.iter().find(|m| { + m.author.id.get() == BOT_USER_ID && m.embeds.iter().any(|e| e.title.as_deref() == Some("Verification")) }); - if has_verify { - info!(channel = %channel_id, "verify embed already exists"); + if let Some(m) = existing { + let edit = serenity::all::EditMessage::new().embed(boot_embed()); + if Banner::Verify.refresh(http, m, edit).await? { + info!(channel = %channel_id, msg = %m.id, "verify embed updated in place"); + } else { + info!(channel = %channel_id, msg = %m.id, "verify embed already up to date"); + } return Ok(()); } - let embed = CreateEmbed::new() - .title("Verification") - .description( - "Click ✅ to get access to the server.\n\nChat is locked here - reaction only.", - ) - .image(crate::util::brand::Banner::Verify.url()) - .color(crate::util::brand::ACCENT); - let msg = channel_id - .send_message( - &http, - CreateMessage::new() - .add_file(crate::util::brand::Banner::Verify.attachment()) - .embed(embed), - ) - .await?; + let create = CreateMessage::new().add_file(Banner::Verify.attachment()).embed(boot_embed()); + let msg = channel_id.send_message(&http, create).await?; msg.react(&http, '✅').await?; info!(channel = %channel_id, msg = %msg.id, "verify embed created on boot"); Ok(()) diff --git a/src/util/brand.rs b/src/util/brand.rs index bfcfa8b..dac1bc9 100644 --- a/src/util/brand.rs +++ b/src/util/brand.rs @@ -1,6 +1,9 @@ //! Brand palette and embedded banner images (orange accent, matches loki-code.dev). -use serenity::all::CreateAttachment; +use serenity::all::{CreateAttachment, EditAttachments, EditMessage, Http, Message}; + +/// The bot's own user id (author check for boot-managed messages). +pub const BOT_USER_ID: u64 = 1527966600173457408; /// Primary accent (the site's orange). pub const ACCENT: u32 = 0x00FF_793F; @@ -75,6 +78,28 @@ impl Banner { pub fn url(self) -> String { format!("attachment://{}", self.file()) } + + /// True when `msg` already carries this banner inside an accent-coloured embed. + pub fn is_on(self, msg: &Message) -> bool { + msg.attachments.iter().any(|a| a.filename == self.file()) + && msg.embeds.iter().any(|e| e.image.is_some() && e.colour.map(|c| c.0) == Some(ACCENT)) + } + + /// Re-renders an existing message in the current design, swapping its attachments for + /// this banner. Returns `false` (no API call) when the message is already up to date. + pub async fn refresh( + self, + http: &Http, + msg: &Message, + edit: EditMessage, + ) -> anyhow::Result { + if self.is_on(msg) { + return Ok(false); + } + let edit = edit.attachments(EditAttachments::new().add(self.attachment())); + msg.channel_id.edit_message(http, msg.id, edit).await?; + Ok(true) + } } #[cfg(test)]