From 4d9ade53c8397b4ae4ea7404d4a178335e7a107e Mon Sep 17 00:00:00 2001 From: NoLogicAlan Date: Thu, 14 May 2026 20:19:58 +0300 Subject: [PATCH] auto-rejoin 24/7 channels after a reboot/crash --- commands/join.mjs | 54 ++++++++++++++++++++++++----- src/GatewayHandler.mjs | 77 ++++++++++++++++++++++++++++++++++++++++++ src/PlayerManager.mjs | 18 ++++++++-- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/commands/join.mjs b/commands/join.mjs index e54d428..fb09017 100644 --- a/commands/join.mjs +++ b/commands/join.mjs @@ -1,6 +1,7 @@ import { CommandBuilder } from "../src/CommandHandler.mjs"; import { EmbedBuilder } from "@fluxerjs/core"; import { getGlobalColor } from "../src/MessageHandler.mjs"; +import { logger } from "../src/constants/Logger.mjs"; import Player from "../src/Player.mjs"; function getGuildId(message) { @@ -57,27 +58,62 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { const activeChannelId = String(p._channelId ?? cid).replace(/\D/g, "") || cid; const homeChannelId = String(p._home247Channel ?? activeChannelId).replace(/\D/g, "") || activeChannelId; const guildId = getGuildId(message); + + // Check 24/7 mode for this channel const is247 = (() => { try { const raw = this.settingsMgr?.getServer?.(guildId)?.get?.("stay_247"); return raw && raw !== "none"; } catch (_) { return false; } })(); - const prefix = (() => { + + // Determine per-channel mode + const mode247 = (() => { + if (!is247) return "off"; try { - return this._commands?.getPrefix?.(guildId) ?? "%"; - } catch (_) { return "%"; } + const set = this.settingsMgr?.getServer?.(guildId); + const modes = set?.get?.("stay_247_modes"); + const matchCh = homeChannelId || activeChannelId; + if (modes && typeof modes === "object" && !Array.isArray(modes) && modes[matchCh]) { + return modes[matchCh]; + } + return set?.get?.("stay_247_mode") ?? "off"; + } catch (_) { return "off"; } })(); - const desc = is247 - ? this.t(message, "responses.join.autoLeaveInactive247", { channel: activeChannelId, prefix }) - : this.t(message, "responses.join.autoLeaveInactive", { channel: activeChannelId }); - const embed = new EmbedBuilder().setColor(getGlobalColor()) - .setDescription(desc); - message.channel.send({ embeds: [embed] }); + this.players.playerMap.delete(activeChannelId); if (activeChannelId !== cid) this.players.playerMap.delete(cid); if (homeChannelId !== activeChannelId) this.players.playerMap.delete(homeChannelId); p.destroy(); + + if (is247 && (mode247 === "on" || mode247 === "auto")) { + // 24/7 mode — auto-rejoin after a delay + const rejoinDelay = this.config?.timers?.rejoin247Delay ?? 3_000; + const prefix = (() => { + try { return this._commands?.getPrefix?.(guildId) ?? "%"; } catch (_) { return "%"; } + })(); + const embed = new EmbedBuilder().setColor(getGlobalColor()) + .setDescription(this.t(message, "responses.join.autoLeaveInactive247", { channel: activeChannelId, prefix })); + message.channel.send({ embeds: [embed] }); + + setTimeout(async () => { + try { + if (typeof this._spawnPlayer === "function") { + await this._spawnPlayer(guildId, homeChannelId); + } + } catch (e) { + logger.warn(`[join/autoleave] 24/7 auto-rejoin failed for ${homeChannelId}: ${e.message}`); + } + }, rejoinDelay); + } else { + // Not 24/7 — send inactivity message + const prefix = (() => { + try { return this._commands?.getPrefix?.(guildId) ?? "%"; } catch (_) { return "%"; } + })(); + const embed = new EmbedBuilder().setColor(getGlobalColor()) + .setDescription(this.t(message, "responses.join.autoLeaveInactive", { channel: activeChannelId })); + message.channel.send({ embeds: [embed] }); + } }); p.on("message", m => { diff --git a/src/GatewayHandler.mjs b/src/GatewayHandler.mjs index c2a1b5d..616f3a5 100644 --- a/src/GatewayHandler.mjs +++ b/src/GatewayHandler.mjs @@ -1143,6 +1143,83 @@ export class GatewayHandler { logger.guild(`[GuildDelete] Startup grace period ended. No deferred deletions.`); } }, this._startupDeleteGraceMs); + + // ── Boot 24/7 auto-rejoin ──────────────────────────────────────────────── + // After a reboot/crash, automatically rejoin all voice channels that have + // 24/7 mode enabled (stay_247 setting with mode "on" or "auto"). + // Delayed slightly to let the gateway settle and Moonlink initialise. + setTimeout(() => this.rejoin247Channels(), 5_000); + } + + /** + * Rejoin all 24/7 voice channels after a bot reboot/crash. + * Reads stay_247 settings from all guilds and spawns players for channels + * that aren't already active. + */ + async rejoin247Channels() { + const { remix } = this; + const settings = remix.settingsMgr; + if (!settings?.guilds) return; + + const rejoinDelay = this.T.rejoin247Delay ?? 3_000; + const channels = []; + + // Collect all 24/7 channels with mode "on" or "auto" + for (const [guildId, serverSettings] of settings.guilds) { + const raw = serverSettings.get("stay_247"); + if (!raw || raw === "none") continue; + + const rawArr = Array.isArray(raw) ? raw : [raw]; + const cleaned = rawArr + .map(id => String(id).replace(/\D/g, "")) + .filter(id => id.length >= 15); + + const modes = serverSettings.get("stay_247_modes"); + + for (const channelId of cleaned) { + const mode = (modes && typeof modes === "object" && !Array.isArray(modes)) + ? modes[channelId] ?? "off" + : serverSettings.get("stay_247_mode") ?? "off"; + + if (mode !== "on" && mode !== "auto") continue; + + // Skip if we already have an active player for this channel + const existing = remix.players.playerMap.get(channelId) + ?? [...remix.players.playerMap.values()].find(p => + String(p?._channelId ?? "").replace(/\D/g, "") === channelId + ); + if (existing && !existing._destroyed) continue; + + channels.push({ guildId, channelId, mode }); + } + } + + if (channels.length === 0) return; + + logger.voice247(`[BootRecovery] Found ${channels.length} 24/7 channel(s) to rejoin`); + + // Stagger the joins to avoid overloading the gateway + for (let i = 0; i < channels.length; i++) { + const { guildId, channelId, mode } = channels[i]; + const delay = i * rejoinDelay; + + setTimeout(async () => { + try { + // Verify the channel still exists + const ch = remix.client?.channels?.get?.(channelId); + if (!ch) { + logger.voice247(`[BootRecovery] Channel ${channelId} no longer exists — skipping`); + return; + } + + logger.voice247(`[BootRecovery] Rejoining 24/7 channel ${channelId} (guild ${guildId}, mode ${mode})`); + await remix._spawnPlayer(guildId, channelId); + logger.voice247(`[BootRecovery] Successfully rejoined 24/7 channel ${channelId}`); + } catch (e) { + logger.warn(`[BootRecovery] Failed to rejoin 24/7 channel ${channelId}: ${e.message}`); + } + }, delay); + } } // ── GuildDelete cleanup (extracted for deferred processing) ──────────────── diff --git a/src/PlayerManager.mjs b/src/PlayerManager.mjs index fea4e85..3bd2c0c 100644 --- a/src/PlayerManager.mjs +++ b/src/PlayerManager.mjs @@ -816,9 +816,21 @@ export class PlayerManager { player.destroy(); if (isIn247List && (mode247 === "on" || mode247 === "auto")) { - // 24/7 mode active — just leave the player, no auto-rejoin. - // Users can re-invoke the play command to restart the bot in the channel. - logger.voice247(`[AutoLeave] 24/7 channel ${homeChannelId} (mode ${mode247}) auto-left — no auto-rejoin (recovery system removed).`); + // 24/7 mode active — auto-rejoin the channel after a delay + const rejoinDelay = this.timers?.rejoin247Delay ?? 3_000; + logger.voice247(`[AutoLeave] 24/7 channel ${homeChannelId} (mode ${mode247}) auto-left — scheduling rejoin in ${rejoinDelay}ms`); + setTimeout(async () => { + try { + if (typeof this.commands?.context?._spawnPlayer === "function") { + await this.commands.context._spawnPlayer(guildId, homeChannelId); + logger.voice247(`[AutoLeave] 24/7 auto-rejoin succeeded for channel ${homeChannelId}`); + } else { + logger.warn(`[AutoLeave] 24/7 auto-rejoin failed: _spawnPlayer not available`); + } + } catch (e) { + logger.warn(`[AutoLeave] 24/7 auto-rejoin failed for channel ${homeChannelId}: ${e.message}`); + } + }, rejoinDelay); } else { // Not 24/7 — send inactivity message const prefix = (() => {