From df27613d7cb33efb2c59587ec3de4b50d98f1e5e Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 8 Nov 2025 20:50:59 +0000 Subject: [PATCH] Create manual Discord command registration script (separate from bot startup) cgen-6340106fd6884f9b9b1325094070b9ac --- discord-bot/scripts/register-commands.js | 100 +++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 discord-bot/scripts/register-commands.js diff --git a/discord-bot/scripts/register-commands.js b/discord-bot/scripts/register-commands.js new file mode 100644 index 00000000..9d92dc21 --- /dev/null +++ b/discord-bot/scripts/register-commands.js @@ -0,0 +1,100 @@ +const { REST, Routes } = require("discord.js"); +const fs = require("fs"); +const path = require("path"); +require("dotenv").config(); + +// Validate environment variables +const requiredEnvVars = [ + "DISCORD_BOT_TOKEN", + "DISCORD_CLIENT_ID", +]; + +const missingVars = requiredEnvVars.filter((envVar) => !process.env[envVar]); +if (missingVars.length > 0) { + console.error( + "āŒ FATAL ERROR: Missing required environment variables:", + missingVars.join(", ") + ); + console.error("\nPlease set these before running command registration:"); + missingVars.forEach((envVar) => { + console.error(` - ${envVar}`); + }); + process.exit(1); +} + +// Load commands from commands directory +const commandsPath = path.join(__dirname, "../commands"); +const commandFiles = fs + .readdirSync(commandsPath) + .filter((file) => file.endsWith(".js")); + +const commands = []; + +for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + if ("data" in command && "execute" in command) { + commands.push(command.data.toJSON()); + console.log(`āœ… Loaded command: ${command.data.name}`); + } +} + +// Register commands with Discord API +async function registerCommands() { + try { + const rest = new REST({ version: "10" }).setToken( + process.env.DISCORD_BOT_TOKEN + ); + + console.log(`\nšŸ“ Registering ${commands.length} slash commands...`); + console.log("āš ļø This will co-exist with Discord's auto-generated Entry Point command.\n"); + + try { + const data = await rest.put( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { body: commands } + ); + console.log(`āœ… Successfully registered ${data.length} slash commands.`); + console.log("\nšŸŽ‰ Command registration complete!"); + console.log("ā„¹ļø Your commands are now live in Discord."); + console.log("ā„¹ļø The Entry Point command (for Activities) will be managed by Discord.\n"); + } catch (error) { + // Handle Entry Point command conflict + if (error.code === 50240) { + console.warn( + "āš ļø Error 50240: Entry Point command detected (Discord Activity enabled)." + ); + console.warn("Registering commands individually...\n"); + + let successCount = 0; + for (const command of commands) { + try { + await rest.post( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { body: command } + ); + successCount++; + } catch (postError) { + if (postError.code === 50045) { + console.warn(` āš ļø ${command.name}: Already registered (skipping)`); + } else { + console.error(` āŒ ${command.name}: ${postError.message}`); + } + } + } + + console.log(`\nāœ… Registered ${successCount} slash commands (individual mode).`); + console.log("šŸŽ‰ Command registration complete!"); + console.log("ā„¹ļø The Entry Point command will be managed by Discord.\n"); + } else { + throw error; + } + } + } catch (error) { + console.error("āŒ Fatal error registering commands:", error.message || error); + process.exit(1); + } +} + +// Run registration +registerCommands();