mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
better log for LiveKit
This commit is contained in:
1 parent
ee0e353f0d
commit
0f40ee2ba8
3 files changed
+70
-51
No files matched your search
+50
-34
@@ -1121,14 +1121,18 @@ export class GatewayHandler {
|
|||||||
|
|
||||||
// End the startup grace period after the configured delay.
|
// End the startup grace period after the configured delay.
|
||||||
// Any deferred GuildDelete events that weren't cancelled by a GuildCreate
|
// Any deferred GuildDelete events that weren't cancelled by a GuildCreate
|
||||||
// will be processed at this point.
|
// are processed in one batch here.
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this._inStartupGrace = false;
|
this._inStartupGrace = false;
|
||||||
const pending = this._deferredGuildDeletes.size;
|
const pending = this._deferredGuildDeletes;
|
||||||
if (pending > 0) {
|
if (pending.size > 0) {
|
||||||
logger.guild(
|
const ids = [...pending.keys()];
|
||||||
`[GuildDelete] Grace period ended — ${pending} server(s) confirmed removed, cleaning up.`
|
// Clear all individual deferred timers — we'll process them all at once
|
||||||
);
|
for (const [, data] of pending) {
|
||||||
|
clearTimeout(data.timer);
|
||||||
|
}
|
||||||
|
pending.clear();
|
||||||
|
this._processGuildDelete(ids);
|
||||||
}
|
}
|
||||||
}, this._startupDeleteGraceMs);
|
}, this._startupDeleteGraceMs);
|
||||||
|
|
||||||
@@ -1213,40 +1217,52 @@ export class GatewayHandler {
|
|||||||
// ── GuildDelete cleanup (extracted for deferred processing) ────────────────
|
// ── GuildDelete cleanup (extracted for deferred processing) ────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the actual cleanup for a guild that the bot was removed from.
|
* Perform the actual cleanup for guild(s) that the bot was removed from.
|
||||||
* Extracted from the GuildDelete handler so it can be called after the
|
* Accepts a single guildId (string) or an array of guildIds for batch
|
||||||
* startup grace period expires.
|
* processing (used when the startup grace period expires with multiple
|
||||||
* @param {string} guildId
|
* deferred deletes). Logs ONE summary line with all cleaned guild IDs.
|
||||||
|
* @param {string|string[]} guildIds
|
||||||
*/
|
*/
|
||||||
_processGuildDelete(guildId) {
|
_processGuildDelete(guildIds) {
|
||||||
|
const ids = Array.isArray(guildIds) ? guildIds : [guildIds];
|
||||||
const { remix } = this;
|
const { remix } = this;
|
||||||
const cleanGuildId = String(guildId).replace(/\D/g, "");
|
const cleanedIds = [];
|
||||||
let destroyedPlayers = 0;
|
let totalDestroyedPlayers = 0;
|
||||||
|
|
||||||
for (const [channelId, player] of remix.players.playerMap) {
|
for (const guildId of ids) {
|
||||||
if (String(player._guildId ?? "").replace(/\D/g, "") === cleanGuildId) {
|
const cleanGuildId = String(guildId).replace(/\D/g, "");
|
||||||
remix.players.playerMap.delete(channelId);
|
let destroyedPlayers = 0;
|
||||||
try { player.leave().catch(() => {}); } catch (_) {}
|
|
||||||
try { player.destroy(); } catch (_) {}
|
for (const [channelId, player] of remix.players.playerMap) {
|
||||||
destroyedPlayers++;
|
if (String(player._guildId ?? "").replace(/\D/g, "") === cleanGuildId) {
|
||||||
|
remix.players.playerMap.delete(channelId);
|
||||||
|
try { player.leave().catch(() => {}); } catch (_) {}
|
||||||
|
try { player.destroy(); } catch (_) {}
|
||||||
|
destroyedPlayers++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const [userId, info] of [...remix.observedVoiceUsers]) {
|
||||||
|
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId) remix.observedVoiceUsers.delete(userId);
|
||||||
|
}
|
||||||
|
for (const [userId, info] of [...remix.observedVoiceBots]) {
|
||||||
|
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId) remix.observedVoiceBots.delete(userId);
|
||||||
|
}
|
||||||
|
for (const [stateKey, info] of [...this._prevVoiceState]) {
|
||||||
|
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId)
|
||||||
|
this._prevVoiceState.delete(stateKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
remix.settingsMgr.removeServer(guildId);
|
||||||
|
remix._announcementChannelCache.delete(guildId);
|
||||||
|
|
||||||
|
cleanedIds.push(guildId);
|
||||||
|
totalDestroyedPlayers += destroyedPlayers;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [userId, info] of [...remix.observedVoiceUsers]) {
|
if (cleanedIds.length > 0) {
|
||||||
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId) remix.observedVoiceUsers.delete(userId);
|
const extra = totalDestroyedPlayers > 0 ? ` (${totalDestroyedPlayers} player(s) destroyed)` : "";
|
||||||
|
logger.guild(`[GuildDelete] Cleaned up ${cleanedIds.length} server(s): ${cleanedIds.join(", ")}${extra}`);
|
||||||
}
|
}
|
||||||
for (const [userId, info] of [...remix.observedVoiceBots]) {
|
|
||||||
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId) remix.observedVoiceBots.delete(userId);
|
|
||||||
}
|
|
||||||
for (const [stateKey, info] of [...this._prevVoiceState]) {
|
|
||||||
if (String(info.guildId).replace(/\D/g, "") === cleanGuildId)
|
|
||||||
this._prevVoiceState.delete(stateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
remix.settingsMgr.removeServer(guildId);
|
|
||||||
remix._announcementChannelCache.delete(guildId);
|
|
||||||
|
|
||||||
const extra = destroyedPlayers > 0 ? ` (${destroyedPlayers} player(s) destroyed)` : "";
|
|
||||||
logger.guild(`[GuildDelete] Cleaned up server ${guildId}${extra}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1003,7 +1003,7 @@ export default class Player extends EventEmitter {
|
|||||||
if (this._destroyed) return;
|
if (this._destroyed) return;
|
||||||
|
|
||||||
if (this._isJoining) {
|
if (this._isJoining) {
|
||||||
logger.player(`[Player] Busy joining. Ignoring: ${channelId}`);
|
logger.mediaplayer(`[Player] Busy joining. Ignoring: ${channelId}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.connection && this._channelId === channelId) {
|
if (this.connection && this._channelId === channelId) {
|
||||||
@@ -1289,7 +1289,7 @@ export default class Player extends EventEmitter {
|
|||||||
|
|
||||||
if (e.message?.includes("401") || e.message?.includes("Unauthorized")) {
|
if (e.message?.includes("401") || e.message?.includes("Unauthorized")) {
|
||||||
// Track 401 errors for logging purposes
|
// Track 401 errors for logging purposes
|
||||||
logger.warn(`[Player] Join failed with 401 Unauthorized for guild ${this._guildId}`);
|
logger.warn(`[Player] Join failed with 401 Unauthorized for guild ${this._guildId ?? this._resolveGuildId() ?? 'unknown'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
|
|||||||
@@ -27,10 +27,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { EventEmitter } from "node:events";
|
import { EventEmitter } from "node:events";
|
||||||
import { Room, RoomEvent, ConnectionState } from "@livekit/rtc-node";
|
import { Room, RoomEvent, ConnectionState, setLogLevel } from "@livekit/rtc-node";
|
||||||
import { joinVoiceChannel, getVoiceManager } from "@fluxerjs/voice";
|
import { joinVoiceChannel, getVoiceManager } from "@fluxerjs/voice";
|
||||||
import { logger } from "./Logger.mjs";
|
import { logger } from "./Logger.mjs";
|
||||||
|
|
||||||
|
// Suppress LiveKit SDK's verbose pino/JSON logs (lk-rtc, [voice LiveKitRtc]).
|
||||||
|
// Only show warnings and errors from the SDK — the "Connect callback received"
|
||||||
|
// JSON lines and "connected to room" messages are noise for production.
|
||||||
|
try { setLogLevel("warn"); } catch (_) {}
|
||||||
|
|
||||||
// ConnectionState enum values for reference:
|
// ConnectionState enum values for reference:
|
||||||
// ConnectionState.CONN_DISCONNECTED = 0
|
// ConnectionState.CONN_DISCONNECTED = 0
|
||||||
// ConnectionState.CONN_CONNECTED = 1
|
// ConnectionState.CONN_CONNECTED = 1
|
||||||
@@ -470,7 +475,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
const elapsed = Date.now() - lastJoin;
|
const elapsed = Date.now() - lastJoin;
|
||||||
if (elapsed < this._guildMinJoinInterval) {
|
if (elapsed < this._guildMinJoinInterval) {
|
||||||
const extraWait = this._guildMinJoinInterval - elapsed;
|
const extraWait = this._guildMinJoinInterval - elapsed;
|
||||||
logger.player(
|
logger.mediaplayer(
|
||||||
`[FluxerRevoice] Guild ${guildId} was joined ${elapsed}ms ago — ` +
|
`[FluxerRevoice] Guild ${guildId} was joined ${elapsed}ms ago — ` +
|
||||||
`waiting additional ${extraWait}ms to avoid 401 race`
|
`waiting additional ${extraWait}ms to avoid 401 race`
|
||||||
);
|
);
|
||||||
@@ -487,7 +492,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
`aborting join for channel ${channelId} to avoid guaranteed failure. `
|
`aborting join for channel ${channelId} to avoid guaranteed failure. `
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
logger.player(
|
logger.mediaplayer(
|
||||||
`[FluxerRevoice] Guild ${guildId} has 401 cooldown — ` +
|
`[FluxerRevoice] Guild ${guildId} has 401 cooldown — ` +
|
||||||
`waiting ${Math.round(cooldownRemaining / 1000)}s before attempting join`
|
`waiting ${Math.round(cooldownRemaining / 1000)}s before attempting join`
|
||||||
);
|
);
|
||||||
@@ -512,18 +517,16 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
// up fully and rejoin instead of returning a dead connection that will
|
// up fully and rejoin instead of returning a dead connection that will
|
||||||
// fail with "LiveKit disconnected (connectionState: 0)".
|
// fail with "LiveKit disconnected (connectionState: 0)".
|
||||||
if (existing && !existing._destroyed && existing.room && existing.room.isConnected) {
|
if (existing && !existing._destroyed && existing.room && existing.room.isConnected) {
|
||||||
logger.player(`[FluxerRevoice] Already connected to ${channelId}, returning existing connection`);
|
logger.mediaplayer(`[FluxerRevoice] Already connected to ${channelId}, returning existing connection`);
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
logger.player(
|
logger.mediaplayer(
|
||||||
`[FluxerRevoice] Stale connection for ${channelId} ` +
|
`[FluxerRevoice] Stale connection for ${channelId} — cleaning up and rejoining`
|
||||||
`(isConnected: ${existing?.room?.isConnected ?? false}, ` +
|
|
||||||
`destroyed: ${existing?._destroyed ?? true}) — cleaning up and rejoining`
|
|
||||||
);
|
);
|
||||||
await this._destroyStaleConnection(channelId, existing);
|
await this._destroyStaleConnection(channelId, existing);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.player(`[FluxerRevoice] Joining channel ${channelId} via Fluxer gateway...`);
|
logger.mediaplayer(`[FluxerRevoice] Joining channel ${channelId} via Fluxer gateway...`);
|
||||||
|
|
||||||
// Fetch the channel object from the client cache (or REST)
|
// Fetch the channel object from the client cache (or REST)
|
||||||
let channel = this.client.channels?.get?.(channelId);
|
let channel = this.client.channels?.get?.(channelId);
|
||||||
@@ -568,7 +571,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
throw new Error(`Fluxer voice join returned null for ${channelId}`);
|
throw new Error(`Fluxer voice join returned null for ${channelId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.player(`[FluxerRevoice] Gateway join successful, extracting LiveKit room...`);
|
logger.mediaplayer(`[FluxerRevoice] Gateway join successful, extracting LiveKit room...`);
|
||||||
|
|
||||||
// The @fluxerjs/voice connection wraps a LiveKit Room.
|
// The @fluxerjs/voice connection wraps a LiveKit Room.
|
||||||
// Extract it so we can use it with revoice.js's MediaPlayer.
|
// Extract it so we can use it with revoice.js's MediaPlayer.
|
||||||
@@ -577,7 +580,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
// Try to get the LiveKit Room from the native connection
|
// Try to get the LiveKit Room from the native connection
|
||||||
if (nativeConnection.room) {
|
if (nativeConnection.room) {
|
||||||
room = nativeConnection.room;
|
room = nativeConnection.room;
|
||||||
logger.player(`[FluxerRevoice] Got LiveKit room from native connection (isConnected: ${room.isConnected}, connectionState: ${stateLabel(room.connectionState)})`);
|
logger.mediaplayer(`[FluxerRevoice] Got LiveKit room from native connection (isConnected: ${room.isConnected}, connectionState: ${stateLabel(room.connectionState)})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// (non-LiveKit) which we don't support for music playback.
|
// (non-LiveKit) which we don't support for music playback.
|
||||||
@@ -642,7 +645,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
throw new Error(`LiveKit room in disconnected state: ${stateLabel(finalCS)}`);
|
throw new Error(`LiveKit room in disconnected state: ${stateLabel(finalCS)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.player(`[FluxerRevoice] LiveKit room ready (isConnected: ${finalConnected}, connectionState: ${stateLabel(finalCS)})`);
|
logger.mediaplayer(`[FluxerRevoice] LiveKit room ready (isConnected: ${finalConnected}, connectionState: ${stateLabel(finalCS)})`);
|
||||||
|
|
||||||
// Record the successful join time for this guild so future joins
|
// Record the successful join time for this guild so future joins
|
||||||
// can enforce the minimum interval and avoid 401 races.
|
// can enforce the minimum interval and avoid 401 races.
|
||||||
@@ -669,7 +672,7 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
room.on(RoomEvent.Disconnected, (reason) => {
|
room.on(RoomEvent.Disconnected, (reason) => {
|
||||||
const isIntentional = this._intentionalDisconnects.has(String(channelId));
|
const isIntentional = this._intentionalDisconnects.has(String(channelId));
|
||||||
const reasonLabel = isIntentional ? "intentional" : (reason ?? "unexpected");
|
const reasonLabel = isIntentional ? "intentional" : (reason ?? "unexpected");
|
||||||
logger.player(`[FluxerRevoice] Room disconnected: ${reasonLabel}${isIntentional ? " (bot-initiated, recovery suppressed)" : ""}`);
|
logger.mediaplayer(`[FluxerRevoice] Room disconnected: ${reasonLabel}${isIntentional ? " (bot-initiated, recovery suppressed)" : ""}`);
|
||||||
this._intentionalDisconnects.delete(String(channelId));
|
this._intentionalDisconnects.delete(String(channelId));
|
||||||
connection._connected = false;
|
connection._connected = false;
|
||||||
connection._destroyed = true;
|
connection._destroyed = true;
|
||||||
@@ -694,11 +697,11 @@ export class FluxerRevoice extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
room.on(RoomEvent.Reconnecting, () => {
|
room.on(RoomEvent.Reconnecting, () => {
|
||||||
logger.player("[FluxerRevoice] Room reconnecting...");
|
logger.mediaplayer("[FluxerRevoice] Room reconnecting...");
|
||||||
});
|
});
|
||||||
|
|
||||||
room.on(RoomEvent.Reconnected, () => {
|
room.on(RoomEvent.Reconnected, () => {
|
||||||
logger.player("[FluxerRevoice] Room reconnected");
|
logger.mediaplayer("[FluxerRevoice] Room reconnected");
|
||||||
connection._connected = true;
|
connection._connected = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user