Skip to content

Commit

Permalink
fix(Player): fix node not exclude after disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
Fab1o0107 committed Dec 29, 2023
1 parent 3f46b3d commit 24328a6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/guild/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,18 @@ export class Player extends EventEmitter {
* @returns true if the player was moved, false if not
*/
public async movePlayer(name?: string): Promise<boolean> {
const node = this.node.manager.nodes.get(name!) ?? this.node.manager.idealNode;
const idealExcludeCurrentNode = [...this.node.manager.nodes.values()]
.filter(node => node.name !== this.node.name && node.state === State.Connected)
.sort((a, b) => a.penalties - b.penalties)
.shift();
const node = this.node.manager.nodes.get(name!) ?? idealExcludeCurrentNode;

if (!node || node.name === this.node.name) return false;
if (node.state !== State.Connected) throw new Error("No available nodes to move to");

let lastNode = this.node.manager.nodes.get(this.node.name);
this._prepareMove = true;

if (!lastNode || lastNode.state !== State.Connected) lastNode = this.node.manager.idealNode;
if (!lastNode || lastNode.state !== State.Connected) lastNode = idealExcludeCurrentNode;

const ICurrentDataCache = await this.node.manager.redis?.get(RedisKey.NodePlayers(this.node.name.toLowerCase()));
const currentDataCache = ICurrentDataCache ? (JSON.parse(ICurrentDataCache) as VoiceChannelOptions[]) : [];
Expand All @@ -222,11 +225,13 @@ export class Player extends EventEmitter {
1
);

await this.destroyPlayer();
this._prepareMove = true;

await this.node.manager.redis?.set(
RedisKey.NodePlayers(this.node.name.toLowerCase()),
JSON.stringify(currentDataCache)
);
if (this.node.state === State.Connected) await this.destroyPlayer();

try {
this.node = node;
Expand Down

0 comments on commit 24328a6

Please sign in to comment.