Replace 12 dynamic `await import()` calls with domain events emitted through the existing systemEvents bus, breaking circular dependencies between services (economy/inventory/leveling -> quest, * -> dashboard). - Add `emitAsync` to SystemEventEmitter for sequential listener awaiting, preserving DB transaction atomicity for quest progress tracking - Add DOMAIN event constants (BALANCE_CHANGED, XP_GAINED, ITEM_COLLECTED, ITEM_USED, TRANSFER_COMPLETED, DAILY_CLAIMED, TRIVIA_*, EXAM_PASSED) - Create shared/lib/eventWiring.ts to register all domain event listeners - Convert quest event calls to `await systemEvents.emitAsync()` (5 calls) - Convert dashboard event calls to `systemEvents.emit()` fire-and-forget (5 calls) - Convert exam.service.ts userService import to static import (1 call) - Convert dashboard.service.ts events import to static import (1 call) - Leave inventory.service.ts validateAndExecuteEffect import unchanged (Task 3) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { AuroraClient } from "@/lib/BotClient";
|
|
import { env } from "@shared/lib/env";
|
|
import { join } from "node:path";
|
|
import { initializeConfig } from "@shared/lib/config";
|
|
import { registerDomainEventListeners } from "@shared/lib/eventWiring";
|
|
|
|
import { startWebServerFromRoot } from "../api/src/server";
|
|
|
|
// Initialize config from database
|
|
await initializeConfig();
|
|
|
|
// Register domain event listeners before loading commands/events
|
|
registerDomainEventListeners();
|
|
|
|
// Load commands & events
|
|
await AuroraClient.loadCommands();
|
|
await AuroraClient.loadEvents();
|
|
await AuroraClient.deployCommands();
|
|
await AuroraClient.setupSystemEvents();
|
|
|
|
console.log("🌐 Starting web server...");
|
|
|
|
let shuttingDown = false;
|
|
|
|
const webProjectPath = join(import.meta.dir, "../api");
|
|
const webPort = Number(process.env.WEB_PORT) || 3000;
|
|
const webHost = process.env.HOST || "0.0.0.0";
|
|
|
|
// Start web server in the same process
|
|
const webServer = await startWebServerFromRoot(webProjectPath, {
|
|
port: webPort,
|
|
hostname: webHost,
|
|
});
|
|
|
|
// login with the token from .env
|
|
if (!env.DISCORD_BOT_TOKEN) {
|
|
throw new Error("❌ DISCORD_BOT_TOKEN is not set in environment variables.");
|
|
}
|
|
AuroraClient.login(env.DISCORD_BOT_TOKEN);
|
|
|
|
// Handle graceful shutdown
|
|
const shutdownHandler = async () => {
|
|
if (shuttingDown) return;
|
|
shuttingDown = true;
|
|
console.log("🛑 Shutdown signal received. Stopping services...");
|
|
|
|
// Stop web server
|
|
await webServer.stop();
|
|
|
|
// Stop bot
|
|
AuroraClient.shutdown();
|
|
|
|
process.exit(0);
|
|
};
|
|
|
|
process.on("SIGINT", shutdownHandler);
|
|
process.on("SIGTERM", shutdownHandler); |