From a8ab031616a37f5b27495a30639cf857214096e6 Mon Sep 17 00:00:00 2001 From: Burrowdev Date: Wed, 21 Oct 2026 12:16:57 +0300 Subject: [PATCH] fix(discord-bot): start the server without discord.js installed b11ae1d made the server load discord.js at start, through lib/discordbot.js, even with the bot off. update.sh installs no npm packages, so prod had none, and every start failed with "Cannot find module 'discord.js'". - lib/discordbot.js loads discord.js when the bot starts (a token and guild id set), not with the server. without the package the bot stays off, with a line in the log, and the panel's Start says to run npm ci. the two message flags cards use are written as numbers, and the slash commands are made when they are synchronized. - deploy/update.sh runs npm ci --omit=dev when package-lock.json changed or node_modules is missing. a failed install leaves the bot off; the game needs no package. Co-Authored-By: Claude Opus 5.5 --- deploy/update.sh | 11 +++++++++++ lib/discordbot.js | 47 ++++++++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/deploy/update.sh b/deploy/update.sh index 6c02e94..4f01338 100644 --- a/deploy/update.sh +++ b/deploy/update.sh @@ -47,6 +47,17 @@ for file in $(git diff --name-only "$old" "$new" -- '*.js'); do fi done +# the npm packages (only discord.js, for the optional Discord bot): installed +# again when the lockfile changed, or when they never were. a failed install +# leaves the bot off; the game itself needs none of them. +if [ -f "$dir/package-lock.json" ] && { ! git diff --quiet "$old" "$new" -- package-lock.json || [ ! -d "$dir/node_modules" ]; }; then + if command -v npm >/dev/null && runuser -u "$user" -- npm --prefix "$dir" ci --omit=dev --no-audit --no-fund; then + echo "npm packages installed" + else + echo "npm packages could not be installed; the Discord bot stays off" >&2 + fi +fi + # public/ is read from disk on every request; the rest is loaded once, at start. # the list is taken whole first: piped straight into grep -q, a long one (a # push of many files) had git cut off mid-write ("Broken pipe"), which diff --git a/lib/discordbot.js b/lib/discordbot.js index f053e20..6265eed 100644 --- a/lib/discordbot.js +++ b/lib/discordbot.js @@ -5,17 +5,27 @@ // 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"); +// discord.js is loaded when the bot starts, not with the server: a server +// without the package installed (npm ci) runs as before, the bot off. +let ActionRowBuilder; +let ButtonBuilder; +let ButtonStyle; +let ChannelType; +let Client; +let GatewayIntentBits; +let PermissionFlagsBits; +let SlashCommandBuilder; +const loadDiscordJs = () => { + if (Client) return true; + try { + ({ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, Client, GatewayIntentBits, PermissionFlagsBits, SlashCommandBuilder} = require("discord.js")); + return true; + } + catch (err) { + console.log(`[discord-bot] discord.js could not be loaded (run npm ci): ${logText(err.message)}`); + return false; + } +}; const crypto = require("crypto"); const accounts = require("./accounts.js"); const config = require("../config.js"); @@ -25,8 +35,10 @@ const reports = require("./reports.js"); const world = require("./world.js"); const {logText} = require("./logtext.js"); -const COMPONENTS_V2 = MessageFlags.IsComponentsV2; -const EPHEMERAL = MessageFlags.Ephemeral; +// discord.js's MessageFlags.IsComponentsV2 and MessageFlags.Ephemeral, which +// cards need before the package is loaded +const COMPONENTS_V2 = 1 << 15; +const EPHEMERAL = 1 << 6; const STATUS_MESSAGE_SETTING = "discordBotStatusMessage"; const STATUS_TITLE_SETTING = "discordBotStatusTitle"; const STATUS_NOTE_SETTING = "discordBotStatusNote"; @@ -211,7 +223,7 @@ 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}); -const commands = [ +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")) .addSubcommand(command => command.setName("player").setDescription("Look up a Burrow player") @@ -763,7 +775,7 @@ 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 => { + const synced = await guild.commands.set(commandDefinitions()).then(() => true).catch(err => { console.log(`[discord-bot] command registration failed: ${logText(err.message)}`); return false; }); @@ -778,6 +790,7 @@ const startup = () => { return false; } if (client) return true; + if (!loadDiscordJs()) return false; securityGatewayOn = security.enabled; const memberEvents = securityGatewayOn || Boolean(community.welcomeChannelId || community.goodbyeChannelId); client = new Client({intents: securityGatewayOn @@ -852,11 +865,11 @@ const control = async action => { 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."}; + return startup() ? {ok: "Discord bot is starting."} : {error: "Set AJ_DISCORD_BOT_TOKEN and AJ_DISCORD_GUILD_ID, and install discord.js (npm ci), 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."}; + return startup() ? {ok: "Discord bot is reconnecting."} : {error: "Set AJ_DISCORD_BOT_TOKEN and AJ_DISCORD_GUILD_ID, and install discord.js (npm ci), before reconnecting the bot."}; } if (action === "sync") return syncCommands(); return {error: "Unknown Discord bot control."};