mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
Remix — the complete rewrite
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).
This commit is contained in:
1 parent
0f085a6ae4
commit
253d11cd57
150 files changed
+19935
-16894
No files matched your search
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @module src/commands/CommandLoader
|
||||
* @description Dynamically loads command modules from a directory, registers
|
||||
* their builders, and binds run handlers with error capture + error-id replies.
|
||||
*/
|
||||
|
||||
import { pathToFileURL } from "node:url";
|
||||
import path from "node:path";
|
||||
import * as fs from "node:fs";
|
||||
import { logger } from "../core/Logger.mjs";
|
||||
import { Utils } from "../utils/Utils.mjs";
|
||||
|
||||
/**
|
||||
* @class CommandLoader
|
||||
* @description Loads command files from a directory, registers builders with
|
||||
* the CommandHandler, and dispatches "run" events to the matching run function.
|
||||
*
|
||||
* Command module shape (both shapes are accepted):
|
||||
* - `export const command` (builder or builder-factory), `export const run`
|
||||
* - default export object with the same keys
|
||||
* - optional `exportDef: { name, object }` registers a global on the context
|
||||
*/
|
||||
export class CommandLoader {
|
||||
/** @type {import('./CommandHandler.mjs').CommandHandler} */
|
||||
commands;
|
||||
/** @type {Map<string, string>} Command UID → file path. */
|
||||
commandFiles = new Map();
|
||||
/** @type {Map<string, Function>} Command UID → run function. */
|
||||
runnables = new Map();
|
||||
/** @type {object} Shared context (the bot instance). */
|
||||
context;
|
||||
|
||||
/**
|
||||
* @param {import('./CommandHandler.mjs').CommandHandler} commands
|
||||
* @param {object} context - Bot context object (gets loader/runnables refs attached).
|
||||
*/
|
||||
constructor(commands, context) {
|
||||
this.commands = commands;
|
||||
this.context = context;
|
||||
this.context.loader ??= this;
|
||||
this.context.commandFiles = this.commandFiles;
|
||||
this.context.runnables = this.runnables;
|
||||
|
||||
this.commands.on("run", (data) => {
|
||||
if (!this.runnables.has(data.command.uid)) return;
|
||||
const runFc = this.runnables.get(data.command.uid);
|
||||
const sendErrorReply = (id) => {
|
||||
Promise.resolve(data.message.reply("An error occurred. Error id: `#" + id + "`"))
|
||||
.catch(err => logger.warn("[CommandHandler] Failed to send error-reply; error id #" + id + ":", err?.message));
|
||||
};
|
||||
try {
|
||||
const result = runFc.call(this.context, data.message, data);
|
||||
if (result && typeof result.catch === "function") {
|
||||
result.catch(e => {
|
||||
const id = Utils.uid();
|
||||
logger.error("Error running command; error id #" + id, e);
|
||||
sendErrorReply(id);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
const id = Utils.uid();
|
||||
logger.error("Error running command; error id #" + id, e);
|
||||
sendErrorReply(id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a module export to `{ command, run, exportDef }`.
|
||||
* @param {object} cData
|
||||
* @returns {object}
|
||||
*/
|
||||
canonData(cData) {
|
||||
if (cData.command !== undefined) {
|
||||
return {
|
||||
command: cData.command,
|
||||
run: cData.run,
|
||||
exportDef: cData.exportDef ?? cData.export ?? null
|
||||
};
|
||||
}
|
||||
const d = cData.default ?? cData;
|
||||
return {
|
||||
command: d.command,
|
||||
run: d.run,
|
||||
exportDef: d.exportDef ?? d.export ?? null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @async Load all command files from a directory.
|
||||
* @param {string} dir
|
||||
* @returns {Promise<undefined[]>}
|
||||
*/
|
||||
loadFromDir(dir) {
|
||||
const files = fs.readdirSync(dir)
|
||||
.filter(f => !f.startsWith(".") && (f.endsWith(".js") || f.endsWith(".mjs")));
|
||||
|
||||
return Promise.all(files.map(async commandFile => {
|
||||
const file = path.join(dir, commandFile);
|
||||
|
||||
const cData = this.canonData(await import(pathToFileURL(file).href));
|
||||
|
||||
const builder = (typeof cData.command === "function")
|
||||
? cData.command.call(this.context)
|
||||
: cData.command;
|
||||
|
||||
if (!builder) return logger.warn("No builder returned. Skipping '" + commandFile + "'");
|
||||
|
||||
if (cData.exportDef)
|
||||
this.context[cData.exportDef.name] = cData.exportDef.object;
|
||||
|
||||
this.commands.addCommand(builder);
|
||||
this.commandFiles.set(builder.uid, file);
|
||||
|
||||
if (!cData.run) return;
|
||||
|
||||
this.runnables.set(builder.uid, cData.run);
|
||||
builder.subcommands.forEach(sub => {
|
||||
this.runnables.set(sub.uid, cData.run);
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user