palico-bot/index.js
Shinuwa 3795ee06cb Add file headers, tidy the stylesheet and document the conventions
Headers:
- Every JavaScript file (sources, front, scripts, tests) starts with a
  /** @file … */ header, in the three levels used by sokko-g: a short
  description for simple files; Utilisé par / Attention / @see for
  shared modules; a full header (context, entry points, callers,
  dependencies, pitfalls) for index.js, the music queue, the web
  server, the storage manager and the jukebox page script.
- No @author, @version or @since: git already knows.
- test/fileHeaders.test.js fails `npm run check` when a header is
  missing or a central file's header is incomplete.

Stylesheet (no visual change, checked by comparing every element's
computed styles in Chromium at 1280, 800 and 400 px, hover included):
- Role header and section comments.
- Transparent shades derive from the colour variables with
  color-mix() instead of copied rgba() values; new tokens for repeated
  values (text, text on accent, accent gradient, white veils, shadow).
- Native nesting for buttons, the live indicator, upload rows and
  home cards; drop the unused .hero__visual--large and redundant rules.
- Utility classes move to the end so they win over components. The
  library list's u-mt-1, which never applied (.list reset it), is
  removed to keep the layout as it was.

Docs: CONTRIBUTING.md describes the language, header and stylesheet
conventions; CLAUDE.md points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 21:05:59 +01:00

97 lines
3.2 KiB
JavaScript

/**
* @file Point d'entrée du bot : connecte le client Discord, branche les commandes et les événements,
* crée la file du jukebox et démarre le serveur web.
*
* Contexte : lancé par `npm start` ; un seul processus sert le bot et le site du jukebox.
* Points d'entrée : exporte `client` et `musicQueue` ; arrêt propre sur SIGTERM et SIGINT.
* Utilisé par : `npm start` (package.json).
* Dépend de : loadCommands.js, src/events/, music/queue.js, music/queuePersistence.js, web/server.js.
* Attention :
* - Les commandes trouvent la file par `interaction.client.musicQueue`.
* - À l'arrêt, les flux en direct des pages ouvertes sont coupés de force : sinon le serveur HTTP
* ne se ferme jamais.
* @see README.md#déploiement
*/
import { Client, Collection, GatewayIntentBits } from "discord.js";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { config } from "./src/config.js";
import { loadCommands } from "./src/loadCommands.js";
import { MusicQueue } from "./src/music/queue.js";
import { createQueuePersistence } from "./src/music/queuePersistence.js";
import { startWebServer } from "./src/web/server.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
});
client.commands = new Collection(
(await loadCommands()).map((command) => [command.data.name, command])
);
const eventsPath = path.join(__dirname, "src/events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const eventFile = await import(filePath);
if (eventFile.event.once) {
client.once(eventFile.event.name, (...args) =>
eventFile.event.execute(...args)
);
} else {
client.on(eventFile.event.name, (...args) =>
eventFile.event.execute(...args)
);
}
}
const musicQueue = new MusicQueue(client, {
persistence: createQueuePersistence(
path.join(process.cwd(), "storage", "runtime", "queue.json")
),
});
// Les commandes du jukebox (/play, /skip, /queue) y accèdent par interaction.client.
client.musicQueue = musicQueue;
await client.login(config.token);
const { server } = startWebServer({ queue: musicQueue, client });
server.on("error", (error) => {
console.error("HTTP server error", error);
});
let shuttingDown = false;
async function shutdown(signal) {
if (shuttingDown) return;
shuttingDown = true;
console.log(`[${new Date().toLocaleString()}] Shutting down (${signal})`);
musicQueue.shutdown();
client.destroy();
const forceExit = setTimeout(() => process.exit(1), 10_000);
forceExit.unref();
server.close(() => {
clearTimeout(forceExit);
});
// close() n'attend que la fin des requêtes en cours : les flux en direct (/api/events) des
// pages jukebox ouvertes ne finissent jamais d'eux-mêmes et bloqueraient l'arrêt.
server.closeAllConnections();
}
process.once("SIGTERM", () => void shutdown("SIGTERM"));
process.once("SIGINT", () => void shutdown("SIGINT"));
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection", error);
});
export { client, musicQueue };