This commit is contained in:
NoLogicAlan committed 2026-05-14 16:24:27 +03:00
1 parent c964eaaddc
commit e3a0f071bc
1 file changed
+29 -9
+29 -9
View File
@@ -54,6 +54,7 @@ export class RecoveryManager {
/** @type {Map<string, number>} Guild ID → consecutive 401 retry count. /** @type {Map<string, number>} Guild ID → consecutive 401 retry count.
* Used for exponential backoff: 1st retry after 15s, 2nd after 45s, 3rd = ban. */ * Used for exponential backoff: 1st retry after 15s, 2nd after 45s, 3rd = ban. */
this._guild401Retries = new Map(); this._guild401Retries = new Map();
this._spawnQueue = Promise.resolve();
// ── Expose convenience methods on the Remix instance ────────────────────── // ── Expose convenience methods on the Remix instance ──────────────────────
remix._spawnPlayer = this.spawnPlayer.bind(this); remix._spawnPlayer = this.spawnPlayer.bind(this);
@@ -72,6 +73,29 @@ export class RecoveryManager {
return this._botReadyAt > 0 && (Date.now() - this._botReadyAt) < this.T.startupGuildGrace; return this._botReadyAt > 0 && (Date.now() - this._botReadyAt) < this.T.startupGuildGrace;
} }
_isGuildAvailable(guildId) {
const { remix } = this;
const cleanGuildId = String(guildId).replace(/\D/g, "");
return remix.client.guilds.has(guildId) || remix.client.guilds.has(cleanGuildId);
}
_enqueueSpawn(guildId, channelId, reason = "spawn") {
const cleanChannelId = this.normalizeChannelId(channelId);
if (!cleanChannelId) return Promise.resolve();
const run = async () => {
try {
await this.spawnPlayer(guildId, cleanChannelId);
} catch (e) {
logger.warn(`[PlayerSpawn] Scheduled ${reason} failed for ${cleanChannelId}:`, e?.message ?? e);
}
};
const queued = this._spawnQueue.then(run, run);
this._spawnQueue = queued.catch(() => {});
return queued;
}
_disable247Channel(guildId, channelId, reason = "disabled") { _disable247Channel(guildId, channelId, reason = "disabled") {
const { remix } = this; const { remix } = this;
const cleanGuildId = String(guildId).replace(/\D/g, ""); const cleanGuildId = String(guildId).replace(/\D/g, "");
@@ -178,9 +202,7 @@ export class RecoveryManager {
const timer = setTimeout(() => { const timer = setTimeout(() => {
this.scheduledSpawns.delete(cleanChannelId); this.scheduledSpawns.delete(cleanChannelId);
this.spawnPlayer(guildId, cleanChannelId).catch(e => { this._enqueueSpawn(guildId, cleanChannelId, reason);
logger.warn(`[PlayerSpawn] Scheduled ${reason} failed for ${cleanChannelId}:`, e?.message ?? e);
});
}, Math.max(0, delayMs)); }, Math.max(0, delayMs));
this.scheduledSpawns.set(cleanChannelId, { timer, guildId, reason }); this.scheduledSpawns.set(cleanChannelId, { timer, guildId, reason });
@@ -215,13 +237,9 @@ export class RecoveryManager {
// messages. The GuildCreate handler will schedule the 24/7 spawn for // messages. The GuildCreate handler will schedule the 24/7 spawn for
// late-arriving guilds. cleanStaleGuild is only appropriate when we // late-arriving guilds. cleanStaleGuild is only appropriate when we
// know for sure the bot was kicked (i.e. a GuildDelete was received). // know for sure the bot was kicked (i.e. a GuildDelete was received).
if (!remix.client.guilds.has(guildId) && !remix.client.guilds.has(cleanGuildId)) { if (!this._isGuildAvailable(guildId)) {
if (this._autoJoinRunning || !this._autoJoinDone || this._inStartupGuildGrace()) { if (this._autoJoinRunning || !this._autoJoinDone || this._inStartupGuildGrace()) {
// Startup phase — guild cache not populated yet, just skip // Startup phase — guild cache not populated yet, just skip quietly.
logger.recovery(
`[PlayerSpawn] Skipping channel ${cleanChannelId} in guild ${cleanGuildId} — ` +
`guild cache not populated yet (startup race). GuildCreate will schedule spawn.`
);
} else { } else {
// Runtime — bot was actually removed from the guild // Runtime — bot was actually removed from the guild
logger.recovery( logger.recovery(
@@ -528,6 +546,8 @@ export class RecoveryManager {
for (const [guildId, serverSettings] of remix.settingsMgr.guilds) { for (const [guildId, serverSettings] of remix.settingsMgr.guilds) {
try { try {
if (!this._isGuildAvailable(guildId)) continue;
const raw = serverSettings.get("stay_247"); const raw = serverSettings.get("stay_247");
if (!raw || raw === "none") continue; if (!raw || raw === "none") continue;