mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
Upgrades the bot from fluxerjs 2.2 to 3.0 and restructures everything into a layered architecture (~28K lines): Dependencies: - @fluxerjs/core, util, voice: ^2.2.0 -> ^3.0.0 (typed events, PartialMessage payloads, camelCase API) - @fluxerjs/sharding ^3.0.0 added — built-in sharding mode (shard.mjs, one process per shard) - @discordjs/opus dropped — opusscript + prism-media cover encoding Architecture: - commands: every command split into submodules (player, lastfm, settings, debug) with a shared CommandBuilder/Loader/Handler core - ui: embeds, paginators, help command, permission checks - voice: gateway event routing, rejoin/247 boot recovery, state resolver - music: player split into Queue/Search/Playback/Display mixins, stream pipeline + WebM/Opus muxer (no FFmpeg needed) - dashboard: Redis pub/sub, RPC handlers, serializers - db: MySQL manager + per-guild settings - services: LastFm manager split into focused mixins - utils: sharding, 24/7 helpers, API client Runtime fixes over v2: - Redis v5 reconnect: capped numeric backoff (0.5s ramp to 15s), never exhausts, auto-recovers when the server comes back - Redis main/subscriber clients connect in parallel; clean shutdown - Logger storm guard: identical error/warn lines deduped in a 60s window - self-deaf fix on voice join - config example uses redis://127.0.0.1 (avoids IPv6 ENETUNREACH) Housekeeping: dead code removed (Tuna.mjs + follow-redirects dep), .gitignore added (config.json, .env, node_modules, runtime state).
143 lines
4.7 KiB
JavaScript
143 lines
4.7 KiB
JavaScript
/**
|
|
* @module index
|
|
* @description Entry point for the Remix music bot. Creates the {@link Remix}
|
|
* instance, sets up process-level error handlers, and registers signal-based
|
|
* graceful shutdown. All boot logic lives in src/core/Bot.mjs.
|
|
*/
|
|
|
|
import { logger } from "./src/core/Logger.mjs";
|
|
import Remix from "./src/core/Bot.mjs";
|
|
|
|
const remix = new Remix();
|
|
|
|
/**
|
|
* Check whether an error is a known-ignorable WebSocket transport crash from
|
|
* the fluxer.js ws or undici internals.
|
|
* @param {Error} err - The error to check.
|
|
* @returns {boolean} True if the error should be silently recovered.
|
|
*/
|
|
const isIgnorableWsCrash = (err) => {
|
|
const message = String(err?.message ?? err ?? "");
|
|
const stack = String(err?.stack ?? "");
|
|
return message === "WebSocket error" &&
|
|
(
|
|
stack.includes("@fluxerjs/ws/dist/index.mjs") ||
|
|
stack.includes("node:internal/deps/undici/undici")
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Check whether an error is the benign LiveKit "AudioSource is closed" race.
|
|
* Happens when a track is skipped/stopped while @fluxerjs/voice's internal
|
|
* WebM demuxer still has queued frames to push into the just-closed audio
|
|
* source. Harmless by itself — but if it ever surfaces as a synchronous
|
|
* uncaughtException it must NOT take the whole bot down.
|
|
* @param {Error} err - The error to check.
|
|
* @returns {boolean} True if the error is the audio stop-race.
|
|
*/
|
|
const isBenignAudioStopRace = (err) =>
|
|
String(err?.message ?? err ?? "").includes("AudioSource is closed");
|
|
|
|
/**
|
|
* @param {Error} err - The error to check.
|
|
* @returns {boolean} True if the error is the stream abort race.
|
|
*/
|
|
const isBenignStreamAbortRace = (err) =>
|
|
err?.code === "ECONNRESET" && String(err?.message ?? "").includes("aborted");
|
|
|
|
let _lastWsCrashLog = 0;
|
|
let _lastAudioRaceLog = 0;
|
|
const WS_CRASH_LOG_COOLDOWN = 30_000;
|
|
|
|
process.on("unhandledRejection", (reason, p) => {
|
|
if (reason?.message?.includes("AudioSource is closed")) return;
|
|
logger.error("[Error_Handling] Unhandled Rejection/Catch");
|
|
logger.error("Reason:", reason, p);
|
|
});
|
|
|
|
process.on("uncaughtException", (err, origin) => {
|
|
if (isIgnorableWsCrash(err)) {
|
|
const now = Date.now();
|
|
if (now - _lastWsCrashLog > WS_CRASH_LOG_COOLDOWN) {
|
|
_lastWsCrashLog = now;
|
|
logger.warn("[Error_Handling] Suppressed recoverable websocket transport crash (will not re-log for 30s).");
|
|
}
|
|
return;
|
|
}
|
|
if (isBenignAudioStopRace(err)) {
|
|
const now = Date.now();
|
|
if (now - _lastAudioRaceLog > WS_CRASH_LOG_COOLDOWN) {
|
|
_lastAudioRaceLog = now;
|
|
logger.warn("[Error_Handling] Suppressed benign audio stop-race (AudioSource is closed) — track was skipped/stopped mid-frame.");
|
|
}
|
|
return;
|
|
}
|
|
if (isBenignStreamAbortRace(err)) {
|
|
const now = Date.now();
|
|
if (now - _lastAudioRaceLog > WS_CRASH_LOG_COOLDOWN) {
|
|
_lastAudioRaceLog = now;
|
|
logger.warn("[Error_Handling] Suppressed benign stream abort race (aborted/ECONNRESET) — loadstream was destroyed mid-transfer.");
|
|
}
|
|
return;
|
|
}
|
|
logger.error("[Error_Handling] Uncaught Exception/Catch");
|
|
logger.error("Error:", err, origin);
|
|
process.exit(1);
|
|
});
|
|
|
|
process.on("uncaughtExceptionMonitor", (err, origin) => {
|
|
if (isIgnorableWsCrash(err)) return;
|
|
if (isBenignAudioStopRace(err)) return;
|
|
if (isBenignStreamAbortRace(err)) return;
|
|
logger.error("[Error_Handling] Uncaught Exception/Catch (MONITOR)");
|
|
logger.error("Error:", err, origin);
|
|
});
|
|
|
|
/**
|
|
* Graceful shutdown handler: destroys all active players, closes Lavalink,
|
|
* Redis, and Dashboard DB connections, then exits.
|
|
* @async
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const saveAndExit = async () => {
|
|
logger.recovery("\n[Shutdown] Cleaning up before exit...");
|
|
try {
|
|
if (remix.players?.playerMap) {
|
|
for (const [channelId, player] of remix.players.playerMap) {
|
|
try { player.destroy(); } catch (e) { logger.warn("[Shutdown] Player destroy error:", e?.message); }
|
|
}
|
|
remix.players.playerMap.clear();
|
|
}
|
|
} catch (e) {
|
|
logger.warn("[Shutdown] Player cleanup error:", e?.message);
|
|
}
|
|
try {
|
|
if (remix.lavalink) {
|
|
remix.lavalink.destroy();
|
|
}
|
|
} catch (e) {
|
|
logger.warn("[Shutdown] Lavalink cleanup error:", e?.message);
|
|
}
|
|
try {
|
|
if (remix.dashboard?.redis?.destroy) {
|
|
await remix.dashboard.redis.destroy();
|
|
}
|
|
} catch (e) {
|
|
logger.error("[Shutdown] Failed to close Redis:", e.message);
|
|
}
|
|
try {
|
|
if (remix.dashboard?.db?.close) {
|
|
await remix.dashboard.db.close();
|
|
}
|
|
} catch (e) {
|
|
logger.error("[Shutdown] Failed to close Dashboard DB:", e.message);
|
|
}
|
|
process.exit(0);
|
|
};
|
|
|
|
process.once("SIGINT", saveAndExit);
|
|
process.once("SIGTERM", saveAndExit);
|
|
process.once("SIGUSR2", saveAndExit);
|
|
|
|
process.on("SIGPIPE", () => {});
|