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."};