From bd60e7695ae144363a3721711fce0e2dc5cc09e6 Mon Sep 17 00:00:00 2001 From: Fab1o0107 Date: Tue, 26 Dec 2023 22:16:53 +0700 Subject: [PATCH] feat(Player): add redis cache handler for moving node --- src/guild/Player.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/guild/Player.ts b/src/guild/Player.ts index 4d6f9b4..452d153 100644 --- a/src/guild/Player.ts +++ b/src/guild/Player.ts @@ -2,8 +2,9 @@ import { EventEmitter } from "node:events"; import { Node } from "../node/index"; import { VoiceConnection } from "./VoiceConnection"; -import { OpCodes, State } from "../Constants"; +import { OpCodes, RedisKey, State } from "../Constants"; import { Exception, Track } from "../node/Rest"; +import { VoiceChannelOptions } from "../Icelink"; export type TrackEndReason = "cleanup" | "finished" | "loadFailed" | "replaced" | "stopped"; export type PlayerEventType = @@ -211,18 +212,54 @@ export class Player extends EventEmitter { if (!lastNode || lastNode.state !== State.Connected) lastNode = this.node.manager.idealNode; + const ICurrentDataCache = await this.node.manager.redis?.get(RedisKey.NodePlayers(this.node.name.toLowerCase())); + const currentDataCache = ICurrentDataCache ? (JSON.parse(ICurrentDataCache) as VoiceChannelOptions[]) : []; + + currentDataCache.splice( + currentDataCache.indexOf(currentDataCache.find(({ guildId: id }) => id === this.guildId)!), + 1 + ); + await this.destroyPlayer(); + await this.node.manager.redis?.set( + RedisKey.NodePlayers(this.node.name.toLowerCase()), + JSON.stringify(currentDataCache) + ); try { this.node = node; + const IDataCache = await this.node.manager.redis?.get(RedisKey.NodePlayers(this.node.name.toLowerCase())); + const dataCache = IDataCache ? (JSON.parse(IDataCache) as VoiceChannelOptions[]) : []; + + dataCache.push({ + guildId: this.guildId, + channelId: this.connection.channelId!, + shardId: this.connection.shardId, + deaf: this.connection.deafened, + mute: this.connection.muted + }); + await this.resumePlayer(); + await this.node.manager.redis?.set(RedisKey.NodePlayers(this.node.name.toLowerCase()), JSON.stringify(dataCache)); return true; } catch { this.node = lastNode!; + const IDataCache = await this.node.manager.redis?.get(RedisKey.NodePlayers(this.node.name.toLowerCase())); + const dataCache = IDataCache ? (JSON.parse(IDataCache) as VoiceChannelOptions[]) : []; + + dataCache.push({ + guildId: this.guildId, + channelId: this.connection.channelId!, + shardId: this.connection.shardId, + deaf: this.connection.deafened, + mute: this.connection.muted + }); + await this.resumePlayer(); + await this.node.manager.redis?.set(RedisKey.NodePlayers(this.node.name.toLowerCase()), JSON.stringify(dataCache)); return false; }