magmastream 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -833,6 +833,8 @@ declare class Manager extends EventEmitter {
833
833
  /** The options that were set. */
834
834
  readonly options: ManagerOptions;
835
835
  private initiated;
836
+ /** Returns the nodes that has the least load. */
837
+ private get leastLoadNode();
836
838
  /** Returns the nodes that has the least amount of players. */
837
839
  private get leastPlayersNode();
838
840
  /** Returns a node based on priority. */
@@ -908,8 +910,10 @@ interface Payload {
908
910
  };
909
911
  }
910
912
  interface ManagerOptions {
911
- /** Use priority mode over least amount of player? */
913
+ /** Use priority mode over least amount of player or load? */
912
914
  usePriority?: boolean;
915
+ /** Use the least amount of players or least load? */
916
+ useNode?: "leastLoad" | "leastPlayers";
913
917
  /** The array of nodes to connect to. */
914
918
  nodes?: NodeOptions[];
915
919
  /** The client ID to use. */
@@ -7,7 +7,6 @@ const Utils_1 = require("./Utils");
7
7
  const collection_1 = require("@discordjs/collection");
8
8
  const events_1 = require("events");
9
9
  const managerCheck_1 = tslib_1.__importDefault(require("../utils/managerCheck"));
10
- const REQUIRED_KEYS = ["event", "guild_id", "op", "sessionId"];
11
10
  /**
12
11
  * The main hub for interacting with Lavalink and using Magmastream,
13
12
  */
@@ -28,6 +27,20 @@ class Manager extends events_1.EventEmitter {
28
27
  /** The options that were set. */
29
28
  options;
30
29
  initiated = false;
30
+ /** Returns the nodes that has the least load. */
31
+ get leastLoadNode() {
32
+ return this.nodes
33
+ .filter((node) => node.connected)
34
+ .sort((a, b) => {
35
+ const aload = a.stats.cpu
36
+ ? (a.stats.cpu.lavalinkLoad / a.stats.cpu.cores) * 100
37
+ : 0;
38
+ const bload = b.stats.cpu
39
+ ? (b.stats.cpu.lavalinkLoad / b.stats.cpu.cores) * 100
40
+ : 0;
41
+ return aload - bload;
42
+ });
43
+ }
31
44
  /** Returns the nodes that has the least amount of players. */
32
45
  get leastPlayersNode() {
33
46
  return this.nodes
@@ -50,13 +63,17 @@ class Manager extends events_1.EventEmitter {
50
63
  return node;
51
64
  }
52
65
  }
53
- return this.leastPlayersNode.first();
66
+ return this.options.useNode === "leastLoad"
67
+ ? this.leastLoadNode.first()
68
+ : this.leastPlayersNode.first();
54
69
  }
55
70
  /** Returns the node to use. */
56
71
  get useableNodes() {
57
72
  return this.options.usePriority
58
73
  ? this.priorityNode
59
- : this.leastPlayersNode.first();
74
+ : this.options.useNode === "leastLoad"
75
+ ? this.leastLoadNode.first()
76
+ : this.leastPlayersNode.first();
60
77
  }
61
78
  /**
62
79
  * Initiates the Manager class.
@@ -87,6 +104,7 @@ class Manager extends events_1.EventEmitter {
87
104
  usePriority: false,
88
105
  clientName: "Magmastream",
89
106
  defaultSearchPlatform: "youtube",
107
+ useNode: "leastPlayers",
90
108
  ...options,
91
109
  };
92
110
  if (this.options.plugins) {
@@ -256,49 +274,38 @@ class Manager extends events_1.EventEmitter {
256
274
  */
257
275
  async updateVoiceState(data) {
258
276
  if ("t" in data &&
259
- !["VOICE_STATE_UPDATE", "VOICE_SERVER_UPDATE"].includes(data.t)) {
277
+ !["VOICE_STATE_UPDATE", "VOICE_SERVER_UPDATE"].includes(data.t))
260
278
  return;
261
- }
262
279
  const update = "d" in data ? data.d : data;
263
- if (!update || (!("token" in update) && !("session_id" in update))) {
280
+ if (!update || (!("token" in update) && !("session_id" in update)))
264
281
  return;
265
- }
266
282
  const player = this.players.get(update.guild_id);
267
- if (!player) {
283
+ if (!player)
268
284
  return;
269
- }
270
285
  if ("token" in update) {
271
- /* voice server update */
272
286
  player.voiceState.event = update;
273
- }
274
- else {
275
- /* voice state update */
276
- if (update.user_id !== this.options.clientId) {
277
- return;
278
- }
279
- if (update.channel_id) {
280
- if (player.voiceChannel !== update.channel_id) {
281
- /* we moved voice channels. */
282
- this.emit("playerMove", player, player.voiceChannel, update.channel_id);
283
- }
284
- player.voiceState.sessionId = update.session_id;
285
- player.voiceChannel = update.channel_id;
286
- }
287
- else {
288
- /* player got disconnected. */
289
- this.emit("playerDisconnect", player, player.voiceChannel);
290
- player.voiceChannel = null;
291
- player.voiceState = Object.assign({});
292
- player.pause(true);
293
- }
294
- }
295
- if (REQUIRED_KEYS.every((key) => key in player.voiceState)) {
296
287
  const { sessionId, event: { token, endpoint }, } = player.voiceState;
297
288
  await player.node.rest.updatePlayer({
298
289
  guildId: player.guild,
299
290
  data: { voice: { token, endpoint, sessionId } },
300
291
  });
292
+ return;
293
+ }
294
+ if (update.user_id !== this.options.clientId)
295
+ return;
296
+ if (update.channel_id) {
297
+ if (player.voiceChannel !== update.channel_id) {
298
+ this.emit("playerMove", player, player.voiceChannel, update.channel_id);
299
+ }
300
+ player.voiceState.sessionId = update.session_id;
301
+ player.voiceChannel = update.channel_id;
302
+ return;
301
303
  }
304
+ this.emit("playerDisconnect", player, player.voiceChannel);
305
+ player.voiceChannel = null;
306
+ player.voiceState = Object.assign({});
307
+ player.destroy();
308
+ return;
302
309
  }
303
310
  }
304
311
  exports.Manager = Manager;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function managerCheck(options) {
4
4
  if (!options)
5
5
  throw new TypeError("ManagerOptions must not be empty.");
6
- const { autoPlay, clientId, clientName, defaultSearchPlatform, nodes, plugins, send, shards, trackPartial, usePriority, } = options;
6
+ const { autoPlay, clientId, clientName, defaultSearchPlatform, nodes, plugins, send, shards, trackPartial, usePriority, useNode, } = options;
7
7
  if (typeof autoPlay !== "undefined" && typeof autoPlay !== "boolean") {
8
8
  throw new TypeError('Manager option "autoPlay" must be a boolean.');
9
9
  }
@@ -42,5 +42,13 @@ function managerCheck(options) {
42
42
  }
43
43
  }
44
44
  }
45
+ if (typeof useNode !== "undefined") {
46
+ if (typeof useNode !== "string") {
47
+ throw new TypeError('Manager option "useNode" must be a string "leastLoad" or "leastPlayers".');
48
+ }
49
+ if (useNode !== "leastLoad" || useNode !== "leastLoad") {
50
+ throw new TypeError('Manager option must be either "leastLoad" or "leastPlayers".');
51
+ }
52
+ }
45
53
  }
46
54
  exports.default = managerCheck;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magmastream",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "A user-friendly Lavalink client designed for NodeJS.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,15 +16,15 @@
16
16
  "devDependencies": {
17
17
  "@favware/rollup-type-bundler": "^3.3.0",
18
18
  "@types/lodash": "^4.17.0",
19
- "@types/node": "^20.12.4",
19
+ "@types/node": "^20.12.7",
20
20
  "@types/ws": "^8.5.10",
21
- "@typescript-eslint/eslint-plugin": "^7.5.0",
22
- "@typescript-eslint/parser": "^7.5.0",
21
+ "@typescript-eslint/eslint-plugin": "^7.7.1",
22
+ "@typescript-eslint/parser": "^7.7.1",
23
23
  "eslint": "^8.57.0",
24
24
  "npm-run-all": "^4.1.5",
25
- "typedoc": "^0.25.12",
25
+ "typedoc": "^0.25.13",
26
26
  "typedoc-plugin-no-inherit": "^1.4.0",
27
- "typescript": "^5.4.4"
27
+ "typescript": "^5.4.5"
28
28
  },
29
29
  "dependencies": {
30
30
  "@discordjs/collection": "^2.0.0",
@@ -32,7 +32,7 @@
32
32
  "events": "^3.3.0",
33
33
  "lodash": "^4.17.21",
34
34
  "tslib": "^2.6.2",
35
- "ws": "^8.16.0"
35
+ "ws": "^8.17.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "discord.js": ">=13.0.0 <15.0.0"
@@ -87,4 +87,4 @@
87
87
  "homepage": "https://docs.blackforthosting.com",
88
88
  "author": "Abel Purnwasy",
89
89
  "license": "Apache-2.0"
90
- }
90
+ }