palico-bot/index.js
Shinuwa 9fb54798dd Keep the queue across restarts; add shuffle, loop, renaming and live events
- Save the queue to storage/runtime/queue.json and restore it at startup,
  the interrupted track first; playback resumes once someone is in the
  voice channel. Shutdown keeps the queued files instead of deleting them.
- Shuffle the upcoming tracks; loop mode sends finished tracks back to the
  end of the queue, except removed or failed ones.
- Rename library tracks (queued copies follow), enqueue the whole library,
  optionally shuffled.
- Stream queue and library changes to the web page with Server-Sent Events
  (GET /api/events).
- Sanitise the requester name sent when queueing a library track.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:15:25 +01:00

98 lines
3 KiB
JavaScript

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 { 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();
const foldersPath = path.join(__dirname, "src/commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
if (folder.indexOf("[disabled]") === -1 || config.environnement === "test") {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const commandFile = await import(filePath);
if ("data" in commandFile.command && "execute" in commandFile.command) {
client.commands.set(commandFile.command.data.name, commandFile);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
}
}
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);
});
}
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 };