From d4488a0fac7641ecbeb37f7e007690105e544714 Mon Sep 17 00:00:00 2001 From: Vexify Date: Tue, 15 Oct 2024 15:49:36 +0200 Subject: [PATCH] Your commit message here --- src/structures/Manager.ts | 81 +++++++++++++++------------------------ 1 file changed, 30 insertions(+), 51 deletions(-) diff --git a/src/structures/Manager.ts b/src/structures/Manager.ts index fde27e23..c296d896 100644 --- a/src/structures/Manager.ts +++ b/src/structures/Manager.ts @@ -103,7 +103,7 @@ export class Manager extends EventEmitter { textChannel: state.options.textChannel, voiceChannel: state.options.voiceChannel, selfDeafen: state.options.selfDeafen, - volume: state.options.volume, + volume: lavaPlayer.volume || state.options.volume, }; this.create(playerOptions); @@ -303,72 +303,51 @@ export class Manager extends EventEmitter { return this.options.usePriority ? this.priorityNode : this.options.useNode === "leastLoad" ? this.leastLoadNode.first() : this.leastPlayersNode.first(); } - private lastProcessedGuilds: Set = new Set(); private lastSaveTimes: Map = new Map(); - private saveInterval: number = 1000; - private saveQueues: Map = new Map(); + private eventBatchInterval: NodeJS.Timeout | null = null; + private eventBatchDuration: number = 1000; + private latestPlayerStates: Map = new Map(); /** Register savePlayerStates events */ private registerPlayerStateEvents(): void { const events: (keyof ManagerEvents)[] = ["playerStateUpdate", "playerDestroy"]; + for (const event of events) { - this.on(event, (player: Player) => this.handleEvent(event, player)); + this.on(event, (player: Player) => this.collectPlayerStateEvent(event, player)); } } - private handleEvent(event: keyof ManagerEvents, player: Player): void { - switch (event) { - case "playerDestroy": - this.lastSaveTimes.delete(player.guild); - this.players.delete(player.guild); - this.cleanupInactivePlayers(); - break; - case "playerStateUpdate": - this.queuePlayerStateSave(player); - break; - default: - this.savePlayerState(player.guild); - break; + /** Collects player state events */ + private collectPlayerStateEvent(event: keyof ManagerEvents, player: Player): void { + if (event === "playerDestroy") { + this.lastSaveTimes.delete(player.guild); + this.players.delete(player.guild); + this.cleanupInactivePlayers(); + } else if (event === "playerStateUpdate") { + // Store the latest player state for the guild + this.latestPlayerStates.set(player.guild, player); } - } - - /** Queues a player state save */ - private queuePlayerStateSave(player: Player): void { - const guildId = player.guild; - - // If the current guild is not being processed, save immediately - if (!this.lastProcessedGuilds.has(guildId)) { - this.lastProcessedGuilds.add(guildId); - this.savePlayerState(guildId); - - setTimeout(() => { - this.lastProcessedGuilds.delete(guildId); - this.processNextQueue(guildId); - }, this.saveInterval); - } else { - if (!this.saveQueues.has(guildId)) { - this.saveQueues.set(guildId, []); - } - this.saveQueues.get(guildId)!.push(player); + // Start the batch timer if it's not already running + if (!this.eventBatchInterval) { + this.eventBatchInterval = setTimeout(() => this.processBatchEvents(), this.eventBatchDuration); } } - /** Processes the next queued save for a specific guild */ - private processNextQueue(guildId: string): void { - const queue = this.saveQueues.get(guildId); - if (queue && queue.length > 0) { - const player = queue.shift()!; - this.savePlayerState(player.guild); + /** Processes the collected player state events */ + private processBatchEvents(): void { + if (this.eventBatchInterval) { + clearTimeout(this.eventBatchInterval); + this.eventBatchInterval = null; + } - if (queue.length === 0) { - this.saveQueues.delete(guildId); - } + // Save the latest player states for each guild + this.latestPlayerStates.forEach((player, guildId) => { + this.savePlayerState(guildId); // Perform a single write operation + }); - setTimeout(() => this.processNextQueue(guildId), this.saveInterval); - } else { - this.lastProcessedGuilds.delete(guildId); - } + // Clear the latest player states after processing + this.latestPlayerStates.clear(); } /**