Skip to content

Commit

Permalink
chore: change player naming scheme & code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Deivu committed Nov 13, 2023
1 parent ea4bb60 commit 94ea562
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
17 changes: 11 additions & 6 deletions src/Shoukaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,18 @@ export class Shoukaku extends EventEmitter {
* @returns The destroyed / disconnected player or undefined if none
* @internal
*/
public async leaveVoiceChannel(guildId: string): Promise<Player|undefined> {
this.connections.get(guildId)?.disconnect();
this.connections.delete(guildId);
public async leaveVoiceChannel(guildId: string): Promise<void> {
const connection = this.connections.get(guildId);
if (connection) {
connection.disconnect();
this.connections.delete(guildId);
}
const player = this.players.get(guildId);
if (player) await player.destroy(true);
this.players.delete(guildId);
return player;
if (player) {
await player.destroyPlayer();
player.clean();
this.players.delete(guildId);
}
}

/**
Expand Down
27 changes: 9 additions & 18 deletions src/guild/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Player extends EventEmitter {
* @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> {
public async movePlayer(name?: string): Promise<boolean> {
const connection = this.node.manager.connections.get(this.guildId)!;
const node = this.node.manager.nodes.get(name!) || this.node.manager.options.nodeResolver(this.node.manager.nodes, connection);
if (!node && ![ ...this.node.manager.nodes.values() ].some(node => node.state === State.CONNECTED))
Expand All @@ -277,23 +277,22 @@ export class Player extends EventEmitter {
let lastNode = this.node.manager.nodes.get(this.node.name);
if (!lastNode || lastNode.state !== State.CONNECTED)
lastNode = ShoukakuDefaults.nodeResolver(this.node.manager.nodes, connection);
await this.destroy();
await this.destroyPlayer();
try {
this.node = node;
await this.resume();
await this.resumePlayer();
return true;
} catch (error) {
this.node = lastNode!;
await this.resume();
await this.resumePlayer();
return false;
}
}

/**
* Destroys the player in remote lavalink side
*/
public async destroy(clean: boolean = false): Promise<void> {
if (clean) this.clean();
public async destroyPlayer(): Promise<void> {
await this.node.rest.destroyPlayer(this.guildId);
}

Expand Down Expand Up @@ -496,20 +495,20 @@ export class Player extends EventEmitter {
* Resumes the current track
* @param options An object that conforms to ResumeOptions that specify behavior on resuming
*/
public async resume(options: ResumeOptions = {}): Promise<void> {
public async resumePlayer(options: ResumeOptions = {}): Promise<void> {
const data = this.playerData;
if (options.noReplace) data.noReplace = options.noReplace;
if (options.startTime) data.playerOptions.position = options.startTime;
if (options.endTime) data.playerOptions.position;
if (options.pause) data.playerOptions.paused = options.pause;
await this.update(data);
await this.updatePlayer(data);
this.emit('resumed', this);
}

/**
* If you want to update the whole player yourself, sends raw update player info to lavalink
*/
public async update(updatePlayer: UpdatePlayerInfo): Promise<void> {
public async updatePlayer(updatePlayer: UpdatePlayerInfo): Promise<void> {
const data = { ...updatePlayer, ...{ guildId: this.guildId, sessionId: this.node.sessionId! }};
await this.node.rest.updatePlayer(data);
if (updatePlayer.playerOptions) {
Expand All @@ -523,19 +522,11 @@ export class Player extends EventEmitter {
}

/**
* Remove all event listeners on this instance
* Cleans this player instance
* @internal
*/
public clean(): void {
this.removeAllListeners();
this.reset();
}

/**
* Reset the track, position and filters on this instance to defaults
* @internal
*/
public reset(): void {
this.track = null;
this.volume = 100;
this.position = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/node/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class Node extends EventEmitter {
* Destroys the websocket connection
* @internal
*/
private destroy(move: boolean, count: number = 0): void {
private destroy(count: number = 0): void {
this.ws?.removeAllListeners();
this.ws?.close();
this.ws = null;
Expand All @@ -341,14 +341,14 @@ export class Node extends EventEmitter {
*/
private async clean(): Promise<void> {
const move = this.manager.options.moveOnDisconnect;
if (!move) return this.destroy(false);
if (!move) return this.destroy();
let count = 0;
try {
count = await this.movePlayers();
} catch (error) {
this.error(error);
} finally {
this.destroy(count > 0, count);
this.destroy(count);
}
}

Expand All @@ -358,7 +358,7 @@ export class Node extends EventEmitter {
*/
private async reconnect(): Promise<void> {
if (this.state === State.RECONNECTING) return;
if (this.state !== State.DISCONNECTED) this.destroy(false);
if (this.state !== State.DISCONNECTED) this.destroy();
this.state = State.RECONNECTING;
this.reconnects++;
this.emit('reconnecting', this.manager.options.reconnectTries - this.reconnects, this.manager.options.reconnectInterval);
Expand All @@ -384,7 +384,7 @@ export class Node extends EventEmitter {
}

await Promise.allSettled([
...playersWithData.map(player => player.resume()),
...playersWithData.map(player => player.resumePlayer()),
...playersWithoutData.map(player => this.manager.leaveVoiceChannel(player.guildId))
]);
}
Expand All @@ -395,7 +395,7 @@ export class Node extends EventEmitter {
*/
private async movePlayers(): Promise<number> {
const players = [ ...this.manager.players.values() ];
const data = await Promise.allSettled(players.map(player => player.move()));
const data = await Promise.allSettled(players.map(player => player.movePlayer()));
return data.filter(results => results.status === 'fulfilled').length;
}
}

0 comments on commit 94ea562

Please sign in to comment.