lavalink-client 2.7.1 → 2.7.2

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.js CHANGED
@@ -47,6 +47,7 @@ __export(index_exports, {
47
47
  Queue: () => Queue,
48
48
  QueueSaver: () => QueueSaver,
49
49
  QueueSymbol: () => QueueSymbol,
50
+ ReconnectionState: () => ReconnectionState,
50
51
  SourceLinksRegexes: () => SourceLinksRegexes,
51
52
  TrackSymbol: () => TrackSymbol,
52
53
  UnresolvedTrackSymbol: () => UnresolvedTrackSymbol,
@@ -367,6 +368,15 @@ var import_events = require("events");
367
368
  var import_path = require("path");
368
369
  var import_ws = __toESM(require("ws"));
369
370
 
371
+ // src/structures/Types/Node.ts
372
+ var ReconnectionState = /* @__PURE__ */ ((ReconnectionState2) => {
373
+ ReconnectionState2["IDLE"] = "IDLE";
374
+ ReconnectionState2["RECONNECTING"] = "RECONNECTING";
375
+ ReconnectionState2["PENDING"] = "PENDING";
376
+ ReconnectionState2["DESTROYING"] = "DESTROYING";
377
+ return ReconnectionState2;
378
+ })(ReconnectionState || {});
379
+
370
380
  // src/structures/Utils.ts
371
381
  var import_node_url = require("url");
372
382
  var import_types = require("util/types");
@@ -1070,12 +1080,14 @@ var LavalinkNode = class {
1070
1080
  resuming = { enabled: true, timeout: null };
1071
1081
  /** Actual Lavalink Information of the Node */
1072
1082
  info = null;
1083
+ /** current state of the Reconnections */
1084
+ reconnectionState = "IDLE" /* IDLE */;
1073
1085
  /** The Node Manager of this Node */
1074
1086
  NodeManager = null;
1075
1087
  /** The Reconnection Timeout */
1076
1088
  reconnectTimeout = void 0;
1077
- /** The Reconnection Attempt counter */
1078
- reconnectAttempts = 1;
1089
+ /** The Reconnection Attempt counter (array of datetimes when it tried it.) */
1090
+ reconnectAttempts = [];
1079
1091
  /** The Socket of the Lavalink */
1080
1092
  socket = null;
1081
1093
  /** Version of what the Lavalink Server should be */
@@ -1098,6 +1110,7 @@ var LavalinkNode = class {
1098
1110
  secure: false,
1099
1111
  retryAmount: 5,
1100
1112
  retryDelay: 1e4,
1113
+ retryTimespan: -1,
1101
1114
  requestSignalTimeoutMS: 1e4,
1102
1115
  heartBeatInterval: 3e4,
1103
1116
  closeOnError: true,
@@ -1355,6 +1368,7 @@ var LavalinkNode = class {
1355
1368
  });
1356
1369
  }
1357
1370
  this.resetAckTimeouts(false, true);
1371
+ if (this.pingTimeout) clearTimeout(this.pingTimeout);
1358
1372
  this.pingTimeout = setTimeout(() => {
1359
1373
  this.pingTimeout = null;
1360
1374
  if (!this.socket) {
@@ -1861,47 +1875,66 @@ var LavalinkNode = class {
1861
1875
  get restAddress() {
1862
1876
  return `http${this.options.secure ? "s" : ""}://${this.options.host}:${this.options.port}`;
1863
1877
  }
1878
+ /**
1879
+ * If already trying to reconnect or pending, return
1880
+ */
1881
+ get isNodeReconnecting() {
1882
+ return this.reconnectionState !== "IDLE" /* IDLE */;
1883
+ }
1864
1884
  /**
1865
1885
  * Reconnect to the lavalink node
1866
- * @param instaReconnect @default false wether to instantly try to reconnect
1886
+ * @param force @default false Wether to instantly try to reconnect (force it)
1867
1887
  * @returns void
1868
1888
  *
1869
1889
  * @example
1870
1890
  * ```ts
1871
- * await player.node.reconnect();
1891
+ * await player.node.reconnect(true); //true forcefully trys the reconnect
1872
1892
  * ```
1873
1893
  */
1874
- reconnect(instaReconnect = false) {
1894
+ reconnect(force = false) {
1895
+ if (this.isNodeReconnecting) {
1896
+ return;
1897
+ }
1898
+ this.reconnectionState = "PENDING" /* PENDING */;
1875
1899
  this.NodeManager.emit("reconnectinprogress", this);
1876
- if (instaReconnect) {
1877
- if (this.reconnectAttempts >= this.options.retryAmount) {
1878
- const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1879
- this.NodeManager.emit("error", this, error);
1880
- return this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1881
- }
1882
- this.NodeManager.emit("reconnecting", this);
1883
- this.connect();
1884
- this.reconnectAttempts++;
1900
+ if (force) {
1901
+ this.executeReconnect();
1885
1902
  return;
1886
1903
  }
1904
+ if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
1887
1905
  this.reconnectTimeout = setTimeout(() => {
1888
1906
  this.reconnectTimeout = null;
1889
- if (this.reconnectAttempts >= this.options.retryAmount) {
1890
- const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1891
- this.NodeManager.emit("error", this, error);
1892
- return this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1893
- }
1894
- this.NodeManager.emit("reconnecting", this);
1895
- this.connect();
1896
- this.reconnectAttempts++;
1907
+ this.executeReconnect();
1897
1908
  }, this.options.retryDelay || 1e3);
1898
1909
  }
1910
+ get reconnectionAttemptCount() {
1911
+ const maxAllowedTimestan = this.options.retryTimespan || -1;
1912
+ if (maxAllowedTimestan <= 0) return this.reconnectAttempts.length;
1913
+ return this.reconnectAttempts.filter((timestamp) => Date.now() - timestamp <= maxAllowedTimestan).length;
1914
+ }
1915
+ /**
1916
+ * Private Utility function to execute the reconnection
1917
+ */
1918
+ executeReconnect() {
1919
+ if (this.reconnectionAttemptCount >= this.options.retryAmount) {
1920
+ const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1921
+ this.reconnectionState = "DESTROYING" /* DESTROYING */;
1922
+ this.NodeManager.emit("error", this, error);
1923
+ this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1924
+ return;
1925
+ }
1926
+ this.reconnectAttempts.push(Date.now());
1927
+ this.reconnectionState = "RECONNECTING" /* RECONNECTING */;
1928
+ this.NodeManager.emit("reconnecting", this);
1929
+ this.connect();
1930
+ }
1899
1931
  /**
1900
1932
  * Private function to reset the reconnection attempts
1901
1933
  * @returns
1902
1934
  */
1903
1935
  resetReconnectionAttempts() {
1904
- this.reconnectAttempts = 1;
1936
+ this.reconnectionState = "IDLE" /* IDLE */;
1937
+ this.reconnectAttempts = [];
1905
1938
  clearTimeout(this.reconnectTimeout);
1906
1939
  this.reconnectTimeout = null;
1907
1940
  return;
@@ -1988,6 +2021,7 @@ var LavalinkNode = class {
1988
2021
  error(error) {
1989
2022
  if (!error) return;
1990
2023
  this.NodeManager.emit("error", this, error);
2024
+ this.reconnect();
1991
2025
  if (this.options.closeOnError) {
1992
2026
  if (this.heartBeatInterval) clearInterval(this.heartBeatInterval);
1993
2027
  if (this.pingTimeout) clearTimeout(this.pingTimeout);
@@ -2421,10 +2455,7 @@ var LavalinkNode = class {
2421
2455
  });
2422
2456
  }
2423
2457
  this.NodeManager.LavalinkManager.emit("playerQueueEmptyStart", player, this.NodeManager.LavalinkManager.options.playerOptions.onEmptyQueue?.destroyAfterMs);
2424
- if (player.get("internal_queueempty")) {
2425
- clearTimeout(player.get("internal_queueempty"));
2426
- player.set("internal_queueempty", void 0);
2427
- }
2458
+ if (player.get("internal_queueempty")) clearTimeout(player.get("internal_queueempty"));
2428
2459
  player.set("internal_queueempty", setTimeout(() => {
2429
2460
  player.set("internal_queueempty", void 0);
2430
2461
  if (player.queue.current) {
@@ -5228,6 +5259,7 @@ var LavalinkManager = class extends import_events2.EventEmitter {
5228
5259
  Queue,
5229
5260
  QueueSaver,
5230
5261
  QueueSymbol,
5262
+ ReconnectionState,
5231
5263
  SourceLinksRegexes,
5232
5264
  TrackSymbol,
5233
5265
  UnresolvedTrackSymbol,
package/dist/index.mjs CHANGED
@@ -307,6 +307,15 @@ import { EventEmitter } from "events";
307
307
  import { isAbsolute } from "path";
308
308
  import WebSocket from "ws";
309
309
 
310
+ // src/structures/Types/Node.ts
311
+ var ReconnectionState = /* @__PURE__ */ ((ReconnectionState2) => {
312
+ ReconnectionState2["IDLE"] = "IDLE";
313
+ ReconnectionState2["RECONNECTING"] = "RECONNECTING";
314
+ ReconnectionState2["PENDING"] = "PENDING";
315
+ ReconnectionState2["DESTROYING"] = "DESTROYING";
316
+ return ReconnectionState2;
317
+ })(ReconnectionState || {});
318
+
310
319
  // src/structures/Utils.ts
311
320
  import { URL as URL2 } from "url";
312
321
  import { isRegExp } from "util/types";
@@ -1010,12 +1019,14 @@ var LavalinkNode = class {
1010
1019
  resuming = { enabled: true, timeout: null };
1011
1020
  /** Actual Lavalink Information of the Node */
1012
1021
  info = null;
1022
+ /** current state of the Reconnections */
1023
+ reconnectionState = "IDLE" /* IDLE */;
1013
1024
  /** The Node Manager of this Node */
1014
1025
  NodeManager = null;
1015
1026
  /** The Reconnection Timeout */
1016
1027
  reconnectTimeout = void 0;
1017
- /** The Reconnection Attempt counter */
1018
- reconnectAttempts = 1;
1028
+ /** The Reconnection Attempt counter (array of datetimes when it tried it.) */
1029
+ reconnectAttempts = [];
1019
1030
  /** The Socket of the Lavalink */
1020
1031
  socket = null;
1021
1032
  /** Version of what the Lavalink Server should be */
@@ -1038,6 +1049,7 @@ var LavalinkNode = class {
1038
1049
  secure: false,
1039
1050
  retryAmount: 5,
1040
1051
  retryDelay: 1e4,
1052
+ retryTimespan: -1,
1041
1053
  requestSignalTimeoutMS: 1e4,
1042
1054
  heartBeatInterval: 3e4,
1043
1055
  closeOnError: true,
@@ -1295,6 +1307,7 @@ var LavalinkNode = class {
1295
1307
  });
1296
1308
  }
1297
1309
  this.resetAckTimeouts(false, true);
1310
+ if (this.pingTimeout) clearTimeout(this.pingTimeout);
1298
1311
  this.pingTimeout = setTimeout(() => {
1299
1312
  this.pingTimeout = null;
1300
1313
  if (!this.socket) {
@@ -1801,47 +1814,66 @@ var LavalinkNode = class {
1801
1814
  get restAddress() {
1802
1815
  return `http${this.options.secure ? "s" : ""}://${this.options.host}:${this.options.port}`;
1803
1816
  }
1817
+ /**
1818
+ * If already trying to reconnect or pending, return
1819
+ */
1820
+ get isNodeReconnecting() {
1821
+ return this.reconnectionState !== "IDLE" /* IDLE */;
1822
+ }
1804
1823
  /**
1805
1824
  * Reconnect to the lavalink node
1806
- * @param instaReconnect @default false wether to instantly try to reconnect
1825
+ * @param force @default false Wether to instantly try to reconnect (force it)
1807
1826
  * @returns void
1808
1827
  *
1809
1828
  * @example
1810
1829
  * ```ts
1811
- * await player.node.reconnect();
1830
+ * await player.node.reconnect(true); //true forcefully trys the reconnect
1812
1831
  * ```
1813
1832
  */
1814
- reconnect(instaReconnect = false) {
1833
+ reconnect(force = false) {
1834
+ if (this.isNodeReconnecting) {
1835
+ return;
1836
+ }
1837
+ this.reconnectionState = "PENDING" /* PENDING */;
1815
1838
  this.NodeManager.emit("reconnectinprogress", this);
1816
- if (instaReconnect) {
1817
- if (this.reconnectAttempts >= this.options.retryAmount) {
1818
- const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1819
- this.NodeManager.emit("error", this, error);
1820
- return this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1821
- }
1822
- this.NodeManager.emit("reconnecting", this);
1823
- this.connect();
1824
- this.reconnectAttempts++;
1839
+ if (force) {
1840
+ this.executeReconnect();
1825
1841
  return;
1826
1842
  }
1843
+ if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
1827
1844
  this.reconnectTimeout = setTimeout(() => {
1828
1845
  this.reconnectTimeout = null;
1829
- if (this.reconnectAttempts >= this.options.retryAmount) {
1830
- const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1831
- this.NodeManager.emit("error", this, error);
1832
- return this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1833
- }
1834
- this.NodeManager.emit("reconnecting", this);
1835
- this.connect();
1836
- this.reconnectAttempts++;
1846
+ this.executeReconnect();
1837
1847
  }, this.options.retryDelay || 1e3);
1838
1848
  }
1849
+ get reconnectionAttemptCount() {
1850
+ const maxAllowedTimestan = this.options.retryTimespan || -1;
1851
+ if (maxAllowedTimestan <= 0) return this.reconnectAttempts.length;
1852
+ return this.reconnectAttempts.filter((timestamp) => Date.now() - timestamp <= maxAllowedTimestan).length;
1853
+ }
1854
+ /**
1855
+ * Private Utility function to execute the reconnection
1856
+ */
1857
+ executeReconnect() {
1858
+ if (this.reconnectionAttemptCount >= this.options.retryAmount) {
1859
+ const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`);
1860
+ this.reconnectionState = "DESTROYING" /* DESTROYING */;
1861
+ this.NodeManager.emit("error", this, error);
1862
+ this.destroy("NodeReconnectFail" /* NodeReconnectFail */);
1863
+ return;
1864
+ }
1865
+ this.reconnectAttempts.push(Date.now());
1866
+ this.reconnectionState = "RECONNECTING" /* RECONNECTING */;
1867
+ this.NodeManager.emit("reconnecting", this);
1868
+ this.connect();
1869
+ }
1839
1870
  /**
1840
1871
  * Private function to reset the reconnection attempts
1841
1872
  * @returns
1842
1873
  */
1843
1874
  resetReconnectionAttempts() {
1844
- this.reconnectAttempts = 1;
1875
+ this.reconnectionState = "IDLE" /* IDLE */;
1876
+ this.reconnectAttempts = [];
1845
1877
  clearTimeout(this.reconnectTimeout);
1846
1878
  this.reconnectTimeout = null;
1847
1879
  return;
@@ -1928,6 +1960,7 @@ var LavalinkNode = class {
1928
1960
  error(error) {
1929
1961
  if (!error) return;
1930
1962
  this.NodeManager.emit("error", this, error);
1963
+ this.reconnect();
1931
1964
  if (this.options.closeOnError) {
1932
1965
  if (this.heartBeatInterval) clearInterval(this.heartBeatInterval);
1933
1966
  if (this.pingTimeout) clearTimeout(this.pingTimeout);
@@ -2361,10 +2394,7 @@ var LavalinkNode = class {
2361
2394
  });
2362
2395
  }
2363
2396
  this.NodeManager.LavalinkManager.emit("playerQueueEmptyStart", player, this.NodeManager.LavalinkManager.options.playerOptions.onEmptyQueue?.destroyAfterMs);
2364
- if (player.get("internal_queueempty")) {
2365
- clearTimeout(player.get("internal_queueempty"));
2366
- player.set("internal_queueempty", void 0);
2367
- }
2397
+ if (player.get("internal_queueempty")) clearTimeout(player.get("internal_queueempty"));
2368
2398
  player.set("internal_queueempty", setTimeout(() => {
2369
2399
  player.set("internal_queueempty", void 0);
2370
2400
  if (player.queue.current) {
@@ -5167,6 +5197,7 @@ export {
5167
5197
  Queue,
5168
5198
  QueueSaver,
5169
5199
  QueueSymbol,
5200
+ ReconnectionState,
5170
5201
  SourceLinksRegexes,
5171
5202
  TrackSymbol,
5172
5203
  UnresolvedTrackSymbol,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lavalink-client",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
4
4
  "description": "Easy, flexible and feature-rich lavalink@v4 Client. Both for Beginners and Proficients.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",