mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
feat(discord-bot): a public status card with a banner, and log cards with fields
- The status card is the players' card now: a title, "Online" and the admin's message, players and rooms, uptime and memory, an optional banner image and a "Play Burrow" button. The open report count is staff data and is no longer on it. - The admin page's Discord tab takes a banner image URL (http(s), at most 500 characters, blank for none), saved as discordBotStatusBanner and named in the mod log. GET /burrow-status-banner.png serves a banner of our own. - The game activity, moderation, bot audit and security logs are one card shape (logCard): a summary line, a list of fields and when it was logged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
92ebb4d07c
commit
8839f86e6c
5 files changed
+119
-27
No files matched your search
+103
-23
@@ -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 <t:${now}:F> (<t:${now}:R>)_`});
|
||||
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: <t:${Math.floor(Date.now() / 1000 - status.uptimeSeconds)}:R>\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 <t:${startedAt}:R> • ${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 <t:${now}:R>_`},
|
||||
],
|
||||
}],
|
||||
};
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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"]);
|
||||
|
||||
+6
-2
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user