Skip to content

Commit 7dc1b26

Browse files
committed
add playerClientUpdate event.
1 parent 9b354b2 commit 7dc1b26

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lavalink-client",
3-
"version": "2.9.1",
3+
"version": "2.9.2",
44
"description": "Easy, flexible and feature-rich lavalink@v4 Client. Both for Beginners and Proficients. - Supports NodeLink@v3 too.",
55
"keywords": [
66
"advanced",

src/structures/Filters.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { audioOutputsData, EQList } from "./Constants";
2+
import { safeStringify } from "./Utils";
3+
24
import type { Player } from "./Player";
35
import type {
46
AudioOutputs,
@@ -8,7 +10,6 @@ import type {
810
PlayerFilters,
911
TimescaleFilter,
1012
} from "./Types/Filters";
11-
import { safeStringify } from "./Utils";
1213
/**
1314
* The FilterManager for each player
1415
*/
@@ -236,6 +237,8 @@ export class FilterManager {
236237

237238
if (this.player.options.instaUpdateFiltersFix === true) this.filterUpdatedState = true;
238239

240+
this.player.triggerPlayerClientUpdate();
241+
239242
await this.player.node.updatePlayer({
240243
guildId: this.player.guildId,
241244
playerOptions: {
@@ -1045,6 +1048,7 @@ export class FilterManager {
10451048

10461049
if (this.player.options.instaUpdateFiltersFix === true) this.filterUpdatedState = true;
10471050

1051+
this.player.triggerPlayerClientUpdate();
10481052
await this.player.node.updatePlayer({
10491053
guildId: this.player.guildId,
10501054
playerOptions: {

src/structures/Player.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { DebugEvents } from "./Constants";
2-
import type { DestroyReasons } from "./Constants";
32
import { bandCampSearch } from "./CustomSearches/BandCampSearch";
43
import { FilterManager } from "./Filters";
4+
import { Queue, QueueSaver } from "./Queue";
5+
import { queueTrackEnd } from "./Utils";
6+
7+
import type { DestroyReasons } from "./Constants";
58
import type { LavalinkManager } from "./LavalinkManager";
69
import type { LavalinkNode } from "./Node";
710
import type { NodeLinkNode } from "./NodeLink";
8-
import { Queue, QueueSaver } from "./Queue";
911
import type { SponsorBlockSegment } from "./Types/Node";
1012
import type {
1113
anyObject,
@@ -17,7 +19,6 @@ import type {
1719
} from "./Types/Player";
1820
import type { Track, UnresolvedTrack } from "./Types/Track";
1921
import type { LavalinkPlayerVoiceOptions, LavaSearchQuery, SearchQuery } from "./Types/Utils";
20-
import { queueTrackEnd } from "./Utils";
2122
export class Player {
2223
/** Filter Manager per player */
2324
public filterManager: FilterManager;
@@ -480,6 +481,19 @@ export class Player {
480481
return this;
481482
}
482483

484+
/**
485+
* The old JSON of the player, used for the "playerClientUpdate" event, which is emitted on every update of the player via a function call, so that you can compare the old data with the new data and do something with it if you want to.
486+
*/
487+
public oldJSON:PlayerJson = this.toJSON();
488+
489+
/**
490+
* Emits the "playerClientUpdate" event, which is emitted on every update of the player via a function call, so that you can compare the old data with the new data and do something with it if you want to.
491+
*/
492+
public triggerPlayerClientUpdate() {
493+
this.LavalinkManager.emit("playerClientUpdate", this.oldJSON, this);
494+
this.oldJSON = this.toJSON();
495+
}
496+
483497
/**
484498
* Set the Volume for the Player
485499
* @param volume The Volume in percent
@@ -506,6 +520,8 @@ export class Player {
506520
),
507521
);
508522

523+
this.triggerPlayerClientUpdate();
524+
509525
const now = performance.now();
510526
if (this.LavalinkManager.options.playerOptions.applyVolumeAsFilter) {
511527
this._emitDebugEvent(DebugEvents.PlayerVolumeAsFilter, {
@@ -583,6 +599,7 @@ export class Player {
583599
this.paused = true;
584600
this.lastPositionChange = null; // needs to removed to not cause issues
585601
const now = performance.now();
602+
this.triggerPlayerClientUpdate();
586603
await this.node.updatePlayer({ guildId: this.guildId, playerOptions: { paused: true } });
587604
this.ping.lavalink = Math.round((performance.now() - now) / 10) / 100;
588605
// emit the event
@@ -597,6 +614,7 @@ export class Player {
597614
if (!this.paused) throw new Error("Player isn't paused - not able to resume.");
598615
this.paused = false;
599616
const now = performance.now();
617+
this.triggerPlayerClientUpdate();
600618
await this.node.updatePlayer({ guildId: this.guildId, playerOptions: { paused: false } });
601619
this.ping.lavalink = Math.round((performance.now() - now) / 10) / 100;
602620
// emit the event
@@ -625,6 +643,7 @@ export class Player {
625643
this.lastPosition = position;
626644

627645
const now = performance.now();
646+
this.triggerPlayerClientUpdate();
628647
await this.node.updatePlayer({ guildId: this.guildId, playerOptions: { position } });
629648
this.ping.lavalink = Math.round((performance.now() - now) / 10) / 100;
630649

@@ -639,6 +658,7 @@ export class Player {
639658
if (!["off", "track", "queue"].includes(repeatMode))
640659
throw new RangeError("Repeatmode must be either 'off', 'track', or 'queue'");
641660
this.repeatMode = repeatMode;
661+
this.triggerPlayerClientUpdate();
642662
return this;
643663
}
644664

@@ -1047,7 +1067,7 @@ export class Player {
10471067
nodeId: this.node?.id,
10481068
nodeSessionId: this.node?.sessionId,
10491069
ping: this.ping,
1050-
queue: this.queue.utils.toJSON(),
1070+
queue: this.queue?.utils?.toJSON?.(),
10511071
} as PlayerJson;
10521072
}
10531073
}

src/structures/Queue.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ManagerQueueOptions, QueueChangesWatcher, QueueStoreManager, StoredQueue } from "./Types/Queue";
22
import type { Track, UnresolvedTrack } from "./Types/Track";
33
import { ManagerUtils, MiniMap, QueueSymbol } from "./Utils";
4+
45
export class QueueSaver {
56
/**
67
* The queue store manager
@@ -240,8 +241,8 @@ export class Queue {
240241
* @returns {{current:Track|null, previous:Track[], tracks:Track[]}}The Queue, but in a raw State, which allows easier handling for the QueueStoreManager
241242
*/
242243
toJSON: (): StoredQueue => {
243-
if (this.previous.length > this.options.maxPreviousTracks)
244-
this.previous.splice(this.options.maxPreviousTracks, this.previous.length);
244+
if (this.previous?.length > this.options?.maxPreviousTracks)
245+
this.previous?.splice(this.options?.maxPreviousTracks, this.previous.length);
245246
return {
246247
current: this.current ? { ...this.current } : null,
247248
previous: this.previous ? [...this.previous] : [],

src/structures/Types/Manager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ export interface LavalinkManagerEvents<CustomPlayerT extends Player = Player> {
104104
*/
105105
playerUpdate: (oldPlayerJson: PlayerJson, newPlayer: CustomPlayerT) => void;
106106

107+
108+
/**
109+
* Always emits when the player (on client side) got updated via a function-call.
110+
* This is useful for example, if you want to save the player data on every update, or similar.
111+
* @event Manager#playerClientUpdate
112+
*
113+
* Emits only when you call one of those functions:
114+
* player.pause()
115+
* player.resume()
116+
* player.seek()
117+
* player.setRepeatMode()
118+
* player.setVolume()
119+
* and on every call of the filterManager.
120+
*/
121+
playerClientUpdate: (oldPlayerJson: PlayerJson, newPlayer: CustomPlayerT) => void;
122+
107123
/**
108124
* Emitted when the player's selfMuted or serverMuted state changed (true -> false | false -> true)
109125
* @event Manager#playerMuteChange

0 commit comments

Comments
 (0)