feat: refresh ticket panel and verify embed in place on boot (idempotent)

This commit is contained in:
Loki 2026-10-21 16:07:09 +02:00
parent b7d368e4fe
commit a33ede96cc
Signed by: boba
GPG key ID: 253067914055423B
5 changed files with 101 additions and 76 deletions

View file

@ -4,17 +4,16 @@ pub mod claim;
pub mod create; pub mod create;
pub mod interaction; pub mod interaction;
pub use crate::feature::ticket::service::panel::ensure_ticket_panel;
pub use interaction::on_select; pub use interaction::on_select;
use serenity::all::{ use serenity::all::{
ChannelId, Context, CreateActionRow, CreateEmbed, CreateEmbedFooter, CreateSelectMenu, Context, CreateActionRow, CreateEmbed, CreateEmbedFooter, CreateSelectMenu,
CreateSelectMenuKind, CreateSelectMenuOption, GuildId, CreateSelectMenuKind, CreateSelectMenuOption,
}; };
use tracing::info;
pub const SUPPORT_CATEGORY: u64 = 1547213779224363008; pub const SUPPORT_CATEGORY: u64 = 1547213779224363008;
pub const TICKET_LOGS: u64 = 1509503717127356446; pub const TICKET_LOGS: u64 = 1509503717127356446;
const TICKET_PANEL_CHANNEL: u64 = 1509503682914549861;
/// Canonical topics - single source of truth. /// Canonical topics - single source of truth.
pub struct Topic { pub struct Topic {
@ -73,54 +72,6 @@ pub fn panel_select_row() -> CreateActionRow {
CreateActionRow::SelectMenu(menu) 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<ChannelId> {
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. /// Handles ticket buttons (claim/close) + legacy create buttons.
pub async fn on_button(ctx: Context, interaction: serenity::all::ComponentInteraction) { pub async fn on_button(ctx: Context, interaction: serenity::all::ComponentInteraction) {
match interaction.data.custom_id.as_str() { match interaction.data.custom_id.as_str() {

View file

@ -1,6 +1,7 @@
//! Ticket service //! Ticket service
pub mod count; pub mod count;
pub mod forum; pub mod forum;
pub mod panel;
use serenity::all::{ChannelId, CreateAttachment, CreateMessage, Http, MessageId}; use serenity::all::{ChannelId, CreateAttachment, CreateMessage, Http, MessageId};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use std::sync::OnceLock; use std::sync::OnceLock;

View file

@ -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<ChannelId> {
let channels = guild_id.channels(http).await.ok()?;
channels.iter().find(|(_, c)| c.name.contains("open-ticket")).map(|(id, _)| *id)
}

View file

@ -1,6 +1,7 @@
//! Reaction handler - Verified + language roles (see ARCHITECTURE.md). //! Reaction handler - Verified + language roles (see ARCHITECTURE.md).
//! English docs, no `sleep` without `// SAFETY:`. No dead code. //! 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 serenity::all::{ChannelId, Context, CreateEmbed, CreateMessage, Reaction};
use tracing::{info, warn}; 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`. fn boot_embed() -> CreateEmbed {
/// - Fetches last 20 messages in #verify, looks for bot's `Verification` embed. CreateEmbed::new()
/// - If not found, posts new embed with ✅ and reacts. .title("Verification")
/// - Tracks only that message ID implicitly via channel check (or store in DB later). .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( pub async fn ensure_verify_message(
http: &serenity::http::Http, http: &serenity::http::Http,
channel_id: ChannelId, channel_id: ChannelId,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let msgs = channel_id.messages(&http, serenity::all::GetMessages::new().limit(20)).await?; let msgs = channel_id.messages(&http, serenity::all::GetMessages::new().limit(20)).await?;
let has_verify = msgs.iter().any(|m| { let existing = msgs.iter().find(|m| {
m.author.id.get() == 1527966600173457408 m.author.id.get() == BOT_USER_ID
&& m.embeds.iter().any(|e| e.title.as_deref() == Some("Verification")) && m.embeds.iter().any(|e| e.title.as_deref() == Some("Verification"))
}); });
if has_verify { if let Some(m) = existing {
info!(channel = %channel_id, "verify embed already exists"); 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(()); return Ok(());
} }
let embed = CreateEmbed::new() let create = CreateMessage::new().add_file(Banner::Verify.attachment()).embed(boot_embed());
.title("Verification") let msg = channel_id.send_message(&http, create).await?;
.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?;
msg.react(&http, '✅').await?; msg.react(&http, '✅').await?;
info!(channel = %channel_id, msg = %msg.id, "verify embed created on boot"); info!(channel = %channel_id, msg = %msg.id, "verify embed created on boot");
Ok(()) Ok(())

View file

@ -1,6 +1,9 @@
//! Brand palette and embedded banner images (orange accent, matches loki-code.dev). //! 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). /// Primary accent (the site's orange).
pub const ACCENT: u32 = 0x00FF_793F; pub const ACCENT: u32 = 0x00FF_793F;
@ -75,6 +78,28 @@ impl Banner {
pub fn url(self) -> String { pub fn url(self) -> String {
format!("attachment://{}", self.file()) 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<bool> {
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)] #[cfg(test)]