mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -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).
147 lines
5.3 KiB
JavaScript
147 lines
5.3 KiB
JavaScript
/**
|
|
* @module commands/settings/utils
|
|
* @description Constants and pure helpers
|
|
* shared by the settings command modules. Bodies are verbatim from the
|
|
* original single-file command; `export ` prefixes and setValidLocales()
|
|
* are the only additions.
|
|
*/
|
|
|
|
|
|
import { EmbedBuilder } from "@fluxerjs/core";
|
|
import { getGlobalColor, cleanId } from "../../src/ui/index.mjs";
|
|
|
|
/**
|
|
* Command alias to setting key mapping.
|
|
* @type {Object.<string, string>}
|
|
*/
|
|
export const SHORTCUTS = {
|
|
prefix: "prefix",
|
|
pfx: "prefix",
|
|
"247": "stay_247",
|
|
};
|
|
|
|
/** @private @type {Set<string>} Strings that evaluate to boolean true. */
|
|
const BOOL_TRUE = new Set(["true", "1", "yes", "on", "enable", "enabled"]);
|
|
/** @private @type {Set<string>} Strings that evaluate to boolean false. */
|
|
const BOOL_FALSE = new Set(["false", "0", "no", "off", "disable", "disabled"]);
|
|
/** @private @type {Set<string>} Setting keys that use boolean display formatting. */
|
|
export const BOOL_SETTINGS = new Set(["songAnnouncements"]);
|
|
|
|
/** @private @type {Set<string>} Available locale codes, populated at load time. */
|
|
export let VALID_LOCALES = new Set(["en"]);
|
|
|
|
/** @private @type {number} Minimum allowed volume value. */
|
|
export const VOLUME_MIN = 1;
|
|
/** @private @type {number} Maximum allowed volume value. */
|
|
export const VOLUME_MAX = 200;
|
|
/** @private @type {number} Maximum prefix length in characters. */
|
|
export const PREFIX_MAX = 5;
|
|
/** @private @type {number} Maximum number of 24/7 channels per guild. */
|
|
export const MAX_247_CHANNELS = 1;
|
|
|
|
/**
|
|
* Replace the set of known locale codes (called by the settings command
|
|
* factory at load time). Importers read VALID_LOCALES as a live binding,
|
|
* so this stays behavior-identical to the original module-level assignment.
|
|
* @param {Iterable<string>} locales
|
|
*/
|
|
export function setValidLocales(locales) {
|
|
VALID_LOCALES = locales;
|
|
}
|
|
|
|
|
|
/**
|
|
* Check whether a string looks like a valid Fluxer ID (Snowflake-like).
|
|
* @param {string} id - The ID to validate.
|
|
* @returns {boolean} True if the ID is 15-22 characters after cleaning.
|
|
*/
|
|
export function isValidFluxerId(id) {
|
|
const cleaned = cleanId(id);
|
|
return cleaned.length >= 15 && cleaned.length <= 22;
|
|
}
|
|
/**
|
|
* Parse a string into a boolean, or return null if unrecognised.
|
|
* @param {string} str - The string to parse.
|
|
* @returns {boolean|null} True, false, or null if not a boolean string.
|
|
*/
|
|
export function parseBool(str) {
|
|
const s = String(str).toLowerCase().trim();
|
|
if (BOOL_TRUE.has(s)) return true;
|
|
if (BOOL_FALSE.has(s)) return false;
|
|
return null;
|
|
}
|
|
/**
|
|
* Format a setting value for display in embed messages.
|
|
* @param {string} key - The setting key.
|
|
* @param {*} value - The setting value.
|
|
* @returns {string} Formatted display string.
|
|
*/
|
|
export function displayValue(key, value) {
|
|
if (BOOL_SETTINGS.has(key)) return value ? "\u2705 enabled" : "\u274c disabled";
|
|
if (value === null || value === undefined || value === "none") return "none";
|
|
return "`" + value + "`";
|
|
}
|
|
/**
|
|
* @private
|
|
* Get the display name of the guild from a message object.
|
|
* @param {object} message - The command message wrapper.
|
|
* @returns {string} The guild name, or a fallback string.
|
|
*/
|
|
export function getGuildName(message) {
|
|
return message.message?.guild?.name ?? message.channel?.guild?.name ?? "this server";
|
|
}
|
|
/**
|
|
* @private
|
|
* Create a simple embed payload with global color, description, and optional title/footer.
|
|
* @param {string} desc - The embed description text.
|
|
* @param {object} [opts={}] - Optional overrides for title, iconURL, and footer.
|
|
* @returns {object} Embed payload for message.reply().
|
|
*/
|
|
export function embed(desc, opts = {}) {
|
|
const b = new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc);
|
|
if (opts.title) b.setTitle(opts.title);
|
|
if (opts.iconURL) b.setAuthor({ name: opts.title || "\u200b", iconURL: opts.iconURL });
|
|
if (opts.footer) b.setFooter({ text: opts.footer });
|
|
return { embeds: [b] };
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* Create a translation function bound to the given context and guild.
|
|
* @param {object} ctx - The bot (Remix) context.
|
|
* @param {string} guildId - The guild ID for localization.
|
|
* @returns {Function} A translation function with signature (key, data?) => string.
|
|
*/
|
|
export function tWrap(ctx, guildId) {
|
|
const tFn = ctx.locale?.translate?.bind(ctx.locale);
|
|
return (key, data = {}) => tFn ? tFn(guildId, key, data) : key;
|
|
}
|
|
/**
|
|
* @private
|
|
* Get a human-readable label for a setting key, using locale translations when available.
|
|
* @param {string} key - The setting key.
|
|
* @param {Function|null} t - Translation function (guildId, key, data) => string.
|
|
* @param {string} guildId - The guild ID for localization.
|
|
* @returns {string} The pretty-printed setting label.
|
|
*/
|
|
export function prettifySettingLabel(key, t, guildId) {
|
|
const localeMap = {
|
|
songAnnouncements: "responses.settings.labelSongAnnouncements",
|
|
prefix: "responses.settings.labelPrefix",
|
|
pfx: "responses.settings.labelPfp",
|
|
locale: "responses.settings.labelLocale",
|
|
stay_247: "responses.settings.label247",
|
|
volume: "responses.settings.labelVolume",
|
|
};
|
|
if (localeMap[key] && t && guildId) return t(guildId, localeMap[key]);
|
|
const fallback = {
|
|
songAnnouncements: "Song announcements",
|
|
prefix: "Prefix",
|
|
pfp: "Bot avatar style",
|
|
locale: "Locale",
|
|
stay_247: "24/7 mode",
|
|
volume: "Default volume",
|
|
};
|
|
return fallback[key] ?? key.replace(/_/g, " ");
|
|
}
|