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

feat: update Player move method to accept MoveOptions #187

Closed
34 changes: 22 additions & 12 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,10 +249,11 @@ export class Player extends EventEmitter {

/**
* Move player to another node
* @param name Name of node to move to, or the default ideal 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?: string): Promise<boolean> {
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 @@ -260,9 +266,13 @@ export class Player extends EventEmitter {
if (!lastNode || lastNode.state !== State.CONNECTED)
lastNode = this.node.manager.getIdealNode(connection);

await this.destroy();

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

try {
this.node = node;
await this.resume();
return true;
Expand Down Expand Up @@ -324,15 +334,15 @@ export class Player extends EventEmitter {
* Sets the filter volume of the player
* @param volume Target volume 0.0-5.0
*/
async setFilterVolume(volume: number): Promise<void> {
public setFilterVolume(volume: number): Promise<void> {
return this.setFilters({ volume });
}

/**
* Change the equalizer settings applied to the currently playing track
* @param equalizer An array of objects that conforms to the Bands type that define volumes at different frequencies
*/
public async setEqualizer(equalizer: Band[]): Promise<void> {
public setEqualizer(equalizer: Band[]): Promise<void> {
return this.setFilters({ equalizer });
}

Expand Down Expand Up @@ -435,7 +445,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 +469,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