From 3d660438642c8757792e26de267842c5efd47fd8 Mon Sep 17 00:00:00 2001 From: NoLogicAlan Date: Mon, 4 May 2026 16:25:22 +0300 Subject: [PATCH] cleaning --- commands/join.mjs | 10 +++++++--- commands/settings.mjs | 19 +++++++++++++------ index.mjs | 9 +++++---- src/MoonlinkManager.mjs | 9 +++++---- src/PlayerManager.mjs | 13 +++++++------ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/commands/join.mjs b/commands/join.mjs index 70c246a..26285e6 100644 --- a/commands/join.mjs +++ b/commands/join.mjs @@ -39,6 +39,8 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { }); p.on("autoleave", () => { + const activeChannelId = String(p._channelId ?? cid).replace(/\D/g, "") || cid; + const homeChannelId = String(p._home247Channel ?? activeChannelId).replace(/\D/g, "") || activeChannelId; const guildId = message.channel?.guildId ?? message.channel?.guild?.id ?? message.message?.guildId @@ -56,13 +58,15 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { } catch (_) { return "%"; } })(); const desc = is247 - ? `Left channel <#${cid}> because of inactivity.` - : `Left channel <#${cid}> because of inactivity.\nIf you want me to stay in voice, use \`${prefix}247 on/auto\``; + ? `Left channel <#${activeChannelId}> because of inactivity.` + : `Left channel <#${activeChannelId}> because of inactivity.\nIf you want me to stay in voice, use \`${prefix}247 on/auto\``; const embed = new EmbedBuilder().setColor(getGlobalColor()) .setDescription(desc) .toJSON(); message.channel.sendEmbed({ embeds: [embed] }); - this.players.playerMap.delete(cid); + this.players.playerMap.delete(activeChannelId); + if (activeChannelId !== cid) this.players.playerMap.delete(cid); + if (homeChannelId !== activeChannelId) this.players.playerMap.delete(homeChannelId); p.destroy(); }); diff --git a/commands/settings.mjs b/commands/settings.mjs index f2e90db..ebb683a 100644 --- a/commands/settings.mjs +++ b/commands/settings.mjs @@ -123,9 +123,15 @@ async function handle247(ctx, message, value) { channels.delete(id); save247Channels(set, channels); ctx.markIntentionalLeave?.(id); - const player = ctx.players.playerMap.get(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) { - ctx.players.playerMap.delete(id); + const activeChannelId = cleanId(player._channelId ?? id); + ctx.players.playerMap.delete(activeChannelId); + if (activeChannelId !== id) ctx.players.playerMap.delete(id); await player.leave().catch(() => {}); player.destroy(); } @@ -137,10 +143,11 @@ async function handle247(ctx, message, value) { save247Channels(set, new Set()); set.set("stay_247_mode", "off"); for (const [chId, player] of [...ctx.players.playerMap.entries()]) { - const ch = ctx.handler.client.channels.cache.get(chId); - if (ch?.guild?.id === guildId || ch?.guildId === guildId) { - ctx.markIntentionalLeave?.(chId); - ctx.players.playerMap.delete(chId); + if (cleanId(player?._guildId ?? "") === cleanId(guildId)) { + const activeChannelId = cleanId(player._channelId ?? chId); + ctx.markIntentionalLeave?.(activeChannelId); + ctx.players.playerMap.delete(activeChannelId); + if (activeChannelId !== chId) ctx.players.playerMap.delete(chId); await player.leave().catch(() => {}); player.destroy(); } diff --git a/index.mjs b/index.mjs index 3310bb4..3980896 100644 --- a/index.mjs +++ b/index.mjs @@ -604,10 +604,10 @@ class Remix { if (wsListenerAttached && this._rawGatewayWsObj && this._rawGatewayWsObj !== wsObj) { try { if (typeof this._rawGatewayWsObj.removeEventListener === "function") { - this._rawGatewayWsObj.removeEventListener("message", this._rawGatewayHandler); + this._rawGatewayWsObj.removeEventListener("message", this._rawGatewayMessageListener ?? this._rawGatewayHandler); this._rawGatewayWsObj.removeEventListener("error", this._rawGatewayErrorHandler); } else if (typeof this._rawGatewayWsObj.off === "function") { - this._rawGatewayWsObj.off("message", this._rawGatewayHandler); + this._rawGatewayWsObj.off("message", this._rawGatewayMessageListener ?? this._rawGatewayHandler); this._rawGatewayWsObj.off("error", this._rawGatewayErrorHandler); } } catch (_) {} @@ -704,12 +704,13 @@ class Remix { this._rawGatewayErrorHandler = (err) => { logger.warn("[Gateway] Raw WS socket error (will reconnect):", err?.message ?? err); }; + this._rawGatewayMessageListener = (event) => this._rawGatewayHandler(event?.data ?? event); if (typeof wsObj.addEventListener === "function") { - wsObj.addEventListener("message", (event) => this._rawGatewayHandler(event.data)); + wsObj.addEventListener("message", this._rawGatewayMessageListener); wsObj.addEventListener("error", this._rawGatewayErrorHandler); } else if (typeof wsObj.on === "function") { - wsObj.on("message", this._rawGatewayHandler); + wsObj.on("message", this._rawGatewayMessageListener); wsObj.on("error", this._rawGatewayErrorHandler); } diff --git a/src/MoonlinkManager.mjs b/src/MoonlinkManager.mjs index 47b0481..19ad623 100644 --- a/src/MoonlinkManager.mjs +++ b/src/MoonlinkManager.mjs @@ -188,10 +188,10 @@ export class MoonlinkManager extends EventEmitter { if (this._rawWsHandler && this._rawWsObj && this._rawWsObj !== wsObj) { try { if (typeof this._rawWsObj.removeEventListener === "function") { - this._rawWsObj.removeEventListener("message", this._rawWsHandler); + this._rawWsObj.removeEventListener("message", this._rawWsMessageListener ?? this._rawWsHandler); this._rawWsObj.removeEventListener("error", this._rawWsErrorHandler); } else if (typeof this._rawWsObj.off === "function") { - this._rawWsObj.off("message", this._rawWsHandler); + this._rawWsObj.off("message", this._rawWsMessageListener ?? this._rawWsHandler); this._rawWsObj.off("error", this._rawWsErrorHandler); } } catch (_) {} @@ -216,12 +216,13 @@ export class MoonlinkManager extends EventEmitter { this._rawWsErrorHandler = (err) => { logger.warn("[MoonlinkManager] Raw WS socket error (will reconnect):", err?.message ?? err); }; + this._rawWsMessageListener = (event) => this._rawWsHandler(event?.data ?? event); if (typeof wsObj.addEventListener === "function") { - wsObj.addEventListener("message", this._rawWsHandler); + wsObj.addEventListener("message", this._rawWsMessageListener); wsObj.addEventListener("error", this._rawWsErrorHandler); } else if (typeof wsObj.on === "function") { - wsObj.on("message", this._rawWsHandler); + wsObj.on("message", this._rawWsMessageListener); wsObj.on("error", this._rawWsErrorHandler); } diff --git a/src/PlayerManager.mjs b/src/PlayerManager.mjs index 262c489..e2ae987 100644 --- a/src/PlayerManager.mjs +++ b/src/PlayerManager.mjs @@ -292,18 +292,19 @@ export class PlayerManager { const serverId = msg.channel?.server_id ?? msg.channel?.serverId ?? msg.message?.server_id ?? msg.message?.serverId; if (serverId) { - cid = [...this.playerMap.keys()].find((id) => { - const ch = this.commands.client?.channels?.get(id) ?? - this.commands.client?.channels?.cache?.get(id); - return ch?.guildId === serverId || ch?.server_id === serverId || ch?.serverId === serverId; - }); + const matchedEntry = [...this.playerMap.entries()].find(([, player]) => + String(player?._guildId ?? "").replace(/\D/g, "") === String(serverId).replace(/\D/g, "") + ); + cid = matchedEntry?.[0] ?? null; } } const player = cid ? this.playerMap.get(cid) : null; if (!player) return msg.replyEmbed(mkEmbed("I'm not in a voice channel.")); - this.playerMap.delete(cid); + const activeChannelId = String(player._channelId ?? cid).replace(/\D/g, "") || cid; + this.playerMap.delete(activeChannelId); + if (activeChannelId !== cid) this.playerMap.delete(cid); await msg.replyEmbed(mkEmbed("✅ Successfully Left")); await player.leave(); player.destroy();