Skip to content

Commit

Permalink
chore: update Player move method to accept MoveOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
flav-code committed Jul 28, 2024
1 parent 6086a99 commit 657abed
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 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 @@ -226,8 +231,8 @@ export class Player extends EventEmitter {
return {
guildId: this.guildId,
playerOptions: {
track: {
encoded: this.track
track: {
encoded: this.track
},
position: this.position,
paused: this.paused,
Expand All @@ -244,12 +249,12 @@ export class Player extends EventEmitter {

/**
* Move player to another node
* @param name Name of node to move to, or the default ideal node
* @param opts An object that conforms to the MoveOptions type
* @returns true if the player was moved, false if not
*/
public async move(name?: string): Promise<boolean> {
public async move(opts: MoveOptions = { force: false }): Promise<boolean> {
const connection = this.node.manager.connections.get(this.guildId);
const node = this.node.manager.nodes.get(name!) || this.node.manager.getIdealNode(connection);
const node = this.node.manager.nodes.get(opts.name!) || this.node.manager.getIdealNode(connection);

if (!node && ![ ...this.node.manager.nodes.values() ].some(node => node.state === State.CONNECTED))
throw new Error('No available nodes to move to');
Expand All @@ -260,9 +265,12 @@ export class Player extends EventEmitter {
if (!lastNode || lastNode.state !== State.CONNECTED)
lastNode = this.node.manager.getIdealNode(connection);

await this.destroy();

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

try {
this.node = node;
await this.resume();
return true;
Expand Down Expand Up @@ -435,7 +443,7 @@ export class Player extends EventEmitter {
public async resume(options: ResumeOptions = {}, noReplace: boolean = false): Promise<void> {
const data = this.data;

if (typeof options.position === 'number')
if (typeof options.position === 'number')
data.playerOptions.position = options.position;
if (typeof options.endTime === 'number')
data.playerOptions.endTime = options.endTime;
Expand All @@ -459,11 +467,11 @@ export class Player extends EventEmitter {
guildId: this.guildId,
noReplace,
playerOptions
}
};

await this.node.rest.updatePlayer(data);

if (!noReplace) this.paused = false
if (!noReplace) this.paused = false;

if (playerOptions.filters) {
const filters = { ...this.filters, ...playerOptions.filters };
Expand Down

0 comments on commit 657abed

Please sign in to comment.