Initial commit: Gaming Hub foundation

Plugin-based Discord bot framework with web frontend:
- Core: Discord.js client, SSE broadcast, JSON persistence
- Plugin system: lifecycle hooks (init, onReady, routes, snapshot, destroy)
- Web: React 19 + Vite 6 + TypeScript, tab-based navigation
- Docker: multi-stage build (Node 24, static ffmpeg, yt-dlp)
- GitLab CI: Kaniko with LAN registry caching

Ready for plugin development.
This commit is contained in:
Claude Code 2026-03-05 22:52:13 +01:00
parent 1ae431dd2f
commit ae1c41f0ae
19 changed files with 954 additions and 0 deletions

136
server/src/index.ts Normal file
View file

@ -0,0 +1,136 @@
import express from 'express';
import path from 'node:path';
import client from './core/discord.js';
import { addSSEClient, removeSSEClient, sseBroadcast, getSSEClientCount } from './core/sse.js';
import { loadState, getFullState } from './core/persistence.js';
import { getPlugins, registerPlugin, PluginContext } from './core/plugin.js';
// ── Config ──
const PORT = Number(process.env.PORT ?? 8080);
const DATA_DIR = process.env.DATA_DIR ?? '/data';
const DISCORD_TOKEN = process.env.DISCORD_TOKEN ?? '';
// ── Persistence ──
loadState();
// ── Express ──
const app = express();
app.use(express.json());
app.use(express.static(path.join(import.meta.dirname ?? __dirname, '..', '..', 'web', 'dist')));
// ── Plugin Context ──
const ctx: PluginContext = { client, dataDir: DATA_DIR };
// ── SSE Events ──
app.get('/api/events', (_req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
res.flushHeaders();
// Send snapshot from all plugins
const snapshot: Record<string, any> = { type: 'snapshot' };
for (const p of getPlugins()) {
if (p.getSnapshot) {
Object.assign(snapshot, p.getSnapshot(ctx));
}
}
try { res.write(`data: ${JSON.stringify(snapshot)}\n\n`); } catch {}
const ping = setInterval(() => { try { res.write(':\n\n'); } catch {} }, 15_000);
addSSEClient(res);
_req.on('close', () => {
removeSSEClient(res);
clearInterval(ping);
try { res.end(); } catch {}
});
});
// ── Health ──
app.get('/api/health', (_req, res) => {
res.json({
status: 'ok',
uptime: process.uptime(),
plugins: getPlugins().map(p => ({ name: p.name, version: p.version })),
sseClients: getSSEClientCount(),
});
});
// ── API: List plugins ──
app.get('/api/plugins', (_req, res) => {
res.json(getPlugins().map(p => ({
name: p.name,
version: p.version,
description: p.description,
})));
});
// ── SPA Fallback ──
app.get('*', (_req, res) => {
res.sendFile(path.join(import.meta.dirname ?? __dirname, '..', '..', 'web', 'dist', 'index.html'));
});
// ── Discord Ready ──
client.once('ready', async () => {
console.log(`[Discord] Logged in as ${client.user?.tag}`);
console.log(`[Discord] Serving ${client.guilds.cache.size} guild(s)`);
for (const p of getPlugins()) {
if (p.onReady) {
try { await p.onReady(ctx); } catch (e) { console.error(`[Plugin:${p.name}] onReady error:`, e); }
}
}
});
// ── Init Plugins ──
async function boot(): Promise<void> {
// --- Load plugins dynamically here ---
// Example: import('./plugins/soundboard/index.js').then(m => registerPlugin(m.default));
// Init all plugins
for (const p of getPlugins()) {
try {
await p.init(ctx);
p.registerRoutes?.(app, ctx);
console.log(`[Plugin:${p.name}] Initialized`);
} catch (e) {
console.error(`[Plugin:${p.name}] Init error:`, e);
}
}
// Start Express
app.listen(PORT, () => console.log(`[HTTP] Listening on :${PORT}`));
// Login Discord
if (DISCORD_TOKEN) {
await client.login(DISCORD_TOKEN);
} else {
console.warn('[Discord] No DISCORD_TOKEN set - running without Discord');
}
}
// ── Graceful Shutdown ──
async function shutdown(signal: string): Promise<void> {
console.log(`\n[${signal}] Shutting down...`);
for (const p of getPlugins()) {
if (p.destroy) {
try { await p.destroy(ctx); } catch (e) { console.error(`[Plugin:${p.name}] destroy error:`, e); }
}
}
client.destroy();
process.exit(0);
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('uncaughtException', (err) => { console.error('Uncaught:', err); });
process.on('unhandledRejection', (err) => { console.error('Unhandled:', err); });
process.on('warning', (w) => {
if (w.name === 'TimeoutNegativeWarning') return;
console.warn(w.name + ': ' + w.message);
});
boot().catch(console.error);