Files
fluxer/commands/debug/gateway.mjs
T
NoLogicAlan 253d11cd57 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).
2026-09-09 02:04:33 +03:00

135 lines
5.0 KiB
JavaScript

/**
* @module commands/debug/gateway
* @description Gateway/voice state inspection helpers (room labels, ghost detection, bot gateway voice state, stale presence, player labels). Verbatim from the original single-file command.
*/
import { getVoiceManager } from "@fluxerjs/voice";
import { logger } from "../../src/core/Logger.mjs";
import { cleanId } from "../../src/ui/index.mjs";
/**
* Get a human-readable label for a voice room's connection state.
* @private
* @param {object|null} room - The voice room object.
* @returns {string} Connection state label.
*/
export function roomStateLabel(room) {
if (!room) return "none";
const cs = room.connectionState;
if (cs === 0 || cs === "CONN_DISCONNECTED") return "disconnected(0)";
if (cs === 1 || cs === "CONN_CONNECTED") return "connected(1)";
if (cs === 2) return room.isConnected ? "connected(2)" : "reconnecting(2)";
if (cs === "CONN_RECONNECTING") return "reconnecting";
if (cs === 3) return "reconnecting(3)";
if (cs === 4) return "signal_reconnecting(4)";
if (typeof cs === "string") return cs;
return String(cs);
}
/**
* Check if a player has a ghost connection (appears in voice but WebSocket is dead).
* @private
* @param {object} player - The player instance.
* @returns {boolean} True if the player has a ghost connection.
*/
export function isGhostConnection(player) {
const conn = player.connection;
if (!conn) return false;
if (player._destroyed || player.leaving || player._isJoining) return false;
const room = conn.room;
if (!room) {
return !conn._destroyed;
}
if (!room.isConnected) return true;
const cs = room.connectionState;
if (cs === 0 || cs === "CONN_DISCONNECTED") return true;
if (cs === "CONN_RECONNECTING" || cs === 3 || cs === 4) return true;
return false;
}
/**
* Get the bot's voice channel state from the gateway cache.
* @private
* @param {object} client - The client instance.
* @param {string} guildId - The guild ID to check.
* @returns {{ userId: string, channelId: string }|null} Bot's voice state or null.
*/
export function getBotGatewayVoiceState(client, guildId) {
const botId = client.user?.id;
if (!botId || !guildId) return null;
try {
const vm = getVoiceManager(client);
if (vm?.voiceStates) {
const cleanGuild = cleanId(guildId);
const guildVoiceMap = vm.voiceStates.get(cleanGuild) ?? vm.voiceStates.get(guildId);
if (guildVoiceMap && typeof guildVoiceMap.get === "function") {
const channelId = guildVoiceMap.get(botId);
if (channelId) return { userId: botId, channelId: cleanId(channelId) };
}
}
} catch (e) { logger.warn("[Debug] getBotGatewayVoiceState (VoiceManager):", e?.message); }
try {
const cleanGuild = cleanId(guildId);
const guild = client.guilds.get(cleanGuild) ?? client.guilds.get(guildId);
const voiceStates = guild?.voice_states ?? guild?.voiceStates ?? null;
if (!voiceStates) return null;
const entries = Array.isArray(voiceStates)
? voiceStates
: typeof voiceStates.values === "function"
? [...voiceStates.values()]
: Object.entries(voiceStates).map(([uid, val]) => {
if (typeof val === "string") return { user_id: uid, channel_id: val };
const obj = typeof val === "object" && val !== null ? val : {};
return { user_id: uid, channel_id: obj.channelId ?? obj.channel_id ?? null, ...obj };
});
for (const state of entries) {
const uid = state?.user_id ?? state?.userId ?? state?.id;
if (uid === botId) {
const chId = state?.channel_id ?? state?.channelId ?? null;
if (chId) return { userId: botId, channelId: cleanId(chId) };
}
}
} catch (e) { logger.warn("[Debug] getBotGatewayVoiceState (guild cache):", e?.message); }
return null;
}
/**
* Check if the gateway voice state is stale (mismatches the player's channel).
* @private
* @param {object} client - The client instance.
* @param {object} player - The player instance.
* @returns {boolean} True if the gateway presence is stale.
*/
export function isStaleGatewayPresence(client, player) {
const guildId = player._guildId ?? player._resolveGuildId?.();
if (!guildId) return false;
const gatewayState = getBotGatewayVoiceState(client, guildId);
if (!gatewayState) return false;
const playerChannel = cleanId(player._channelId ?? player._home247Channel);
const gatewayChannel = gatewayState.channelId;
return !playerChannel || playerChannel !== gatewayChannel;
}
/**
* Build a human-readable label for a player (guild name / #channel name).
* @private
* @param {object} client - The client instance.
* @param {string} channelId - The voice channel ID.
* @param {object} player - The player instance.
* @returns {string} Formatted label string.
*/
export function buildPlayerLabel(client, channelId, player) {
const channel = client.channels.get(channelId);
const gId = player._guildId ?? channel?.guildId;
const guild = gId ? client.guilds.get(cleanId(gId)) : null;
return `${guild?.name ?? "unknown"} / #${channel?.name ?? channelId}`;
}