mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -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
+15
-448
@@ -1,455 +1,22 @@
|
||||
/**
|
||||
* @module commands/settings
|
||||
* @description Server settings management with subcommands (get/set/reset/help) and shortcut aliases (prefix, 247).
|
||||
* @description Server settings management with subcommands (get/set/reset/help) and
|
||||
* shortcut aliases (prefix, 247). The helpers live in commands/settings/ (utils,
|
||||
* channels247, setters); the command factory and run() dispatch stay here.
|
||||
*/
|
||||
|
||||
import { CommandBuilder } from "../src/CommandHandler.mjs";
|
||||
import { EmbedBuilder } from "@fluxerjs/core";
|
||||
import { getGlobalColor, cleanId, getMessageGuildId } from "../src/MessageHandler.mjs";
|
||||
import { logger } from "../src/constants/Logger.mjs";
|
||||
import runnables from "../settings/runnables.mjs";
|
||||
|
||||
/**
|
||||
* Command alias to setting key mapping.
|
||||
* @type {Object.<string, string>}
|
||||
*/
|
||||
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. */
|
||||
const BOOL_SETTINGS = new Set(["songAnnouncements"]);
|
||||
|
||||
/** @private @type {Set<string>} Available locale codes, populated at load time. */
|
||||
let VALID_LOCALES = new Set(["en"]);
|
||||
|
||||
/** @private @type {number} Minimum allowed volume value. */
|
||||
const VOLUME_MIN = 1;
|
||||
/** @private @type {number} Maximum allowed volume value. */
|
||||
const VOLUME_MAX = 200;
|
||||
/** @private @type {number} Maximum prefix length in characters. */
|
||||
const PREFIX_MAX = 5;
|
||||
/** @private @type {number} Maximum number of 24/7 channels per guild. */
|
||||
const MAX_247_CHANNELS = 1;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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().
|
||||
*/
|
||||
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] };
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the stay_247 setting into a Set of validated channel IDs.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @returns {Set<string>} Set of cleaned, valid channel IDs.
|
||||
*/
|
||||
function get247Channels(set) {
|
||||
const raw = set.get("stay_247");
|
||||
if (!raw || raw === "none") return new Set();
|
||||
if (typeof raw === "string") {
|
||||
const id = cleanId(raw);
|
||||
return (id && isValidFluxerId(id)) ? new Set([id]) : new Set();
|
||||
}
|
||||
if (Array.isArray(raw)) {
|
||||
return new Set(raw.map(id => cleanId(id)).filter(id => id && isValidFluxerId(id)));
|
||||
}
|
||||
return new Set();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a Set of channel IDs back to the stay_247 setting.
|
||||
* Writes 'none' if the set is empty.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {Set<string>} channels - The channel IDs to save.
|
||||
*/
|
||||
function save247Channels(set, channels) {
|
||||
const arr = [...channels].filter(id => id && isValidFluxerId(id));
|
||||
set.set("stay_247", arr.length > 0 ? arr : "none");
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Resolve a channel ID to a human-readable channel name from the client cache.
|
||||
* @param {import('@fluxerjs/core').Client} client - The Discord/Fluxer client.
|
||||
* @param {string} channelId - The channel ID to look up.
|
||||
* @returns {string|null} The channel name, or null if not found.
|
||||
*/
|
||||
function resolveChannelName(client, channelId) {
|
||||
try {
|
||||
const ch = client?.channels?.get?.(channelId);
|
||||
if (ch?.name) return ch.name;
|
||||
} catch (_) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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, " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an embed payload showing the current 24/7 status for the guild.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {string} guildId - The guild ID.
|
||||
* @param {string|null} channelId - The channel ID (for active indicator), or null.
|
||||
* @returns {object} Embed payload object for message.reply().
|
||||
*/
|
||||
function build247Panel(set, ctx, guildId, channelId) {
|
||||
const channels = [...get247Channels(set)];
|
||||
const prefix = ctx.handler.getPrefix(guildId);
|
||||
|
||||
if (channels.length > 0) {
|
||||
const lines = channels.map(id => {
|
||||
const name = resolveChannelName(ctx.client, id);
|
||||
const isActive = id === channelId;
|
||||
return (isActive ? "\u25b6 " : "\u2022 ") + (name ? "**" + name + "** " : "") + "<#" + id + ">";
|
||||
});
|
||||
return embed(
|
||||
channels.length === 1 && channelId
|
||||
? "\u2705 24/7 is now **enabled** in <#" + channelId + ">\n\n" +
|
||||
"The bot will stay connected and auto-rejoin if disconnected."
|
||||
: "\u2705 24/7 active in " + channels.length + " channel(s):\n\n" + lines.join("\n"),
|
||||
{ title: "\u2705 24/7 Mode" }
|
||||
);
|
||||
}
|
||||
|
||||
return embed(
|
||||
"\u274c 24/7 is **disabled**\n\n" +
|
||||
"The bot will leave voice channels when idle.\n\n" +
|
||||
"Join a voice channel and use `" + prefix + "247` to enable.",
|
||||
{ title: "\u274c 24/7 Mode" }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable 24/7 mode for a specific channel.
|
||||
* Removes the channel from stay_247, marks intentional leave, and destroys the player.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {string} guildId - The guild ID.
|
||||
* @param {string} channelId - The channel ID to disable.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function disable247(ctx, set, guildId, channelId) {
|
||||
const id = cleanId(channelId);
|
||||
const channels = get247Channels(set);
|
||||
channels.delete(id);
|
||||
save247Channels(set, channels);
|
||||
ctx.markIntentionalLeave?.(id);
|
||||
const player = ctx.players.playerMap.get(id)
|
||||
?? [...ctx.players.playerMap.values()].find(p =>
|
||||
cleanId(p?._channelId ?? "") === id &&
|
||||
cleanId(p?._guildId ?? "") === cleanId(guildId)
|
||||
);
|
||||
if (player) {
|
||||
const activeId = cleanId(player._channelId ?? id);
|
||||
ctx.players.playerMap.delete(activeId);
|
||||
if (activeId !== id) ctx.players.playerMap.delete(id);
|
||||
await player.leave().catch(() => {});
|
||||
player.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable 24/7 mode for a specific channel.
|
||||
* Enforces MAX_247_CHANNELS (1) by disabling any existing 24/7 channel first,
|
||||
* then saves the new channel and spawns a player if none exists.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {string} guildId - The guild ID.
|
||||
* @param {string} channelId - The channel ID to enable.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function enable247(ctx, set, guildId, channelId) {
|
||||
const id = cleanId(channelId);
|
||||
const channels = get247Channels(set);
|
||||
if (channels.has(id)) return;
|
||||
|
||||
// Platform only supports 1 voice channel per bot per guild.
|
||||
// If another channel already has 24/7, disable it first.
|
||||
if (channels.size >= MAX_247_CHANNELS) {
|
||||
for (const oldId of channels) {
|
||||
if (oldId !== id) {
|
||||
ctx.markIntentionalLeave?.(oldId);
|
||||
const oldPlayer = ctx.players.playerMap.get(oldId)
|
||||
?? [...ctx.players.playerMap.values()].find(p =>
|
||||
cleanId(p?._channelId ?? "") === oldId &&
|
||||
cleanId(p?._guildId ?? "") === cleanId(guildId)
|
||||
);
|
||||
if (oldPlayer) {
|
||||
const activeId = cleanId(oldPlayer._channelId ?? oldId);
|
||||
ctx.players.playerMap.delete(activeId);
|
||||
if (activeId !== oldId) ctx.players.playerMap.delete(oldId);
|
||||
await oldPlayer.leave().catch(() => {});
|
||||
oldPlayer.destroy();
|
||||
}
|
||||
channels.delete(oldId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
channels.add(id);
|
||||
save247Channels(set, channels);
|
||||
const playerExists = ctx.players.playerMap.has(id) ||
|
||||
[...ctx.players.playerMap.values()].some(p =>
|
||||
cleanId(p?._channelId ?? "") === id && cleanId(p?._guildId ?? "") === cleanId(guildId)
|
||||
);
|
||||
if (!playerExists) {
|
||||
try { await ctx._spawnPlayer(guildId, id); } catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the !247 toggle command. If the user is in a 24/7 channel, disable it;
|
||||
* otherwise, enable it for the user's current voice channel.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {object} message - The command message.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {string} guildId - The guild ID.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function handle247Toggle(ctx, message, set, guildId) {
|
||||
const loc = tWrap(ctx, guildId);
|
||||
if (!guildId) return message.reply(embed(loc("responses.settings.noServer")));
|
||||
|
||||
const { channelId: userChannelId } = await ctx.players.checkVoiceChannels(message);
|
||||
if (!userChannelId) {
|
||||
return message.reply(build247Panel(set, ctx, guildId, null));
|
||||
}
|
||||
|
||||
const id = cleanId(userChannelId);
|
||||
const channels = get247Channels(set);
|
||||
|
||||
if (channels.has(id)) {
|
||||
await disable247(ctx, set, guildId, userChannelId);
|
||||
return message.reply(build247Panel(set, ctx, guildId, id));
|
||||
}
|
||||
|
||||
const result = await enable247(ctx, set, guildId, userChannelId);
|
||||
if (result?.max) {
|
||||
return message.reply(embed(
|
||||
loc("responses.settings.max247Channels", { max: MAX_247_CHANNELS, prefix: ctx.handler.getPrefix(guildId) })
|
||||
));
|
||||
}
|
||||
|
||||
return message.reply(build247Panel(set, ctx, guildId, id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a new value to a setting, with type-specific validation.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {object} message - The command message.
|
||||
* @param {ServerSettings} set - The guild settings instance.
|
||||
* @param {string} key - The setting key.
|
||||
* @param {string} rawValue - The raw string value from the command.
|
||||
* @returns {Promise<string|null>} Error message string, or null on success.
|
||||
*/
|
||||
async function applySet(ctx, message, set, key, rawValue) {
|
||||
if (BOOL_SETTINGS.has(key)) {
|
||||
const bool = parseBool(rawValue);
|
||||
if (bool === null) {
|
||||
return ctx.t(message, "responses.settings.mustBeBool", { setting: key });
|
||||
}
|
||||
set.set(key, bool);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (key === "volume") {
|
||||
const num = parseInt(rawValue, 10);
|
||||
if (isNaN(num) || num < VOLUME_MIN || num > VOLUME_MAX) {
|
||||
return ctx.t(message, "responses.settings.volumeRange");
|
||||
}
|
||||
set.set(key, num);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (key === "locale") {
|
||||
if (!VALID_LOCALES.has(rawValue)) {
|
||||
return ctx.t(message, "responses.settings.invalidLocale", {
|
||||
locale: rawValue,
|
||||
locales: [...VALID_LOCALES].map(l => "`" + l + "`").join(", ")
|
||||
});
|
||||
}
|
||||
set.set(key, rawValue);
|
||||
const gid = getMessageGuildId(message);
|
||||
if (gid) ctx.locale.invalidateCache(gid);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (key === "prefix") {
|
||||
if (!rawValue || rawValue.length > PREFIX_MAX) {
|
||||
return ctx.t(message, "responses.settings.prefixLength");
|
||||
}
|
||||
if (/\s/.test(rawValue)) {
|
||||
return ctx.t(message, "responses.settings.prefixSpaces");
|
||||
}
|
||||
}
|
||||
|
||||
if (runnables[key]) {
|
||||
const err = runnables[key].call(ctx, rawValue, { msg: message });
|
||||
if (err) return "\u274c " + err;
|
||||
}
|
||||
|
||||
set.set(key, rawValue);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @async
|
||||
* Handle a settings shortcut command (e.g. `prefix`, `247`).
|
||||
* Dispatches to 24/7 toggle, value display, or value set depending on arguments.
|
||||
* @param {object} ctx - The bot (Remix) context.
|
||||
* @param {object} message - The command message.
|
||||
* @param {string} settingKey - The resolved setting key.
|
||||
* @param {string[]} valueTokens - Remaining argument tokens after the shortcut alias.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function handleShortcut(ctx, message, settingKey, valueTokens) {
|
||||
const set = ctx.getSettings(message);
|
||||
const guildId = getMessageGuildId(message);
|
||||
const t247 = ctx.locale?.translate?.bind(ctx.locale);
|
||||
|
||||
if (valueTokens.length === 0) {
|
||||
if (settingKey === "stay_247") {
|
||||
return handle247Toggle(ctx, message, set, guildId);
|
||||
}
|
||||
const val = set.get(settingKey);
|
||||
const label = prettifySettingLabel(settingKey, t247, guildId);
|
||||
return message.reply(embed("**" + label + "**\nValue: " + displayValue(settingKey, val)));
|
||||
}
|
||||
|
||||
const rawValue = valueTokens.join(" ");
|
||||
|
||||
if (settingKey === "stay_247") {
|
||||
const mode = rawValue.toLowerCase().trim();
|
||||
if (mode === "off" || mode === "false" || mode === "disable" || mode === "0") {
|
||||
const loc = tWrap(ctx, guildId);
|
||||
if (!guildId) return message.reply(embed(loc("responses.settings.noServer")));
|
||||
const { channelId } = await ctx.players.checkVoiceChannels(message);
|
||||
if (channelId) {
|
||||
await disable247(ctx, set, guildId, channelId);
|
||||
return message.reply(build247Panel(set, ctx, guildId, cleanId(channelId)));
|
||||
}
|
||||
for (const [chId, player] of [...ctx.players.playerMap.entries()]) {
|
||||
if (cleanId(player?._guildId ?? "") === cleanId(guildId)) {
|
||||
const activeId = cleanId(player._channelId ?? chId);
|
||||
ctx.markIntentionalLeave?.(activeId);
|
||||
ctx.players.playerMap.delete(activeId);
|
||||
if (activeId !== chId) ctx.players.playerMap.delete(chId);
|
||||
await player.leave().catch(() => {});
|
||||
player.destroy();
|
||||
}
|
||||
}
|
||||
save247Channels(set, new Set());
|
||||
return message.reply(build247Panel(set, ctx, guildId, null));
|
||||
}
|
||||
return handle247Toggle(ctx, message, set, guildId);
|
||||
}
|
||||
|
||||
const err = await applySet(ctx, message, set, settingKey, rawValue);
|
||||
if (err) return message.reply(embed(err));
|
||||
|
||||
const val = set.get(settingKey);
|
||||
const label = prettifySettingLabel(settingKey, t247, guildId);
|
||||
return message.reply(embed(ctx.t(message, "responses.settings.setSuccess", { label, value: displayValue(settingKey, val) })));
|
||||
}
|
||||
import { CommandBuilder } from "../src/commands/index.mjs";
|
||||
import { getMessageGuildId } from "../src/ui/index.mjs";
|
||||
// kept verbatim from the original file (unused there as well):
|
||||
import { logger } from "../src/core/Logger.mjs";
|
||||
import {
|
||||
SHORTCUTS, BOOL_SETTINGS, VALID_LOCALES, setValidLocales,
|
||||
VOLUME_MIN, VOLUME_MAX,
|
||||
displayValue, getGuildName, embed, prettifySettingLabel,
|
||||
} from "./settings/utils.mjs";
|
||||
import { get247Channels, build247Panel } from "./settings/channels247.mjs";
|
||||
import { applySet, handleShortcut } from "./settings/setters.mjs";
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
@@ -459,7 +26,7 @@ async function handleShortcut(ctx, message, settingKey, valueTokens) {
|
||||
*/
|
||||
export const command = function() {
|
||||
if (this.locale) {
|
||||
VALID_LOCALES = this.locale.availableLocales();
|
||||
setValidLocales(this.locale.availableLocales());
|
||||
}
|
||||
|
||||
if (this.loader) {
|
||||
|
||||
Reference in new issue
Block a user