From 9c0a12eafad894042b7dfc358496d731f3816349 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sun, 9 Nov 2025 04:41:42 +0000 Subject: [PATCH] completionId: cgen-849d95395f8e445f8f8833b2500748ac cgen-849d95395f8e445f8f8833b2500748ac --- discord-bot/bot.js | 145 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 2 deletions(-) diff --git a/discord-bot/bot.js b/discord-bot/bot.js index a36242d4..6277b6d4 100644 --- a/discord-bot/bot.js +++ b/discord-bot/bot.js @@ -110,12 +110,128 @@ client.on("interactionCreate", async (interaction) => { // This prevents Error 50240 (Entry Point conflict) when Activities are enabled // The bot will simply load and listen for the already-registered commands +// Define all commands for registration +const COMMANDS_TO_REGISTER = [ + { + name: "verify", + description: "Link your Discord account to AeThex", + }, + { + name: "set-realm", + description: "Choose your primary arm/realm (Labs, GameForge, Corp, etc.)", + options: [ + { + name: "realm", + type: 3, + description: "Your primary realm", + required: true, + choices: [ + { name: "Labs", value: "labs" }, + { name: "GameForge", value: "gameforge" }, + { name: "Corp", value: "corp" }, + { name: "Foundation", value: "foundation" }, + { name: "Dev-Link", value: "devlink" }, + ], + }, + ], + }, + { + name: "profile", + description: "View your linked AeThex profile", + }, + { + name: "unlink", + description: "Disconnect your Discord account from AeThex", + }, + { + name: "verify-role", + description: "Check your assigned Discord roles", + }, +]; + +// Function to register commands with Discord +async function registerDiscordCommands() { + try { + const rest = new REST({ version: "10" }).setToken( + process.env.DISCORD_BOT_TOKEN, + ); + + console.log(`📝 Registering ${COMMANDS_TO_REGISTER.length} slash commands...`); + + try { + // Try bulk update first + const data = await rest.put( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { body: COMMANDS_TO_REGISTER }, + ); + + console.log(`✅ Successfully registered ${data.length} slash commands`); + return { success: true, count: data.length, results: null }; + } catch (bulkError) { + // Handle Error 50240 (Entry Point conflict) + if (bulkError.code === 50240) { + console.warn( + "⚠️ Error 50240: Entry Point detected. Registering individually...", + ); + + const results = []; + let successCount = 0; + let skipCount = 0; + + for (const command of COMMANDS_TO_REGISTER) { + try { + const posted = await rest.post( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { body: command }, + ); + results.push({ + name: command.name, + status: "registered", + id: posted.id, + }); + successCount++; + } catch (postError) { + if (postError.code === 50045) { + results.push({ + name: command.name, + status: "already_exists", + }); + skipCount++; + } else { + results.push({ + name: command.name, + status: "error", + error: postError.message, + }); + } + } + } + + console.log( + `✅ Registration complete: ${successCount} new, ${skipCount} already existed`, + ); + return { + success: true, + count: successCount, + skipped: skipCount, + results, + }; + } + + throw bulkError; + } + } catch (error) { + console.error("❌ Failed to register commands:", error); + return { success: false, error: error.message }; + } +} + // Start HTTP health check server const healthPort = process.env.HEALTH_PORT || 3000; http .createServer((req, res) => { res.setHeader("Access-Control-Allow-Origin", "*"); - res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); + res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); res.setHeader("Content-Type", "application/json"); if (req.method === "OPTIONS") { @@ -138,11 +254,36 @@ http return; } + if (req.url === "/register-commands" && req.method === "POST") { + // Verify admin token if provided + const authHeader = req.headers.authorization; + const adminToken = process.env.DISCORD_ADMIN_REGISTER_TOKEN; + + if (adminToken && authHeader !== `Bearer ${adminToken}`) { + res.writeHead(401); + res.end(JSON.stringify({ error: "Unauthorized" })); + return; + } + + // Register commands + registerDiscordCommands().then((result) => { + if (result.success) { + res.writeHead(200); + res.end(JSON.stringify(result)); + } else { + res.writeHead(500); + res.end(JSON.stringify(result)); + } + }); + return; + } + res.writeHead(404); res.end(JSON.stringify({ error: "Not found" })); }) .listen(healthPort, () => { - console.log(`🏥 Health check server running on port ${healthPort}`); + console.log(`���� Health check server running on port ${healthPort}`); + console.log(`📝 Register commands at: POST http://localhost:${healthPort}/register-commands`); }); client.login(process.env.DISCORD_BOT_TOKEN);