diff --git a/lib/discordbot.js b/lib/discordbot.js index 6265eed..d82275e 100644 --- a/lib/discordbot.js +++ b/lib/discordbot.js @@ -42,6 +42,7 @@ const EPHEMERAL = 1 << 6; const STATUS_MESSAGE_SETTING = "discordBotStatusMessage"; const STATUS_TITLE_SETTING = "discordBotStatusTitle"; const STATUS_NOTE_SETTING = "discordBotStatusNote"; +const STATUS_BANNER_SETTING = "discordBotStatusBanner"; const STATUS_EVERY_MS = 5 * 60000; const REPORT_BUTTON_PREFIX = "burrow:report:"; const MAX_GITHUB_WEBHOOK_BYTES = 512 * 1024; @@ -71,18 +72,36 @@ let lastRaidAlertAt = 0; const safe = (value, max = 1000) => String(value === undefined || value === null ? "" : value) .replace(/@/g, "@\u200b").replace(/`/g, "'").replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, max); +// Discord fetches the banner itself, so only absolute http(s) URLs work. +// Blank means the card has no banner; null means the URL was rejected. +const bannerUrl = value => { + const text = String(value || "").trim(); + if (!text) return ""; + try { + const url = new URL(text); + return /^https?:$/.test(url.protocol) && text.length <= 500 ? url.toString() : null; + } + catch { + return null; + } +}; + const statusSettings = () => ({ title: safe(accounts.setting(STATUS_TITLE_SETTING) || "Burrow status", 80) || "Burrow status", note: safe(accounts.setting(STATUS_NOTE_SETTING) || "", 500), + banner: bannerUrl(accounts.setting(STATUS_BANNER_SETTING)) || "", }); -const setStatusSettings = ({title, note}) => { +const setStatusSettings = ({title, note, banner}) => { const next = { title: safe(title || "Burrow status", 80) || "Burrow status", note: safe(note || "", 500), + banner: bannerUrl(banner), }; + if (next.banner === null) return {error: "Banner must be an http(s) image URL of 500 characters or fewer, or blank for none."}; accounts.setSetting(STATUS_TITLE_SETTING, next.title); accounts.setSetting(STATUS_NOTE_SETTING, next.note); + accounts.setSetting(STATUS_BANNER_SETTING, next.banner); return next; }; @@ -223,6 +242,33 @@ const success = (title, sections, ephemeral = true) => card({title, sections, to const warning = (message, ephemeral = true) => card({title: "Burrow", sections: [message], tone: 0xfee75c, ephemeral}); const failure = (message, ephemeral = true) => card({title: "Burrow", sections: [message], tone: 0xed4245, ephemeral}); +// Staff audit streams use the same compact Components V2 pattern as SRP-Bot: +// a brief summary, structured fields, and a durable log timestamp. +const logCard = ({title, description = "", fields = [], tone = 0x5865f2}) => { + const shown = fields.filter(field => field && field.name && field.value !== undefined && field.value !== null).slice(0, 12); + const components = [{type: 10, content: [ + `## ${safe(title, 160)}`, + description ? safe(description, 900) : "", + ].filter(Boolean).join("\n")}]; + + if (shown.length) { + components.push({type: 14, divider: true, spacing: 1}); + components.push({type: 10, content: shown + .map(field => `• **${safe(field.name, 80)}:** ${safe(field.value, 500)}`) + .join("\n") + .slice(0, 1800)}); + } + + const now = Math.floor(Date.now() / 1000); + components.push({type: 14, divider: false, spacing: 1}); + components.push({type: 10, content: `_Logged ()_`}); + return { + flags: COMPONENTS_V2, + allowedMentions: {parse: []}, + components: [{type: 17, accent_color: tone, components}], + }; +}; + const commandDefinitions = () => [ new SlashCommandBuilder().setName("burrow").setDescription("View Burrow server and player information") .addSubcommand(command => command.setName("status").setDescription("Show the live Burrow server status")) @@ -302,12 +348,33 @@ const playerCard = player => success(`Player: ${player.username}`, [ const statusCard = status => { const settings = statusSettings(); - return card({title: settings.title, tone: 0x5865f2, sections: [ - settings.note ? `**Status message**\n${settings.note}` : "", - `**Online**\n${status.players} players in ${status.rooms} room${status.rooms === 1 ? "" : "s"}`, - `**Moderation queue**\n${status.reports} open report${status.reports === 1 ? "" : "s"}`, - `**Server**\nUptime: \nMemory: ${status.memoryMb} MB`, - ]}); + const now = Math.floor(Date.now() / 1000); + const startedAt = now - status.uptimeSeconds; + const rooms = `${status.rooms} room${status.rooms === 1 ? "" : "s"}`; + const playUrl = new URL("gameClient/game/index.html", config.siteUrl).toString(); + + // This is intentionally public-only: moderation reports and queue depth are + // staff data and do not belong in a community-facing server-status post. + return { + flags: COMPONENTS_V2, + allowedMentions: {parse: []}, + components: [{ + type: 17, + accent_color: 0x7c5ce7, + components: [ + {type: 10, content: `## ${safe(settings.title, 80)}`}, + {type: 14, divider: true, spacing: 1}, + {type: 10, content: ["### STATUS", "🟢 **Online**", settings.note ? `\n${safe(settings.note, 500)}` : ""].filter(Boolean).join("\n")}, + {type: 14, divider: true, spacing: 1}, + {type: 10, content: `### PLAYERS\n\`${status.players} online\` • \`${rooms}\``}, + {type: 14, divider: true, spacing: 1}, + {type: 10, content: `### UPTIME\nStarted • ${status.memoryMb} MB memory`}, + ...settings.banner ? [{type: 12, items: [{media: {url: settings.banner}}]}] : [], + {type: 1, components: [{type: 2, style: 5, label: "Play Burrow", url: playUrl}]}, + {type: 10, content: `_Last updated _`}, + ], + }], + }; }; const staffCard = staff => success("Burrow staff", staff.length @@ -555,41 +622,54 @@ const notifyReport = async report => { const notifyGameActivity = async entry => { const minutes = Math.max(1, Math.ceil((entry.playedSeconds || 0) / 60)); - const played = entry.type === "logout" ? `\nSession: ${minutes} minute${minutes === 1 ? "" : "s"}` : ""; - await postCard(config.discord.gameLogsChannelId, card({ - title: "In-game player activity", - tone: entry.type === "login" ? 0x57f287 : 0x5865f2, - sections: [`**${safe(entry.username, 40)}** ${entry.type === "login" ? "signed in" : "signed out"}.${played}`], + const signedIn = entry.type === "login"; + await postCard(config.discord.gameLogsChannelId, logCard({ + title: "In-game activity", + description: signedIn ? "A player signed in." : "A player signed out.", + tone: signedIn ? 0x57f287 : 0x5865f2, + fields: [ + {name: "Player", value: entry.username}, + {name: "Event", value: signedIn ? "Signed in" : "Signed out"}, + ...(!signedIn ? [{name: "Session", value: `${minutes} minute${minutes === 1 ? "" : "s"}`}] : []), + ], }), "game activity log"); }; const notifyModeration = async entry => { - await postCard(config.discord.moderationLogsChannelId, card({ + await postCard(config.discord.moderationLogsChannelId, logCard({ title: "Moderation audit", + description: "A Burrow staff action was recorded.", tone: 0xfee75c, - sections: [ - `**${safe(entry.by, 50)}** — ${safe(entry.action, 120)}`, - entry.target ? `Player: **${safe(entry.target, 50)}**` : "", - entry.detail ? `Details: ${safe(entry.detail, 500)}` : "", + fields: [ + {name: "Staff", value: entry.by}, + {name: "Action", value: entry.action}, + ...(entry.target ? [{name: "Player", value: entry.target}] : []), + ...(entry.detail ? [{name: "Details", value: entry.detail}] : []), ], }), "moderation log"); }; const notifyBot = async (interaction, action) => { const actor = modpanel.discordBot.actor(interaction.user.id); - await postCard(config.discord.botLogsChannelId, card({ + await postCard(config.discord.botLogsChannelId, logCard({ title: "Discord bot audit", + description: "A Discord command or control was used.", tone: actor ? 0x5865f2 : 0xed4245, - sections: [ - `Discord user: **${safe(interaction.user.username, 80)}** (\`${safe(interaction.user.id, 30)}\`)`, - `Action: ${safe(action, 160)}`, - actor ? `Linked staff account: **${safe(actor.username, 40)}**` : "No active linked Burrow staff account.", + fields: [ + {name: "Discord user", value: `${interaction.user.username} (${interaction.user.id})`}, + {name: "Action", value: action}, + {name: "Linked staff account", value: actor ? actor.username : "No active linked Burrow staff account."}, ], }), "Discord bot audit log"); }; const securityIncident = async (title, sections, detail = "") => { - await postCard(config.discord.moderationLogsChannelId, card({title, tone: 0xed4245, sections}), "Discord security log"); + await postCard(config.discord.moderationLogsChannelId, logCard({ + title, + description: "Discord security activity detected.", + tone: 0xed4245, + fields: [{name: "Details", value: sections.filter(Boolean).join("\n")}], + }), "Discord security log"); modlog.record("discord security", title.toLowerCase(), "", detail); }; diff --git a/lib/http.js b/lib/http.js index dafb893..1fea8eb 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1008,6 +1008,10 @@ const handle = async (req, res) => { res.setHeader("Cache-Control", "public, max-age=86400"); send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "ban-icon.png")), MIME[".png"]); return; + case "GET /burrow-status-banner.png": // public banner used by the Discord status card + res.setHeader("Cache-Control", "public, max-age=86400"); + send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "burrow-status-banner.png")), MIME[".png"]); + return; case "GET /bio-icon.png": // the button of the card's Bio tab res.setHeader("Cache-Control", "public, max-age=86400"); send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "bio-icon.png")), MIME[".png"]); diff --git a/lib/panel.js b/lib/panel.js index 6f3d346..0d2fd8d 100644 --- a/lib/panel.js +++ b/lib/panel.js @@ -814,8 +814,12 @@ const handle = async (req, res, url, from, web) => { return; case "POST /admin/api/discord/status": { - const statusCard = discordBot.setStatusSettings({title: body.title, note: body.note}); - modlog.record("admin", "Discord public status card changed", "", `title: ${statusCard.title}`); + const statusCard = discordBot.setStatusSettings({title: body.title, note: body.note, banner: body.banner}); + if (statusCard.error) { + reply(400, {error: statusCard.error}); + return; + } + modlog.record("admin", "Discord public status card changed", "", `title: ${statusCard.title}, banner: ${statusCard.banner || "none"}`); reply(200, {ok: "Status-card text saved. Use Publish now to update Discord.", statusCard}); return; } diff --git a/public/admin.html b/public/admin.html index bd8be34..a2fcefb 100644 --- a/public/admin.html +++ b/public/admin.html @@ -490,6 +490,8 @@ + +
@@ -1328,10 +1330,11 @@ ? "Credentials and webhook secrets are intentionally never shown in this dashboard." : "Add the missing environment variables to the service that starts Burrow; dashboard settings cannot store bot secrets."; $("discord-bot-status").classList.toggle("bad", !configured || !data.connected); - const editingStatusCard = ["discord-status-title", "discord-status-note"].includes(document.activeElement && document.activeElement.id); + const editingStatusCard = ["discord-status-title", "discord-status-note", "discord-status-banner"].includes(document.activeElement && document.activeElement.id); if (!editingStatusCard) { $("discord-status-title").value = data.statusCard && data.statusCard.title || "Burrow status"; $("discord-status-note").value = data.statusCard && data.statusCard.note || ""; + $("discord-status-banner").value = data.statusCard && data.statusCard.banner || ""; } const editingRouting = document.activeElement && (document.activeElement.dataset.discordChannel || document.activeElement.id === "discord-github-repository"); if (!editingRouting) { @@ -1444,12 +1447,13 @@ event.preventDefault(); const button = event.submitter; if (button) button.disabled = true; - const result = await api("discord/status", {title: $("discord-status-title").value, note: $("discord-status-note").value}); + const result = await api("discord/status", {title: $("discord-status-title").value, note: $("discord-status-note").value, banner: $("discord-status-banner").value}); if (button) button.disabled = false; setStatus($("discord-status-card-result"), said(result), !result.ok); if (result.ok && result.data.statusCard) { $("discord-status-title").value = result.data.statusCard.title; $("discord-status-note").value = result.data.statusCard.note; + $("discord-status-banner").value = result.data.statusCard.banner; } }); diff --git a/public/burrow-status-banner.png b/public/burrow-status-banner.png new file mode 100644 index 0000000..b472a78 Binary files /dev/null and b/public/burrow-status-banner.png differ