mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
feat: add unified Discord bot and dashboard
This commit is contained in:
1 parent
113bf9342e
commit
b11ae1dfa8
14 files changed
+1708
-14
No files matched your search
@@ -0,0 +1,865 @@
|
||||
"use strict";
|
||||
|
||||
// Burrow's Discord control surface. It is intentionally a thin client over
|
||||
// lib/modpanel.js: Discord proves who clicked a command, and the panel then
|
||||
// proves that their linked Burrow account is still active staff and applies
|
||||
// its usual permission and rank rules. There is no Discord-only staff list.
|
||||
|
||||
const {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
ChannelType,
|
||||
Client,
|
||||
GatewayIntentBits,
|
||||
MessageFlags,
|
||||
PermissionFlagsBits,
|
||||
SlashCommandBuilder,
|
||||
} = require("discord.js");
|
||||
const crypto = require("crypto");
|
||||
const accounts = require("./accounts.js");
|
||||
const config = require("../config.js");
|
||||
const modpanel = require("./modpanel.js");
|
||||
const modlog = require("./modlog.js");
|
||||
const reports = require("./reports.js");
|
||||
const world = require("./world.js");
|
||||
const {logText} = require("./logtext.js");
|
||||
|
||||
const COMPONENTS_V2 = MessageFlags.IsComponentsV2;
|
||||
const EPHEMERAL = MessageFlags.Ephemeral;
|
||||
const STATUS_MESSAGE_SETTING = "discordBotStatusMessage";
|
||||
const STATUS_TITLE_SETTING = "discordBotStatusTitle";
|
||||
const STATUS_NOTE_SETTING = "discordBotStatusNote";
|
||||
const STATUS_EVERY_MS = 5 * 60000;
|
||||
const REPORT_BUTTON_PREFIX = "burrow:report:";
|
||||
const MAX_GITHUB_WEBHOOK_BYTES = 512 * 1024;
|
||||
const CONFIG_SETTING_PREFIX = "discordBotConfig:";
|
||||
const SECURITY_SETTING = "discordBotSecurity";
|
||||
const COMMUNITY_SETTING = "discordBotCommunity";
|
||||
const CHANNELS = [
|
||||
["reportsChannelId", "Reports"],
|
||||
["statusChannelId", "Public status"],
|
||||
["gameLogsChannelId", "In-game activity"],
|
||||
["moderationLogsChannelId", "Moderation audit"],
|
||||
["botLogsChannelId", "Discord bot audit"],
|
||||
["updatesChannelId", "GitHub updates"],
|
||||
];
|
||||
const MANAGED_CONFIG = [...CHANNELS.map(([key]) => key), "githubRepository"];
|
||||
let loadedDashboardConfig = false;
|
||||
let securityConfigLoaded = false;
|
||||
let securityConfig = null;
|
||||
let communityConfig = null;
|
||||
let communityConfigLoaded = false;
|
||||
const recentMessages = new Map();
|
||||
const recentJoins = [];
|
||||
let lastRaidAlertAt = 0;
|
||||
|
||||
// Text Displays accept markdown. Keep player-provided strings readable while
|
||||
// ensuring a report cannot ping a Discord role or user.
|
||||
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);
|
||||
|
||||
const statusSettings = () => ({
|
||||
title: safe(accounts.setting(STATUS_TITLE_SETTING) || "Burrow status", 80) || "Burrow status",
|
||||
note: safe(accounts.setting(STATUS_NOTE_SETTING) || "", 500),
|
||||
});
|
||||
|
||||
const setStatusSettings = ({title, note}) => {
|
||||
const next = {
|
||||
title: safe(title || "Burrow status", 80) || "Burrow status",
|
||||
note: safe(note || "", 500),
|
||||
};
|
||||
accounts.setSetting(STATUS_TITLE_SETTING, next.title);
|
||||
accounts.setSetting(STATUS_NOTE_SETTING, next.note);
|
||||
return next;
|
||||
};
|
||||
|
||||
const securitySettings = () => {
|
||||
if (securityConfigLoaded) return {...securityConfig};
|
||||
const defaults = {enabled: false, blockInvites: true, blockedWords: [], spamLimit: 6, raidJoinLimit: 12};
|
||||
try {
|
||||
const saved = JSON.parse(accounts.setting(SECURITY_SETTING) || "{}");
|
||||
const words = Array.isArray(saved.blockedWords) ? saved.blockedWords.map(word => safe(word, 60).toLowerCase()).filter(Boolean).slice(0, 50) : [];
|
||||
securityConfig = {
|
||||
enabled: saved.enabled === true,
|
||||
blockInvites: saved.blockInvites !== false,
|
||||
blockedWords: [...new Set(words)],
|
||||
spamLimit: Number.isInteger(saved.spamLimit) && saved.spamLimit >= 2 && saved.spamLimit <= 20 ? saved.spamLimit : defaults.spamLimit,
|
||||
raidJoinLimit: Number.isInteger(saved.raidJoinLimit) && saved.raidJoinLimit >= 3 && saved.raidJoinLimit <= 100 ? saved.raidJoinLimit : defaults.raidJoinLimit,
|
||||
};
|
||||
}
|
||||
catch {
|
||||
securityConfig = defaults;
|
||||
}
|
||||
securityConfigLoaded = true;
|
||||
return {...securityConfig};
|
||||
};
|
||||
|
||||
const updateSecuritySettings = values => {
|
||||
const words = String(values.blockedWords || "").split(/\r?\n|,/)
|
||||
.map(word => safe(word, 60).toLowerCase()).filter(Boolean).slice(0, 50);
|
||||
const spamLimit = Number(values.spamLimit);
|
||||
const raidJoinLimit = Number(values.raidJoinLimit);
|
||||
if (!Number.isInteger(spamLimit) || spamLimit < 2 || spamLimit > 20 || !Number.isInteger(raidJoinLimit) || raidJoinLimit < 3 || raidJoinLimit > 100) {
|
||||
return {error: "Spam limit must be 2 to 20 and raid join limit must be 3 to 100."};
|
||||
}
|
||||
securityConfig = {enabled: values.enabled === true, blockInvites: values.blockInvites === true,
|
||||
blockedWords: [...new Set(words)], spamLimit, raidJoinLimit};
|
||||
securityConfigLoaded = true;
|
||||
accounts.setSetting(SECURITY_SETTING, JSON.stringify(securityConfig));
|
||||
return {ok: "Discord security settings saved. Reconnect the bot to apply message and join monitoring.", security: securitySettings()};
|
||||
};
|
||||
|
||||
const communitySettings = () => {
|
||||
if (communityConfigLoaded) return {...communityConfig};
|
||||
try {
|
||||
const saved = JSON.parse(accounts.setting(COMMUNITY_SETTING) || "{}");
|
||||
communityConfig = {ticketCategoryId: channelId(saved.ticketCategoryId) || "", ticketStaffRoleId: channelId(saved.ticketStaffRoleId) || "",
|
||||
welcomeChannelId: channelId(saved.welcomeChannelId) || "", welcomeMessage: safe(saved.welcomeMessage || "Welcome {user} to {server}!", 500),
|
||||
goodbyeChannelId: channelId(saved.goodbyeChannelId) || "", goodbyeMessage: safe(saved.goodbyeMessage || "Goodbye {user}.", 500)};
|
||||
}
|
||||
catch { communityConfig = {ticketCategoryId: "", ticketStaffRoleId: "", welcomeChannelId: "", welcomeMessage: "Welcome {user} to {server}!", goodbyeChannelId: "", goodbyeMessage: "Goodbye {user}."}; }
|
||||
communityConfigLoaded = true;
|
||||
return {...communityConfig};
|
||||
};
|
||||
|
||||
const updateCommunitySettings = values => {
|
||||
const current = communitySettings();
|
||||
const next = {...current};
|
||||
for (const key of ["ticketCategoryId", "ticketStaffRoleId", "welcomeChannelId", "goodbyeChannelId"]) {
|
||||
const id = channelId(values[key]);
|
||||
if (id === null) return {error: "Discord channel, category, and role IDs must contain only 5 to 25 digits."};
|
||||
next[key] = id;
|
||||
}
|
||||
next.welcomeMessage = safe(values.welcomeMessage || "", 500);
|
||||
next.goodbyeMessage = safe(values.goodbyeMessage || "", 500);
|
||||
communityConfig = next;
|
||||
communityConfigLoaded = true;
|
||||
accounts.setSetting(COMMUNITY_SETTING, JSON.stringify(next));
|
||||
return {ok: "Community and ticket settings saved.", community: communitySettings()};
|
||||
};
|
||||
|
||||
// Non-secret routing settings are owner-controlled in the dashboard. The bot
|
||||
// token and webhook secret deliberately remain environment-only, so a copied
|
||||
// database or dashboard session cannot disclose them.
|
||||
const loadDashboardConfig = () => {
|
||||
if (loadedDashboardConfig) return;
|
||||
for (const key of MANAGED_CONFIG) {
|
||||
const saved = accounts.setting(`${CONFIG_SETTING_PREFIX}${key}`);
|
||||
if (saved !== null) config.discord[key] = saved;
|
||||
}
|
||||
loadedDashboardConfig = true;
|
||||
};
|
||||
|
||||
const channelId = value => {
|
||||
const id = String(value === undefined || value === null ? "" : value).trim();
|
||||
return id === "" || /^\d{5,25}$/.test(id) ? id : null;
|
||||
};
|
||||
|
||||
const updateDashboardConfig = values => {
|
||||
loadDashboardConfig();
|
||||
const previousStatusChannelId = config.discord.statusChannelId || "";
|
||||
const next = {};
|
||||
for (const [key] of CHANNELS) {
|
||||
const id = channelId(values[key]);
|
||||
if (id === null) return {error: "Discord channel IDs must contain only 5 to 25 digits."};
|
||||
next[key] = id;
|
||||
}
|
||||
const repository = String(values.githubRepository === undefined || values.githubRepository === null ? "" : values.githubRepository).trim();
|
||||
if (repository && !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}\/[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/.test(repository)) {
|
||||
return {error: "GitHub repository must use the form owner/repository."};
|
||||
}
|
||||
next.githubRepository = repository;
|
||||
for (const key of MANAGED_CONFIG) {
|
||||
config.discord[key] = next[key];
|
||||
accounts.setSetting(`${CONFIG_SETTING_PREFIX}${key}`, next[key]);
|
||||
}
|
||||
if (next.statusChannelId !== previousStatusChannelId) {
|
||||
accounts.setSetting(STATUS_MESSAGE_SETTING, "");
|
||||
}
|
||||
return {ok: "Discord routing settings saved.", configuration: dashboardStatus()};
|
||||
};
|
||||
|
||||
const duration = seconds => {
|
||||
if (seconds === -1) return "permanent";
|
||||
if (!seconds) return "none";
|
||||
const hours = Math.ceil(Number(seconds) / 3600);
|
||||
return `${hours} hour${hours === 1 ? "" : "s"}`;
|
||||
};
|
||||
|
||||
// Components V2 means every visible response—success, refusal, report alert
|
||||
// and status card—uses a Container and Text Displays. Never add `content` or
|
||||
// `embeds` to these payloads: Discord ignores them for Components V2.
|
||||
const card = ({title, sections = [], tone = 0x5865f2, ephemeral = false, components = []}) => ({
|
||||
flags: COMPONENTS_V2 | (ephemeral ? EPHEMERAL : 0),
|
||||
allowedMentions: {parse: []},
|
||||
components: [{
|
||||
type: 17, // Container
|
||||
accent_color: tone,
|
||||
components: [
|
||||
{type: 10, content: `## ${safe(title, 160)}`}, // Text Display
|
||||
...sections.filter(Boolean).slice(0, 8).flatMap((section, index) => [
|
||||
...(index ? [{type: 14, divider: true, spacing: 1}] : []), // Separator
|
||||
{type: 10, content: safe(section, 1800)},
|
||||
]),
|
||||
...components,
|
||||
],
|
||||
}],
|
||||
});
|
||||
|
||||
const success = (title, sections, ephemeral = true) => card({title, sections, tone: 0x57f287, ephemeral});
|
||||
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});
|
||||
|
||||
const commands = [
|
||||
new SlashCommandBuilder().setName("burrow").setDescription("View Burrow server and player information")
|
||||
.addSubcommand(command => command.setName("status").setDescription("Show the live Burrow server status"))
|
||||
.addSubcommand(command => command.setName("player").setDescription("Look up a Burrow player")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true)))
|
||||
.addSubcommand(command => command.setName("reports").setDescription("Show open player reports"))
|
||||
.addSubcommand(command => command.setName("staff").setDescription("Show Burrow staff"))
|
||||
.addSubcommand(command => command.setName("announce").setDescription("Send an announcement to everyone playing")
|
||||
.addStringOption(option => option.setName("message").setDescription("Announcement shown in-game").setRequired(true).setMaxLength(300))),
|
||||
new SlashCommandBuilder().setName("moderate").setDescription("Moderate Burrow through your linked staff account")
|
||||
.addSubcommand(command => command.setName("warn").setDescription("Warn a player")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Warning reason, e.g. behavior or custom").setRequired(true))
|
||||
.addStringOption(option => option.setName("note").setDescription("Required explanation for a custom warning")))
|
||||
.addSubcommand(command => command.setName("silence").setDescription("Stop a player from chatting")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true))
|
||||
.addIntegerOption(option => option.setName("hours").setDescription("Whole hours; 0 means until lifted").setRequired(true).setMinValue(0).setMaxValue(87600)))
|
||||
.addSubcommand(command => command.setName("unsilence").setDescription("Let a player chat again")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true)))
|
||||
.addSubcommand(command => command.setName("kick").setDescription("Disconnect an online player")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true)))
|
||||
.addSubcommand(command => command.setName("ban").setDescription("Ban a player")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true))
|
||||
.addIntegerOption(option => option.setName("hours").setDescription("Whole hours; 0 means permanent").setRequired(true).setMinValue(0).setMaxValue(87600))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why this player is being banned").setRequired(true)))
|
||||
.addSubcommand(command => command.setName("unban").setDescription("Lift a player's Burrow ban")
|
||||
.addStringOption(option => option.setName("username").setDescription("Burrow username").setRequired(true))),
|
||||
new SlashCommandBuilder().setName("discord").setDescription("Moderate this Discord server through Burrow staff permissions")
|
||||
.addSubcommand(command => command.setName("timeout").setDescription("Temporarily silence a Discord member")
|
||||
.addUserOption(option => option.setName("member").setDescription("Discord member").setRequired(true))
|
||||
.addIntegerOption(option => option.setName("hours").setDescription("Whole hours, up to 28 days").setRequired(true).setMinValue(1).setMaxValue(672))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why this member is being timed out").setMaxLength(300)))
|
||||
.addSubcommand(command => command.setName("untimeout").setDescription("Remove a Discord member timeout")
|
||||
.addUserOption(option => option.setName("member").setDescription("Discord member").setRequired(true))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why the timeout is being removed").setMaxLength(300)))
|
||||
.addSubcommand(command => command.setName("kick").setDescription("Remove a Discord member from this server")
|
||||
.addUserOption(option => option.setName("member").setDescription("Discord member").setRequired(true))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why this member is being removed").setMaxLength(300)))
|
||||
.addSubcommand(command => command.setName("ban").setDescription("Ban a Discord member from this server")
|
||||
.addUserOption(option => option.setName("member").setDescription("Discord member").setRequired(true))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why this member is being banned").setMaxLength(300)))
|
||||
.addSubcommand(command => command.setName("unban").setDescription("Lift a Discord server ban")
|
||||
.addStringOption(option => option.setName("user_id").setDescription("Discord user ID").setRequired(true).setMinLength(5).setMaxLength(25))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why the ban is being removed").setMaxLength(300)))
|
||||
.addSubcommand(command => command.setName("purge").setDescription("Delete recent messages in this channel")
|
||||
.addIntegerOption(option => option.setName("amount").setDescription("Messages to delete (1 to 100)").setRequired(true).setMinValue(1).setMaxValue(100))
|
||||
.addStringOption(option => option.setName("reason").setDescription("Why messages are being removed").setMaxLength(300))),
|
||||
new SlashCommandBuilder().setName("ticket").setDescription("Open or manage a private support ticket")
|
||||
.addSubcommand(command => command.setName("open").setDescription("Open a private support ticket")
|
||||
.addStringOption(option => option.setName("subject").setDescription("What you need help with").setRequired(true).setMaxLength(80)))
|
||||
.addSubcommand(command => command.setName("close").setDescription("Close this support ticket")),
|
||||
].map(command => command.toJSON());
|
||||
|
||||
const actorFor = interaction => modpanel.discordBot.actor(interaction.user.id);
|
||||
|
||||
const reportButton = report => new ActionRowBuilder().addComponents(
|
||||
new ButtonBuilder().setCustomId(`${REPORT_BUTTON_PREFIX}${report.id}:handled`).setLabel("Mark handled").setStyle(ButtonStyle.Success),
|
||||
).toJSON();
|
||||
|
||||
const reportCard = (report, {handled = false, ephemeral = false} = {}) => card({
|
||||
title: handled ? `Report #${report.id} handled` : `Report #${report.id}`,
|
||||
tone: handled ? 0x57f287 : 0xfee75c,
|
||||
ephemeral,
|
||||
sections: [
|
||||
`**${safe(report.reporter, 40)}** reported **${safe(report.target, 40)}**\n${safe(report.reason, 200)}`,
|
||||
report.detail ? `**Details**\n${safe(report.detail, 500)}` : "*No additional details.*",
|
||||
report.room ? `Room: \`${safe(report.room, 100)}\`` : "",
|
||||
],
|
||||
components: handled ? [] : [{type: 14, divider: true, spacing: 1}, reportButton(report)],
|
||||
});
|
||||
|
||||
const playerCard = player => success(`Player: ${player.username}`, [
|
||||
`**Status**\n${player.online ? `Online${player.room ? ` in ${safe(player.room, 100)}` : ""}` : "Offline"}\nRole: ${player.role || "Player"}`,
|
||||
`**Moderation**\nBan: ${duration(player.banned)}\nSilence: ${duration(player.silenced)}\nWarnings: ${player.warnings}\nOpen reports: ${player.openReports}`,
|
||||
player.discord ? `**Discord**\n${safe(player.discord, 80)}` : "",
|
||||
]);
|
||||
|
||||
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 staffCard = staff => success("Burrow staff", staff.length
|
||||
? [staff.map(member => `**${safe(member.username, 40)}** — ${safe(member.role, 30)}${member.twoStep ? " · 2FA" : ""}`).join("\n")]
|
||||
: ["No active Burrow staff accounts."]);
|
||||
|
||||
const inConfiguredGuild = interaction => interaction.guildId === config.discord.guildId;
|
||||
|
||||
const replyResult = async (interaction, result, title = "Burrow") => {
|
||||
if (result.error) {
|
||||
await interaction.editReply(failure(result.error));
|
||||
return;
|
||||
}
|
||||
await interaction.editReply(success(title, [result.ok, result.detail].filter(Boolean)));
|
||||
};
|
||||
|
||||
const handleBurrow = async interaction => {
|
||||
const actor = actorFor(interaction);
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
if (subcommand === "status") {
|
||||
const result = modpanel.discordBot.status(actor);
|
||||
await interaction.editReply(result.error ? failure(result.error) : {...statusCard(result), flags: COMPONENTS_V2 | EPHEMERAL});
|
||||
return;
|
||||
}
|
||||
if (subcommand === "player") {
|
||||
const result = modpanel.discordBot.player(actor, interaction.options.getString("username", true));
|
||||
await interaction.editReply(result.error ? failure(result.error) : {...playerCard(result.player), flags: COMPONENTS_V2 | EPHEMERAL});
|
||||
return;
|
||||
}
|
||||
if (subcommand === "staff") {
|
||||
const result = modpanel.discordBot.staff(actor);
|
||||
await interaction.editReply(result.error ? failure(result.error) : staffCard(result.staff));
|
||||
return;
|
||||
}
|
||||
if (subcommand === "announce") {
|
||||
await replyResult(interaction, modpanel.discordBot.announce(actor, interaction.options.getString("message", true)), "In-game announcement");
|
||||
return;
|
||||
}
|
||||
const result = modpanel.discordBot.reports(actor);
|
||||
if (result.error) {
|
||||
await interaction.editReply(failure(result.error));
|
||||
return;
|
||||
}
|
||||
const shown = result.reports.slice(0, 5);
|
||||
await interaction.editReply(success("Open reports", [
|
||||
`${result.open} open report${result.open === 1 ? "" : "s"}.`,
|
||||
shown.length ? shown.map(report => `#${report.id} — **${safe(report.target, 40)}** — ${safe(report.reason, 100)}`).join("\n") : "No reports are waiting.",
|
||||
]));
|
||||
};
|
||||
|
||||
const handleModerate = async interaction => {
|
||||
const actor = actorFor(interaction);
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
const username = interaction.options.getString("username", true);
|
||||
const fields = {action: subcommand};
|
||||
if (subcommand === "warn") {
|
||||
fields.reason = interaction.options.getString("reason", true);
|
||||
fields.note = interaction.options.getString("note") || "";
|
||||
}
|
||||
if (subcommand === "silence" || subcommand === "ban") {
|
||||
fields.hours = interaction.options.getInteger("hours", true);
|
||||
}
|
||||
if (subcommand === "ban") {
|
||||
fields.reason = interaction.options.getString("reason", true);
|
||||
}
|
||||
await replyResult(interaction, modpanel.discordBot.act(actor, username, fields), `Moderation: ${subcommand}`);
|
||||
};
|
||||
|
||||
const nativeDiscordPermission = action => ({
|
||||
timeout: "chat.silence",
|
||||
untimeout: "chat.silence",
|
||||
kick: "player.kick",
|
||||
ban: "player.ban",
|
||||
unban: "player.ban",
|
||||
purge: "player.kick",
|
||||
})[action] || "";
|
||||
|
||||
const handleDiscordModeration = async interaction => {
|
||||
const actor = actorFor(interaction);
|
||||
const action = interaction.options.getSubcommand();
|
||||
const refused = modpanel.discordBot.authorize(actor, nativeDiscordPermission(action));
|
||||
if (refused) {
|
||||
await interaction.editReply(failure(refused.error));
|
||||
return;
|
||||
}
|
||||
const reason = safe(interaction.options.getString("reason") || `Burrow staff action by ${actor.username}`, 300);
|
||||
const user = action === "unban" ? null : interaction.options.getUser("member", true);
|
||||
if (user && modpanel.discordBot.actor(user.id)) {
|
||||
await interaction.editReply(failure("Discord-native moderation cannot target an active linked Burrow staff account."));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let detail = "";
|
||||
let target = user ? user.username : interaction.options.getString("user_id", true);
|
||||
if (action === "timeout" || action === "untimeout" || action === "kick") {
|
||||
const member = await interaction.guild.members.fetch(user.id);
|
||||
if (action === "timeout") {
|
||||
const hours = interaction.options.getInteger("hours", true);
|
||||
await member.timeout(hours * 3600000, reason);
|
||||
detail = `${hours} hour${hours === 1 ? "" : "s"}`;
|
||||
}
|
||||
else if (action === "untimeout") {
|
||||
await member.timeout(null, reason);
|
||||
}
|
||||
else {
|
||||
await member.kick(reason);
|
||||
}
|
||||
}
|
||||
else if (action === "ban") {
|
||||
await interaction.guild.members.ban(user.id, {reason, deleteMessageSeconds: 0});
|
||||
}
|
||||
else if (action === "unban") {
|
||||
if (!/^\d{5,25}$/.test(target)) throw new Error("bad_user_id");
|
||||
await interaction.guild.members.unban(target, reason);
|
||||
}
|
||||
else if (action === "purge") {
|
||||
const amount = interaction.options.getInteger("amount", true);
|
||||
if (!interaction.channel || !interaction.channel.isTextBased() || typeof interaction.channel.bulkDelete !== "function") throw new Error("not_text_channel");
|
||||
const deleted = await interaction.channel.bulkDelete(amount, true);
|
||||
detail = `${deleted.size} message${deleted.size === 1 ? "" : "s"}`;
|
||||
target = "this channel";
|
||||
}
|
||||
else throw new Error("unknown_action");
|
||||
modlog.record(actor.username, `Discord ${action}`, target, detail || reason);
|
||||
await interaction.editReply(success(`Discord ${action}`, [`${safe(target, 80)} — ${detail || "completed"}.`].concat(reason ? [`Reason: ${reason}`] : [])));
|
||||
}
|
||||
catch (err) {
|
||||
if (err.message === "bad_user_id") {
|
||||
await interaction.editReply(failure("Enter a valid Discord user ID."));
|
||||
return;
|
||||
}
|
||||
console.log(`[discord-bot] Discord ${action} failed: ${logText(err.message)}`);
|
||||
await interaction.editReply(failure("Discord could not complete that action. Check the bot permission and role position."));
|
||||
}
|
||||
};
|
||||
|
||||
const ticketOwner = channel => (channel && /^burrow-ticket:(\d{5,25})$/.exec(channel.topic || "") || [])[1] || "";
|
||||
|
||||
const handleTicket = async interaction => {
|
||||
const action = interaction.options.getSubcommand();
|
||||
const settings = communitySettings();
|
||||
if (action === "open") {
|
||||
if (!settings.ticketCategoryId || !settings.ticketStaffRoleId) {
|
||||
await interaction.editReply(failure("Tickets are not configured. Ask an owner to set the ticket category and staff role in the Discord dashboard."));
|
||||
return;
|
||||
}
|
||||
const subject = safe(interaction.options.getString("subject", true), 80);
|
||||
const channel = await interaction.guild.channels.create({
|
||||
name: `ticket-${interaction.user.id}`,
|
||||
type: ChannelType.GuildText,
|
||||
parent: settings.ticketCategoryId,
|
||||
topic: `burrow-ticket:${interaction.user.id}`,
|
||||
permissionOverwrites: [
|
||||
{id: interaction.guild.roles.everyone.id, deny: [PermissionFlagsBits.ViewChannel]},
|
||||
{id: interaction.user.id, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ReadMessageHistory]},
|
||||
{id: settings.ticketStaffRoleId, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ReadMessageHistory]},
|
||||
{id: client.user.id, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ManageChannels, PermissionFlagsBits.ReadMessageHistory]},
|
||||
],
|
||||
}).catch(() => null);
|
||||
if (!channel) {
|
||||
await interaction.editReply(failure("Discord could not open the ticket. Check the category, staff role, and bot channel permissions."));
|
||||
return;
|
||||
}
|
||||
await channel.send(card({title: "Support ticket", tone: 0x5865f2, sections: [`Opened by **${safe(interaction.user.username, 80)}**\nSubject: ${subject}`, "A staff member will reply here. Use `/ticket close` when this is resolved."]}));
|
||||
await interaction.editReply(success("Ticket opened", [`Your private ticket: ${channel}`]));
|
||||
return;
|
||||
}
|
||||
const owner = ticketOwner(interaction.channel);
|
||||
const staff = actorFor(interaction);
|
||||
if (!owner || (owner !== interaction.user.id && !staff)) {
|
||||
await interaction.editReply(failure("Only the ticket opener or linked Burrow staff can close this ticket."));
|
||||
return;
|
||||
}
|
||||
await interaction.channel.setName(`closed-${interaction.channel.name}`.slice(0, 100)).catch(() => {});
|
||||
if (owner) await interaction.channel.permissionOverwrites.edit(owner, {ViewChannel: false}).catch(() => {});
|
||||
modlog.record(staff ? staff.username : interaction.user.username, "Discord ticket closed", interaction.channel.name);
|
||||
await interaction.editReply(success("Ticket closed", ["The opener can no longer view this channel. Staff retain it for the support record."]));
|
||||
};
|
||||
|
||||
const handleButton = async interaction => {
|
||||
const match = new RegExp(`^${REPORT_BUTTON_PREFIX.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(\\d+):handled$`).exec(interaction.customId);
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
if (!inConfiguredGuild(interaction)) {
|
||||
await interaction.reply(failure("This control belongs to another Discord server.")).catch(() => {});
|
||||
return true;
|
||||
}
|
||||
const result = modpanel.discordBot.handleReport(actorFor(interaction), Number(match[1]));
|
||||
if (result.error) {
|
||||
await interaction.reply(failure(result.error)).catch(() => {});
|
||||
return true;
|
||||
}
|
||||
await interaction.update(success(`Report #${Number(match[1])} handled`, [result.ok], false)).catch(() => {});
|
||||
return true;
|
||||
};
|
||||
|
||||
let client = null;
|
||||
let statusTimer = null;
|
||||
let unsubscribeReports = null;
|
||||
let unsubscribeGameActivity = null;
|
||||
let unsubscribeModeration = null;
|
||||
let securityGatewayOn = false;
|
||||
|
||||
const channelFor = async id => {
|
||||
if (!id || !client || !client.isReady()) return null;
|
||||
const channel = await client.channels.fetch(id).catch(() => null);
|
||||
return channel && channel.isTextBased() ? channel : null;
|
||||
};
|
||||
|
||||
const postCard = async (channelId, payload, label) => {
|
||||
const channel = await channelFor(channelId);
|
||||
if (channel) {
|
||||
await channel.send(payload).catch(err => console.log(`[discord-bot] ${label} failed: ${logText(err.message)}`));
|
||||
}
|
||||
};
|
||||
|
||||
const publishStatus = async () => {
|
||||
const channel = await channelFor(config.discord.statusChannelId);
|
||||
if (!channel) return {error: "The public status channel is not configured or the Discord bot is not connected."};
|
||||
const payload = statusCard({players: 0, rooms: 0, reports: reports.openCount(), uptimeSeconds: Math.floor(process.uptime()), memoryMb: Math.round(process.memoryUsage().rss / 1048576)});
|
||||
// The public status card needs no staff actor; it exposes only aggregate
|
||||
// counts. Store its message id in the server database so restarts edit it.
|
||||
const saved = accounts.setting(STATUS_MESSAGE_SETTING);
|
||||
const existing = saved && await channel.messages.fetch(saved).catch(() => null);
|
||||
if (existing) {
|
||||
const updated = await existing.edit(payload).then(() => true).catch(err => {
|
||||
console.log(`[discord-bot] status update failed: ${logText(err.message)}`);
|
||||
return false;
|
||||
});
|
||||
return updated ? {ok: "The public status card was updated."} : {error: "Discord could not update the public status card."};
|
||||
}
|
||||
const message = await channel.send(payload).catch(err => {
|
||||
console.log(`[discord-bot] status post failed: ${logText(err.message)}`);
|
||||
return null;
|
||||
});
|
||||
if (!message) return {error: "Discord could not post the public status card."};
|
||||
accounts.setSetting(STATUS_MESSAGE_SETTING, message.id);
|
||||
return {ok: "The public status card was posted."};
|
||||
};
|
||||
|
||||
const notifyReport = async report => {
|
||||
await postCard(config.discord.reportsChannelId, reportCard(report), "report notification");
|
||||
};
|
||||
|
||||
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}`],
|
||||
}), "game activity log");
|
||||
};
|
||||
|
||||
const notifyModeration = async entry => {
|
||||
await postCard(config.discord.moderationLogsChannelId, card({
|
||||
title: "Moderation audit",
|
||||
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)}` : "",
|
||||
],
|
||||
}), "moderation log");
|
||||
};
|
||||
|
||||
const notifyBot = async (interaction, action) => {
|
||||
const actor = modpanel.discordBot.actor(interaction.user.id);
|
||||
await postCard(config.discord.botLogsChannelId, card({
|
||||
title: "Discord bot audit",
|
||||
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.",
|
||||
],
|
||||
}), "Discord bot audit log");
|
||||
};
|
||||
|
||||
const securityIncident = async (title, sections, detail = "") => {
|
||||
await postCard(config.discord.moderationLogsChannelId, card({title, tone: 0xed4245, sections}), "Discord security log");
|
||||
modlog.record("discord security", title.toLowerCase(), "", detail);
|
||||
};
|
||||
|
||||
const isConfiguredGuild = guild => Boolean(guild && guild.id === config.discord.guildId);
|
||||
|
||||
const inspectDiscordMessage = async message => {
|
||||
const settings = securitySettings();
|
||||
if (!settings.enabled || message.author?.bot || !isConfiguredGuild(message.guild)) return;
|
||||
const at = Date.now();
|
||||
const key = message.author.id;
|
||||
const times = (recentMessages.get(key) || []).filter(time => time > at - 10000);
|
||||
times.push(at);
|
||||
recentMessages.set(key, times);
|
||||
const text = String(message.content || "").toLowerCase();
|
||||
const hasInvite = settings.blockInvites && /(?:discord\.gg|discord(?:app)?\.com\/invite)\//i.test(text);
|
||||
const word = settings.blockedWords.find(entry => text.includes(entry));
|
||||
const reason = times.length > settings.spamLimit ? "message spam" : hasInvite ? "Discord invite" : word ? "blocked word" : "";
|
||||
if (!reason) return;
|
||||
if (message.deletable) await message.delete().catch(() => {});
|
||||
await securityIncident("Discord AutoMod", [
|
||||
`Member: **${safe(message.author.username, 80)}**`,
|
||||
`Action: deleted message for ${reason}.`,
|
||||
], `${safe(message.author.username, 80)}: ${reason}`);
|
||||
};
|
||||
|
||||
const inspectDiscordJoin = async member => {
|
||||
const settings = securitySettings();
|
||||
if (!settings.enabled || !isConfiguredGuild(member.guild)) return;
|
||||
const at = Date.now();
|
||||
recentJoins.push(at);
|
||||
while (recentJoins.length && recentJoins[0] <= at - 60000) recentJoins.shift();
|
||||
if (recentJoins.length < settings.raidJoinLimit || lastRaidAlertAt > at - 60000) return;
|
||||
lastRaidAlertAt = at;
|
||||
await securityIncident("Discord anti-raid alert", [
|
||||
`${recentJoins.length} members joined in the last minute.`,
|
||||
"Review recent joins and use the dashboard or Discord moderation controls if action is needed.",
|
||||
], `${recentJoins.length} joins in one minute`);
|
||||
};
|
||||
|
||||
const communityNotice = async (member, kind) => {
|
||||
const settings = communitySettings();
|
||||
const channelId = kind === "welcome" ? settings.welcomeChannelId : settings.goodbyeChannelId;
|
||||
const template = kind === "welcome" ? settings.welcomeMessage : settings.goodbyeMessage;
|
||||
if (!channelId || !isConfiguredGuild(member.guild)) return;
|
||||
const message = safe(template.replace(/\{user\}/gi, member.user.username).replace(/\{server\}/gi, member.guild.name), 500);
|
||||
if (message) await postCard(channelId, card({title: kind === "welcome" ? "Welcome" : "Goodbye", tone: kind === "welcome" ? 0x57f287 : 0x5865f2, sections: [message]}), `${kind} notice`);
|
||||
};
|
||||
|
||||
const githubUpdatesOn = () => {
|
||||
loadDashboardConfig();
|
||||
return Boolean(config.discord.githubRepository && config.discord.githubWebhookSecret && config.discord.updatesChannelId);
|
||||
};
|
||||
|
||||
// Safe to expose to the owner dashboard: it deliberately contains state and
|
||||
// channel ids only, never the bot token or GitHub webhook secret.
|
||||
const dashboardStatus = () => {
|
||||
loadDashboardConfig();
|
||||
return {
|
||||
botTokenConfigured: Boolean(config.discord.botToken),
|
||||
guildId: config.discord.guildId || "",
|
||||
connected: Boolean(client && client.isReady()),
|
||||
commands: ["/burrow status", "/burrow player", "/burrow reports", "/burrow staff", "/burrow announce", "/moderate warn", "/moderate silence", "/moderate unsilence", "/moderate kick", "/moderate ban", "/moderate unban", "/discord timeout", "/discord untimeout", "/discord kick", "/discord ban", "/discord unban", "/discord purge"],
|
||||
channels: CHANNELS.map(([key, name]) => ({key, name, id: config.discord[key] || "", configured: Boolean(config.discord[key])})),
|
||||
github: {
|
||||
repository: config.discord.githubRepository || "",
|
||||
webhookSecretConfigured: Boolean(config.discord.githubWebhookSecret),
|
||||
updatesEnabled: githubUpdatesOn(),
|
||||
endpoint: "/discord/github",
|
||||
},
|
||||
statusCard: statusSettings(),
|
||||
security: {...securitySettings(), monitoring: securityGatewayOn},
|
||||
community: communitySettings(),
|
||||
modules: [
|
||||
{name: "Burrow staff tools", detail: "Player lookup, reports, staff list, in-game announcements, and game moderation", enabled: Boolean(config.discord.botToken && config.discord.guildId)},
|
||||
{name: "Discord moderation", detail: "Timeout, kick, ban, unban, and purge; gated by linked Burrow staff permissions", enabled: Boolean(config.discord.botToken && config.discord.guildId)},
|
||||
{name: "Activity and audit logs", detail: "In-game activity, reports, moderation, and bot command audit streams", enabled: CHANNELS.some(([key]) => key !== "updatesChannelId" && Boolean(config.discord[key]))},
|
||||
{name: "GitHub updates", detail: "Signed commit cards, with client changes marked as game updates", enabled: githubUpdatesOn()},
|
||||
{name: "AutoMod and anti-raid", detail: "Blocked words, invite blocking, anti-spam, and join-rate alerts", enabled: securitySettings().enabled},
|
||||
{name: "Tickets and member notices", detail: "Private support tickets plus configurable welcome and goodbye cards", enabled: Boolean(communitySettings().ticketCategoryId && communitySettings().ticketStaffRoleId)},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
const githubBody = req => new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
req.on("data", chunk => {
|
||||
size += chunk.length;
|
||||
if (size > MAX_GITHUB_WEBHOOK_BYTES) {
|
||||
req.destroy();
|
||||
reject(new Error("too_large"));
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
req.on("end", () => resolve(Buffer.concat(chunks)));
|
||||
req.on("error", reject);
|
||||
});
|
||||
|
||||
const githubSignatureMatches = (body, signature) => {
|
||||
if (!/^sha256=[0-9a-f]{64}$/i.test(String(signature || ""))) return false;
|
||||
const expected = `sha256=${crypto.createHmac("sha256", config.discord.githubWebhookSecret).update(body).digest("hex")}`;
|
||||
return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(String(signature)));
|
||||
};
|
||||
|
||||
const changedFiles = commit => [commit.added, commit.modified, commit.removed].flat().filter(file => typeof file === "string");
|
||||
const gameChange = payload => (payload.commits || []).some(commit => changedFiles(commit)
|
||||
.some(file => /^(swf\/|swf-src\/|client-updates\/|public\/(?:badges|music|fonts)\/)/.test(file)));
|
||||
|
||||
const githubUpdateCard = payload => {
|
||||
const commits = Array.isArray(payload.commits) ? payload.commits.slice(-5) : [];
|
||||
const isGameUpdate = gameChange(payload);
|
||||
const compare = config.githubUrl ? `${config.githubUrl.replace(/\/$/, "")}/compare/${safe(payload.before, 40)}...${safe(payload.after, 40)}` : "";
|
||||
const branch = String(payload.ref || "").replace(/^refs\/heads\//, "") || "default branch";
|
||||
return card({
|
||||
title: isGameUpdate ? "Game update" : "Server update",
|
||||
tone: isGameUpdate ? 0x57f287 : 0x5865f2,
|
||||
sections: [
|
||||
`**${safe(config.discord.githubRepository, 100)}** · ${safe(branch, 100)}\n${commits.length} new commit${commits.length === 1 ? "" : "s"}`,
|
||||
commits.length ? commits.map(commit => `\`${safe(String(commit.id || "").slice(0, 7), 7)}\` ${safe(String(commit.message || "").split("\n")[0], 180)} — ${safe(commit.author && commit.author.name, 80)}`).join("\n") : "No commit details were supplied.",
|
||||
],
|
||||
components: compare ? [{type: 1, components: [{type: 2, style: 5, label: "View on GitHub", url: compare}]}] : [],
|
||||
});
|
||||
};
|
||||
|
||||
// GitHub calls this public endpoint. It is intentionally signature-first:
|
||||
// an untrusted request never reaches Discord or becomes a log entry.
|
||||
const handleGithubWebhook = async (req, res) => {
|
||||
const answer = status => { res.writeHead(status, {"Content-Length": 0}); res.end(); };
|
||||
if (!githubUpdatesOn()) {
|
||||
answer(404);
|
||||
return;
|
||||
}
|
||||
if (req.method !== "POST") {
|
||||
answer(405);
|
||||
return;
|
||||
}
|
||||
let body;
|
||||
try {
|
||||
body = await githubBody(req);
|
||||
}
|
||||
catch {
|
||||
answer(413);
|
||||
return;
|
||||
}
|
||||
if (!githubSignatureMatches(body, req.headers["x-hub-signature-256"])) {
|
||||
answer(401);
|
||||
return;
|
||||
}
|
||||
if (req.headers["x-github-event"] === "ping") {
|
||||
answer(204);
|
||||
return;
|
||||
}
|
||||
if (req.headers["x-github-event"] !== "push") {
|
||||
answer(204);
|
||||
return;
|
||||
}
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(body.toString("utf8"));
|
||||
}
|
||||
catch {
|
||||
answer(400);
|
||||
return;
|
||||
}
|
||||
if (payload.repository?.full_name !== config.discord.githubRepository || payload.deleted === true) {
|
||||
answer(204);
|
||||
return;
|
||||
}
|
||||
const channel = await channelFor(config.discord.updatesChannelId);
|
||||
if (!channel) {
|
||||
answer(503); // GitHub retries after the bot finishes reconnecting.
|
||||
return;
|
||||
}
|
||||
const posted = await channel.send(githubUpdateCard(payload)).then(() => true).catch(err => {
|
||||
console.log(`[discord-bot] GitHub update post failed: ${logText(err.message)}`);
|
||||
return false;
|
||||
});
|
||||
answer(posted ? 204 : 503);
|
||||
};
|
||||
|
||||
const syncCommands = async () => {
|
||||
if (!client || !client.isReady()) return {error: "The Discord bot is not connected."};
|
||||
const guild = await client.guilds.fetch(config.discord.guildId).catch(() => null);
|
||||
if (!guild) return {error: "The configured Discord server could not be found."};
|
||||
const synced = await guild.commands.set(commands).then(() => true).catch(err => {
|
||||
console.log(`[discord-bot] command registration failed: ${logText(err.message)}`);
|
||||
return false;
|
||||
});
|
||||
return synced ? {ok: "Discord slash commands are synchronized."} : {error: "Discord could not synchronize slash commands."};
|
||||
};
|
||||
|
||||
const startup = () => {
|
||||
loadDashboardConfig();
|
||||
const security = securitySettings();
|
||||
const community = communitySettings();
|
||||
if (!config.discord.botToken || !config.discord.guildId) {
|
||||
return false;
|
||||
}
|
||||
if (client) return true;
|
||||
securityGatewayOn = security.enabled;
|
||||
const memberEvents = securityGatewayOn || Boolean(community.welcomeChannelId || community.goodbyeChannelId);
|
||||
client = new Client({intents: securityGatewayOn
|
||||
? [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers]
|
||||
: memberEvents ? [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] : [GatewayIntentBits.Guilds]});
|
||||
client.once("ready", async () => {
|
||||
console.log(`[discord-bot] signed in as ${logText(client.user.tag)}`);
|
||||
const synced = await syncCommands();
|
||||
console.log(`[discord-bot] ${synced.ok || synced.error}`);
|
||||
await publishStatus();
|
||||
});
|
||||
client.on("interactionCreate", async interaction => {
|
||||
if (interaction.user.bot) return;
|
||||
if (interaction.isButton()) {
|
||||
await notifyBot(interaction, `button ${interaction.customId}`).catch(() => {});
|
||||
await handleButton(interaction);
|
||||
return;
|
||||
}
|
||||
if (!interaction.isChatInputCommand() || !inConfiguredGuild(interaction)) return;
|
||||
await notifyBot(interaction, `/${interaction.commandName} ${interaction.options.getSubcommand(false) || ""}`.trim()).catch(() => {});
|
||||
await interaction.deferReply({flags: EPHEMERAL}).catch(() => {});
|
||||
if (!interaction.deferred) return;
|
||||
try {
|
||||
if (interaction.commandName === "burrow") await handleBurrow(interaction);
|
||||
else if (interaction.commandName === "moderate") await handleModerate(interaction);
|
||||
else if (interaction.commandName === "discord") await handleDiscordModeration(interaction);
|
||||
else if (interaction.commandName === "ticket") await handleTicket(interaction);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(`[discord-bot] interaction failed: ${err.stack || err.message}`);
|
||||
await interaction.editReply(failure("The command could not be completed.")).catch(() => {});
|
||||
}
|
||||
});
|
||||
client.on("error", err => console.error(`[discord-bot] client error: ${err.message}`));
|
||||
if (securityGatewayOn) {
|
||||
client.on("messageCreate", message => { inspectDiscordMessage(message).catch(err => console.error(`[discord-bot] AutoMod failed: ${err.message}`)); });
|
||||
client.on("guildMemberAdd", member => { inspectDiscordJoin(member).catch(err => console.error(`[discord-bot] anti-raid check failed: ${err.message}`)); });
|
||||
}
|
||||
if (memberEvents) {
|
||||
client.on("guildMemberAdd", member => { communityNotice(member, "welcome").catch(err => console.error(`[discord-bot] welcome notice failed: ${err.message}`)); });
|
||||
client.on("guildMemberRemove", member => { communityNotice(member, "goodbye").catch(err => console.error(`[discord-bot] goodbye notice failed: ${err.message}`)); });
|
||||
}
|
||||
unsubscribeReports = reports.onFile(notifyReport);
|
||||
unsubscribeGameActivity = world.onGameActivity(notifyGameActivity);
|
||||
unsubscribeModeration = modlog.onRecord(notifyModeration);
|
||||
statusTimer = setInterval(() => { publishStatus().catch(err => console.error(`[discord-bot] status failed: ${err.message}`)); }, STATUS_EVERY_MS);
|
||||
statusTimer.unref();
|
||||
client.login(config.discord.botToken).catch(err => console.error(`[discord-bot] login failed: ${err.message}`));
|
||||
return true;
|
||||
};
|
||||
|
||||
const shutdown = () => {
|
||||
if (statusTimer) clearInterval(statusTimer);
|
||||
statusTimer = null;
|
||||
if (unsubscribeReports) unsubscribeReports();
|
||||
unsubscribeReports = null;
|
||||
if (unsubscribeGameActivity) unsubscribeGameActivity();
|
||||
unsubscribeGameActivity = null;
|
||||
if (unsubscribeModeration) unsubscribeModeration();
|
||||
unsubscribeModeration = null;
|
||||
if (client) client.destroy();
|
||||
client = null;
|
||||
securityGatewayOn = false;
|
||||
recentMessages.clear();
|
||||
recentJoins.length = 0;
|
||||
};
|
||||
|
||||
const control = async action => {
|
||||
if (action === "stop") {
|
||||
if (!client) return {error: "The Discord bot is already stopped."};
|
||||
shutdown();
|
||||
return {ok: "Discord bot stopped. Game and panel services remain online."};
|
||||
}
|
||||
if (action === "start") {
|
||||
return startup() ? {ok: "Discord bot is starting."} : {error: "Set AJ_DISCORD_BOT_TOKEN and AJ_DISCORD_GUILD_ID before starting the bot."};
|
||||
}
|
||||
if (action === "reconnect") {
|
||||
shutdown();
|
||||
return startup() ? {ok: "Discord bot is reconnecting."} : {error: "Set AJ_DISCORD_BOT_TOKEN and AJ_DISCORD_GUILD_ID before reconnecting the bot."};
|
||||
}
|
||||
if (action === "sync") return syncCommands();
|
||||
return {error: "Unknown Discord bot control."};
|
||||
};
|
||||
|
||||
module.exports = {startup, shutdown, control, card, COMPONENTS_V2, dashboardStatus, updateDashboardConfig, updateSecuritySettings, updateCommunitySettings, setStatusSettings, publishStatus, handleGithubWebhook};
|
||||
@@ -22,6 +22,7 @@ const defpacks = require("./defpacks.js");
|
||||
const limits = require("./limits.js");
|
||||
const manage = require("./manage.js");
|
||||
const modpanel = require("./modpanel.js");
|
||||
const discordBot = require("./discordbot.js");
|
||||
const badges = require("./badges.js");
|
||||
const extraMusic = require("./extramusic.js");
|
||||
const customMusic = require("./custommusic.js");
|
||||
@@ -758,6 +759,12 @@ const handle = async (req, res) => {
|
||||
from.local = false;
|
||||
}
|
||||
|
||||
// GitHub sends signed push events here for optional Discord update cards.
|
||||
if (url.pathname === "/discord/github") {
|
||||
await discordBot.handleGithubWebhook(req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
// One entry point for the owner and staff dashboards. The old addresses
|
||||
// stay as bookmark-friendly redirects; their APIs remain available for
|
||||
// installed tools and are not redirected.
|
||||
|
||||
+20
-2
@@ -18,13 +18,23 @@ const SCHEMA = `
|
||||
`;
|
||||
const MAX_DETAIL = 400;
|
||||
const SHOWN = 300;
|
||||
const listeners = new Set();
|
||||
|
||||
const load = () => accounts.database().exec(SCHEMA);
|
||||
|
||||
const record = (by, action, target = "", detail = "") => {
|
||||
const entry = {at: Math.floor(Date.now() / 1000), by: String(by), action: String(action), target: String(target), detail: String(detail).slice(0, MAX_DETAIL)};
|
||||
accounts.database().prepare("INSERT INTO mod_log (at, by, action, target, detail) VALUES (?, ?, ?, ?, ?)")
|
||||
.run(Math.floor(Date.now() / 1000), by, action, String(target), String(detail).slice(0, MAX_DETAIL));
|
||||
.run(entry.at, entry.by, entry.action, entry.target, entry.detail);
|
||||
console.log(`[mod] ${logText(by)}: ${logText(action)}${logText(target ? ` "${target}"` : "")}`);
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(entry);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(`[modlog] listener failed: ${logText(err.message)}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const recent = () => accounts.database().prepare("SELECT at, by, action, target, detail FROM mod_log ORDER BY id DESC LIMIT ?").all(SHOWN);
|
||||
@@ -33,4 +43,12 @@ const recent = () => accounts.database().prepare("SELECT at, by, action, target,
|
||||
const about = username => accounts.database().prepare("SELECT at, by, action, target, detail FROM mod_log WHERE target = ? COLLATE NOCASE ORDER BY id DESC LIMIT 100")
|
||||
.all(String(username));
|
||||
|
||||
module.exports = {load, record, recent, about};
|
||||
// Optional integrations subscribe here rather than wrapping record(), so an
|
||||
// unavailable Discord channel can never stop a moderation action or its DB
|
||||
// audit record from being written.
|
||||
const onRecord = listener => {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
};
|
||||
|
||||
module.exports = {load, record, recent, about, onRecord};
|
||||
+86
-1
@@ -1084,4 +1084,89 @@ const moderators = () => accounts.all().filter(account => roles.roleOf(account))
|
||||
.map(account => ({username: account.username, role: roles.roleOf(account), twoStep: totp.isOn(account), lastSeen: account.lastSeen || null}))
|
||||
.sort((a, b) => a.username.localeCompare(b.username));
|
||||
|
||||
module.exports = {handle, forget, moderators, signedInAs, itemRoutes, reviewRoutes};
|
||||
// The Discord bot is deliberately a small adapter over the panel rather than
|
||||
// its own moderator system. Its actor is a Discord user id that is already
|
||||
// linked to one Burrow account. From there these methods use the same role,
|
||||
// rank, permission and action checks as POST /mod/api/act, and log every
|
||||
// successful change under the staff member's normal Burrow username.
|
||||
const discordActor = discordId => {
|
||||
const account = accounts.all().find(candidate => candidate.discordId === String(discordId));
|
||||
return mayModerate(account) ? account : null;
|
||||
};
|
||||
|
||||
const discordError = (actor, permission) => !actor
|
||||
? {error: "Link this Discord account to an active Burrow staff account first."}
|
||||
: !roles.can(actor, permission) ? {error: "Your Burrow role cannot do that."} : null;
|
||||
|
||||
const discordStatus = actor => {
|
||||
const error = discordError(actor, "server.view");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const memory = process.memoryUsage();
|
||||
const players = world.players();
|
||||
return {ok: true, uptimeSeconds: Math.floor(process.uptime()), players: players.length,
|
||||
rooms: new Set(players.map(player => player.room).filter(Boolean)).size,
|
||||
reports: reports.openCount(), memoryMb: Math.round(memory.rss / 1048576)};
|
||||
};
|
||||
|
||||
const discordPlayer = (actor, username) => {
|
||||
const error = discordError(actor, "player.view");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const target = accounts.byName(String(username || "").trim());
|
||||
return target ? {ok: true, player: playerDetail(target)} : {error: "No such player."};
|
||||
};
|
||||
|
||||
const discordReports = actor => {
|
||||
const error = discordError(actor, "player.view");
|
||||
return error || {ok: true, reports: reports.list(false), open: reports.openCount()};
|
||||
};
|
||||
|
||||
const discordStaff = actor => {
|
||||
const error = discordError(actor, "staff.list");
|
||||
return error || {ok: true, staff: moderators()};
|
||||
};
|
||||
|
||||
const discordAnnounce = (actor, text) => {
|
||||
const error = discordError(actor, "server.announce");
|
||||
if (error) return error;
|
||||
const message = String(text || "").trim().slice(0, 300);
|
||||
if (!message) return {error: "The announcement is empty."};
|
||||
const reached = world.announce(message);
|
||||
modlog.record(actor.username, "Discord announcement", "", `${reached} player${reached === 1 ? "" : "s"}`);
|
||||
return {ok: reached === 1 ? "Shown to 1 player." : `Shown to ${reached} players.`};
|
||||
};
|
||||
|
||||
const discordHandleReport = (actor, id) => {
|
||||
const error = discordError(actor, "player.warn");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const handled = reports.markHandled(Number(id));
|
||||
if (handled) {
|
||||
modlog.record(actor.username, "report dealt with", "", `#${Number(id)}`);
|
||||
}
|
||||
return handled ? {ok: "Marked as dealt with."} : {error: "No such report waiting."};
|
||||
};
|
||||
|
||||
const discordAct = (actor, username, body) => {
|
||||
if (!actor) {
|
||||
return {error: "Link this Discord account to an active Burrow staff account first."};
|
||||
}
|
||||
const target = accounts.byName(String(username || "").trim());
|
||||
if (!target) {
|
||||
return {error: "No such player."};
|
||||
}
|
||||
const result = act(actor, target, body || {});
|
||||
if (result.ok) {
|
||||
modlog.record(actor.username, String(body.action), target.username, result.detail || "");
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const discordBot = {actor: discordActor, authorize: discordError, status: discordStatus, player: discordPlayer, reports: discordReports,
|
||||
staff: discordStaff, announce: discordAnnounce, handleReport: discordHandleReport, act: discordAct};
|
||||
|
||||
module.exports = {handle, forget, moderators, signedInAs, itemRoutes, reviewRoutes, discordBot};
|
||||
@@ -9,6 +9,7 @@ const achievements = require("./achievements.js");
|
||||
const adopt = require("./adopt.js");
|
||||
const badges = require("./badges.js");
|
||||
const discordLinks = require("./discord.js");
|
||||
const discordBot = require("./discordbot.js");
|
||||
const bans = require("./bans.js");
|
||||
const colours = require("./colours.js");
|
||||
const chatlog = require("./chatlog.js");
|
||||
@@ -795,6 +796,61 @@ const handle = async (req, res, url, from, web) => {
|
||||
reply(200, overview());
|
||||
return;
|
||||
|
||||
case "GET /admin/api/discord":
|
||||
reply(200, discordBot.dashboardStatus());
|
||||
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}`);
|
||||
reply(200, {ok: "Status-card text saved. Use Publish now to update Discord.", statusCard});
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/discord/status/publish": {
|
||||
const result = await discordBot.publishStatus();
|
||||
if (result.ok) {
|
||||
modlog.record("admin", "Discord public status card published");
|
||||
}
|
||||
reply(result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/discord/config": {
|
||||
const result = discordBot.updateDashboardConfig(body);
|
||||
if (result.ok) {
|
||||
modlog.record("admin", "Discord bot routing settings changed");
|
||||
}
|
||||
reply(result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/discord/security": {
|
||||
const result = discordBot.updateSecuritySettings(body);
|
||||
if (result.ok) {
|
||||
modlog.record("admin", "Discord security settings changed");
|
||||
}
|
||||
reply(result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/discord/community": {
|
||||
const result = discordBot.updateCommunitySettings(body);
|
||||
if (result.ok) modlog.record("admin", "Discord community and ticket settings changed");
|
||||
reply(result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/discord/control": {
|
||||
const action = String(body.action || "");
|
||||
const result = await discordBot.control(action);
|
||||
if (result.ok) {
|
||||
modlog.record("admin", `Discord bot ${action}`);
|
||||
}
|
||||
reply(result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// a page of accounts by name (lib/staff.js says in what order), never
|
||||
// all of them: with thousands, sending every one every few seconds
|
||||
// slowed the page and the game alike.
|
||||
|
||||
+19
-2
@@ -45,6 +45,7 @@ const MERGE_SECONDS = 10;
|
||||
const perTenMinutes = limits.limiter(10, 600000);
|
||||
const now = () => Math.floor(Date.now() / 1000);
|
||||
const clean = (text, max) => String(text === undefined ? "" : text).replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, " ").trim().slice(0, max);
|
||||
const listeners = new Set();
|
||||
|
||||
const load = () => accounts.database().exec(SCHEMA);
|
||||
|
||||
@@ -68,9 +69,18 @@ const file = ({reporter, target, reason, detail = "", context = "", room = ""})
|
||||
if (!perTenMinutes.allow(reporter.toLowerCase()) || openCount() >= MAX_OPEN) {
|
||||
return false;
|
||||
}
|
||||
db.prepare("INSERT INTO reports (created_at, reporter, target, reason, detail, context, room) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
||||
const inserted = db.prepare("INSERT INTO reports (created_at, reporter, target, reason, detail, context, room) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
||||
.run(now(), reporter, target, Number.isInteger(reason) ? reason : 0, clean(detail, MAX_DETAIL), clean(context, MAX_CONTEXT), clean(room, 100));
|
||||
console.log(`[world] ${logText(reporter)} reported "${logText(target)}" (${logText(REASONS[reason] || `reason ${reason}`)})`);
|
||||
const report = row(db.prepare("SELECT * FROM reports WHERE id = ?").get(Number(inserted.lastInsertRowid)));
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(report);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(`[reports] listener failed: ${logText(err.message)}`);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -95,8 +105,15 @@ const list = (handled = false) => accounts.database()
|
||||
|
||||
const markHandled = id => accounts.database().prepare("UPDATE reports SET handled_at = ? WHERE id = ? AND handled_at IS NULL").run(now(), id).changes > 0;
|
||||
|
||||
// Lets optional integrations (the Discord bot) receive a newly filed report
|
||||
// without making a report depend on an external service being available.
|
||||
const onFile = listener => {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
};
|
||||
|
||||
// the reports about one player, newest first, for their timeline.
|
||||
const about = username => accounts.database().prepare("SELECT * FROM reports WHERE target = ? COLLATE NOCASE ORDER BY id DESC LIMIT 100")
|
||||
.all(String(username)).map(row);
|
||||
|
||||
module.exports = {load, file, list, about, markHandled, openCount, MAX_DETAIL};
|
||||
module.exports = {load, file, list, about, markHandled, onFile, openCount, MAX_DETAIL};
|
||||
+2
-1
@@ -16,6 +16,7 @@ const totp = require("./totp.js");
|
||||
|
||||
const PERMISSIONS = {
|
||||
"server.view": "see read-only server health in the moderator panel",
|
||||
"server.announce": "send an announcement to everyone playing",
|
||||
"player.view": "see players, chat, reports, requests, bans and paintings in the moderator panel, and keep notes on players (!note, !notes)",
|
||||
"staff.list": "see which staff are online (!staff)",
|
||||
"player.warn": "warn a player, take a warning back, send them a message and mark a report dealt with (!warn, !unwarn, !warnings, !message)",
|
||||
@@ -38,7 +39,7 @@ const PERMISSIONS = {
|
||||
|
||||
const HELPER = ["server.view", "player.view", "staff.list", "player.warn", "player.goto", "chat.silence"];
|
||||
const MODERATOR = [...HELPER, "player.kick", "player.summon", "staff.invisible", "player.ban", "player.names", "address.ban", "items.approve", "items.restrict", "theater.control", "staff.log"];
|
||||
const ADMIN = [...MODERATOR, "items.create", "room.effects", "music.upload"];
|
||||
const ADMIN = [...MODERATOR, "server.announce", "items.create", "room.effects", "music.upload"];
|
||||
// a developer makes things, and moderates nobody.
|
||||
const DEVELOPER = ["server.view", "staff.list", "items.create", "room.effects", "theater.control"];
|
||||
|
||||
|
||||
@@ -161,6 +161,26 @@ const THEME_PATH_PREFIX = "theme_"; // lib/http.js looks for this
|
||||
const chatLines = limits.limiter(config.chatLinesPerWindow, config.chatWindowMs);
|
||||
const online = new Map(); // lowercased username -> session
|
||||
const rooms = new Map(); // "path.name#shard" -> room
|
||||
const gameActivityListeners = new Set();
|
||||
|
||||
// An intentionally small, privacy-safe activity feed for optional
|
||||
// integrations. It names only the player and lifecycle event—never IPs,
|
||||
// device identifiers, game tokens or chat text.
|
||||
const gameActivity = entry => {
|
||||
for (const listener of gameActivityListeners) {
|
||||
try {
|
||||
listener(entry);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(`[world] activity listener failed: ${logText(err.message)}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onGameActivity = listener => {
|
||||
gameActivityListeners.add(listener);
|
||||
return () => gameActivityListeners.delete(listener);
|
||||
};
|
||||
|
||||
// worlds (config.shards): a land room's name ends in "#<world>"; dens,
|
||||
// parties and adventures have none and belong to no world. each player has
|
||||
@@ -384,6 +404,7 @@ const login = (session, msg) => {
|
||||
accounts.save(account);
|
||||
online.set(key, session);
|
||||
console.log(`[world] ${logText(account.username)} logged in as sfs user ${logText(session.id)}`);
|
||||
gameActivity({type: "login", username: account.username, at: Math.floor(Date.now() / 1000)});
|
||||
session.sendXtJson(-1, {_cmd: "login", status: "1", params: loginParams(session)});
|
||||
// every custom colour, before anything that might be drawn with one
|
||||
// (XtReplyDemuxer -> PaletteHelper.handleCustomColors in the patched SWF).
|
||||
@@ -1379,6 +1400,8 @@ setInterval(() => {
|
||||
}, 60000).unref();
|
||||
|
||||
const leaveWorld = session => {
|
||||
const username = session.account && session.account.username;
|
||||
const playedSeconds = session.playStarted ? Math.max(0, Math.round((Date.now() - session.playStarted) / 1000)) : 0;
|
||||
minigames.leave(session);
|
||||
trades.forget(session);
|
||||
leaveRoom(session);
|
||||
@@ -1390,6 +1413,7 @@ const leaveWorld = session => {
|
||||
accounts.release(session.account);
|
||||
accounts.save(session.account);
|
||||
buddies.announce(session.account);
|
||||
gameActivity({type: "logout", username, at: Math.floor(Date.now() / 1000), playedSeconds});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2134,4 +2158,5 @@ module.exports = {
|
||||
room: session.room ? session.room.name.replace(/#(\d+)$/, (whole, id) => (worldOf(whole) ? ` (${worldName(Number(id))})` : whole)) : null,
|
||||
address: session.address,
|
||||
})),
|
||||
onGameActivity,
|
||||
};
|
||||
Reference in new issue
Block a user