Skip to content

Commit

Permalink
fix(Player): fix force event emit while node is moving
Browse files Browse the repository at this point in the history
  • Loading branch information
Fab1o0107 committed Dec 26, 2023
1 parent bd60e76 commit b747cbf
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/guild/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class Player extends EventEmitter {
* @internal
*/
private _encodedTrack: string | null = null;
private _prepareMove = false;

/**
* Creates a new Player instance on {@link Node}.
Expand All @@ -184,7 +185,8 @@ export class Player extends EventEmitter {

Object.defineProperties(this, {
guildId: { enumerable: true, writable: false },
_encodedTrack: { enumerable: false, writable: true }
_encodedTrack: { enumerable: false, writable: true },
_prepareMove: { enumerable: false, writable: true }
});
}

Expand All @@ -204,11 +206,11 @@ export class Player extends EventEmitter {
public async movePlayer(name?: string): Promise<boolean> {
const node = this.node.manager.nodes.get(name!) ?? this.node.manager.idealNode;

if (!node && ![...this.node.manager.nodes.values()].some(({ state }) => state === State.Connected))
throw new Error("No available nodes to move to");
if (!node || node.name === this.node.name || node.state !== State.Connected) return false;
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;

Expand Down Expand Up @@ -243,6 +245,8 @@ export class Player extends EventEmitter {
await this.resumePlayer();
await this.node.manager.redis?.set(RedisKey.NodePlayers(this.node.name.toLowerCase()), JSON.stringify(dataCache));

this._prepareMove = false;

return true;
} catch {
this.node = lastNode!;
Expand All @@ -261,6 +265,8 @@ export class Player extends EventEmitter {
await this.resumePlayer();
await this.node.manager.redis?.set(RedisKey.NodePlayers(this.node.name.toLowerCase()), JSON.stringify(dataCache));

this._prepareMove = false;

return false;
}
}
Expand Down Expand Up @@ -523,6 +529,8 @@ export class Player extends EventEmitter {
* @internal
*/
public onPlayerUpdate(data: { state: { position: number; ping: number; connected: boolean } }): void {
if (this._prepareMove) return undefined;

const { position, ping, connected } = data.state;

this.position = position;
Expand All @@ -538,6 +546,8 @@ export class Player extends EventEmitter {
* @internal
*/
public onPlayerEvent(data: { type: string; track: Track }): void {
if (this._prepareMove) return undefined;

switch (data.type) {
case "TrackStartEvent":
this.trackIdentifier = data.track.info.identifier;
Expand Down

0 comments on commit b747cbf

Please sign in to comment.