"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startBot = startBot; const discord_js_1 = require("discord.js"); const new_agent_1 = require("./commands/new-agent"); const router_1 = require("./router"); async function startBot(envConfig, disclawConfig, db) { const client = new discord_js_1.Client({ intents: [ discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.MessageContent, ], }); // Track the management channel id once resolved let managementChannelId = null; // --- Event: Ready --- client.once("ready", async () => { if (!client.user) return; console.log(`Bot online as ${client.user.tag}`); const guild = envConfig.DISCORD_GUILD_ID ? client.guilds.cache.get(envConfig.DISCORD_GUILD_ID) : client.guilds.cache.first(); if (!guild) { console.error("No guild found. Is the bot added to a server?"); process.exit(1); } console.log(`Connected to guild: ${guild.name} (${guild.id})`); // --- Ensure management channel exists --- const managementName = disclawConfig.management_channel; let existingWorkspace = db.getWorkspaceByName(managementName); if (existingWorkspace) { // Verify the channel still exists on Discord const existingChannel = guild.channels.cache.get(existingWorkspace.channel_id); if (existingChannel) { managementChannelId = existingWorkspace.channel_id; console.log(`Management channel found: #${managementName}`); } else { // Channel was deleted from Discord, recreate it existingWorkspace = undefined; } } if (!existingWorkspace) { // Look for an existing channel with that name first let mgmtChannel; const found = guild.channels.cache.find((ch) => ch.type === discord_js_1.ChannelType.GuildText && ch.name === managementName); if (found) { db.createWorkspace(found.id, guild.id, managementName, "__management__"); managementChannelId = found.id; mgmtChannel = found; console.log(`Adopted existing channel #${managementName}`); } else { const newChannel = await guild.channels.create({ name: managementName, type: discord_js_1.ChannelType.GuildText, topic: "DisClaw Management -- verwende /new-agent um Agenten zu erstellen", }); db.createWorkspace(newChannel.id, guild.id, managementName, "__management__"); managementChannelId = newChannel.id; mgmtChannel = newChannel; console.log(`Created management channel #${managementName}`); } // Send welcome message using the direct channel reference await mgmtChannel .send("DisClaw ist bereit. Verwende `/new-agent` um einen neuen Agenten zu erstellen.") .catch(() => { }); } // --- Register slash commands --- const rest = new discord_js_1.REST({ version: "10" }).setToken(envConfig.DISCORD_BOT_TOKEN); try { await rest.put(discord_js_1.Routes.applicationGuildCommands(client.user.id, guild.id), { body: [new_agent_1.newAgentCommand.toJSON()], }); console.log("Slash commands registered."); } catch (err) { console.error("Failed to register slash commands:", err); } }); // --- Event: interactionCreate (slash commands) --- client.on("interactionCreate", async (interaction) => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === "new-agent") { await (0, new_agent_1.handleNewAgent)(interaction, db, disclawConfig); } }); // --- Event: messageCreate (agent routing) --- client.on("messageCreate", async (message) => { // Ignore bot messages if (message.author.bot) return; // Only handle text channels in guilds if (!message.guild || message.channel.type !== discord_js_1.ChannelType.GuildText) return; await (0, router_1.routeMessage)(message, db, disclawConfig, managementChannelId); }); // --- Login --- await client.login(envConfig.DISCORD_BOT_TOKEN); return client; } //# sourceMappingURL=bot.js.map