Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor move method in Player.ts #190

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/guild/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export enum PlayerEventType {
WEBSOCKET_CLOSED_EVENT = 'WebSocketClosedEvent'
}

export interface MoveOptions {
name?: string;
force?: boolean;
}

export interface Band {
band: number;
gain: number;
Expand Down Expand Up @@ -242,11 +247,12 @@ export class Player extends EventEmitter implements IPlayer {
}

/**
* Move player to another node
* @param name Name of node to move to, or the default ideal node
* @returns true if the player was moved, false if not
*/
public async move(name?: string): Promise<boolean> {
* Move player to another node
* @param options.name Name of node to move to, or the default ideal node
* @param options.force Force the move and ignore errors when destroying the original player fails (e.g. during Node disconnect)
* @returns true if the player was moved, false if not
*/
public async move({ name, force }: MoveOptions = {}): Promise<boolean> {
const connection = this.node.manager.connections.get(this.guildId);
const node = this.node.manager.nodes.get(name!) ?? this.node.manager.getIdealNode(connection);

Expand All @@ -259,7 +265,11 @@ export class Player extends EventEmitter implements IPlayer {
if (!lastNode || lastNode.state !== State.CONNECTED)
lastNode = this.node.manager.getIdealNode(connection);

await this.destroy();
if (!force) {
await this.destroy();
} else {
await this.destroy().catch(() => null);
}

try {
this.node = node;
Expand Down