poru 5.1.0 → 5.3.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.
Files changed (83) hide show
  1. package/README.md +17 -13
  2. package/assets/poru.png +0 -0
  3. package/dist/src/Node/Node.d.ts +69 -34
  4. package/dist/src/Node/Node.js +205 -94
  5. package/dist/src/Node/Node.js.map +1 -1
  6. package/dist/src/Node/Rest.d.ts +24 -9
  7. package/dist/src/Node/Rest.js +33 -23
  8. package/dist/src/Node/Rest.js.map +1 -1
  9. package/dist/src/Player/Connection.d.ts +36 -4
  10. package/dist/src/Player/Connection.js +19 -6
  11. package/dist/src/Player/Connection.js.map +1 -1
  12. package/dist/src/Player/CustomFilters.d.ts +15 -17
  13. package/dist/src/Player/CustomFilters.js +44 -38
  14. package/dist/src/Player/CustomFilters.js.map +1 -1
  15. package/dist/src/Player/Filters.d.ts +23 -23
  16. package/dist/src/Player/Filters.js +56 -42
  17. package/dist/src/Player/Filters.js.map +1 -1
  18. package/dist/src/Player/Player.d.ts +189 -76
  19. package/dist/src/Player/Player.js +363 -110
  20. package/dist/src/Player/Player.js.map +1 -1
  21. package/dist/src/Poru.d.ts +221 -164
  22. package/dist/src/Poru.js +143 -110
  23. package/dist/src/Poru.js.map +1 -1
  24. package/dist/src/config.d.ts +1 -1
  25. package/dist/src/config.js +1 -1
  26. package/dist/src/guild/Queue.d.ts +17 -19
  27. package/dist/src/guild/Queue.js +16 -20
  28. package/dist/src/guild/Queue.js.map +1 -1
  29. package/dist/src/guild/Response.d.ts +12 -6
  30. package/dist/src/guild/Response.js +40 -18
  31. package/dist/src/guild/Response.js.map +1 -1
  32. package/dist/src/guild/Track.d.ts +15 -10
  33. package/dist/src/guild/Track.js +10 -5
  34. package/dist/src/guild/Track.js.map +1 -1
  35. package/package.json +4 -6
  36. package/src/Node/Node.ts +291 -132
  37. package/src/Node/Rest.ts +48 -35
  38. package/src/Player/Connection.ts +56 -14
  39. package/src/Player/CustomFilters.ts +55 -50
  40. package/src/Player/Filters.ts +71 -73
  41. package/src/Player/Player.ts +827 -520
  42. package/src/Poru.ts +304 -226
  43. package/src/config.ts +1 -1
  44. package/src/guild/Queue.ts +17 -22
  45. package/src/guild/Response.ts +114 -76
  46. package/src/guild/Track.ts +23 -17
  47. package/tsconfig.json +6 -1
  48. package/website/README.md +42 -0
  49. package/website/babel.config.js +3 -0
  50. package/website/docs/intro.md +53 -0
  51. package/website/docs/tutorial-basics/_category_.json +8 -0
  52. package/website/docs/tutorial-basics/congratulations.md +23 -0
  53. package/website/docs/tutorial-basics/create-a-blog-post.md +34 -0
  54. package/website/docs/tutorial-basics/create-a-document.md +57 -0
  55. package/website/docs/tutorial-basics/create-a-page.md +43 -0
  56. package/website/docs/tutorial-basics/deploy-your-site.md +31 -0
  57. package/website/docs/tutorial-basics/markdown-features.mdx +152 -0
  58. package/website/docs/tutorial-extras/_category_.json +7 -0
  59. package/website/docs/tutorial-extras/img/docsVersionDropdown.png +0 -0
  60. package/website/docs/tutorial-extras/img/localeDropdown.png +0 -0
  61. package/website/docs/tutorial-extras/manage-docs-versions.md +55 -0
  62. package/website/docs/tutorial-extras/translate-your-site.md +88 -0
  63. package/website/docusaurus.config.ts +154 -0
  64. package/website/package-lock.json +14848 -0
  65. package/website/package.json +48 -0
  66. package/website/sidebars.ts +31 -0
  67. package/website/src/components/HomepageFeatures/index.tsx +67 -0
  68. package/website/src/components/HomepageFeatures/styles.module.css +11 -0
  69. package/website/src/css/custom.css +30 -0
  70. package/website/src/pages/index.module.css +23 -0
  71. package/website/src/pages/index.tsx +43 -0
  72. package/website/src/pages/markdown-page.md +7 -0
  73. package/website/static/.nojekyll +0 -0
  74. package/website/static/CNAME +1 -0
  75. package/website/static/img/docusaurus-social-card.jpg +0 -0
  76. package/website/static/img/docusaurus.png +0 -0
  77. package/website/static/img/favicon.ico +0 -0
  78. package/website/static/img/logo.svg +1 -0
  79. package/website/static/img/poru.png +0 -0
  80. package/website/static/img/undraw_docusaurus_mountain.svg +171 -0
  81. package/website/static/img/undraw_docusaurus_react.svg +170 -0
  82. package/website/static/img/undraw_docusaurus_tree.svg +40 -0
  83. package/website/tsconfig.json +7 -0
package/README.md CHANGED
@@ -1,18 +1,18 @@
1
1
  <p align="center">
2
- <img src="https://cdn.discordapp.com/attachments/732987654165233744/987656504373026816/20220618_000923_0000.png" />
2
+ <img src="https://raw.githubusercontent.com/parasop/poru/v5/assets/poru.png" alt="Poru Logo" height="340" width="340"/>
3
3
  </p>
4
4
 
5
5
  <p align="center">
6
6
  <a href="https://discord.gg/Zmmc47Nrh8">
7
- <img src="https://img.shields.io/discord/567705326774779944?style=flat-square" alt="Discord">
7
+ <img src="https://img.shields.io/discord/567705326774779944?style=flat-square" alt="Discord"/>
8
8
  </a>
9
9
  <a href="https://www.npmjs.com/package/poru">
10
- <img src="https://img.shields.io/npm/v/poru?style=flat-square" alt="npm">
10
+ <img src="https://img.shields.io/npm/v/poru?style=flat-square" alt="npm"/>
11
11
  </a>
12
- <img src="https://img.shields.io/github/stars/parasop/poru?style=flat-square" alt="GitHub Stars">
13
- <img src="https://img.shields.io/github/issues-raw/parasop/poru?style=flat-square" alt="GitHub issues">
14
- <img src="https://img.shields.io/snyk/vulnerabilities/npm/poru?style=flat-square" alt="Snyk Vulnerabilities for npm package">
15
- <img src="https://img.shields.io/npm/l/poru?style=flat-square" alt="NPM">
12
+ <img src="https://img.shields.io/github/stars/parasop/poru?style=flat-square" alt="GitHub Stars"/>
13
+ <img src="https://img.shields.io/github/issues-raw/parasop/poru?style=flat-square" alt="GitHub issues"/>
14
+ <img src="https://img.shields.io/snyk/vulnerabilities/npm/poru?style=flat-square" alt="Snyk Vulnerabilities for npm package"/>
15
+ <img src="https://img.shields.io/npm/l/poru?style=flat-square" alt="NPM"/>
16
16
  </p>
17
17
 
18
18
  <p align="center">
@@ -21,8 +21,8 @@
21
21
 
22
22
  <p align="center">
23
23
  <a href="https://nodei.co/npm/poru/">
24
- <img src="https://nodei.co/npm/poru.png?downloads=true&downloadRank=true&stars=true" alt="Poru NPM Package">
25
- </a>
24
+ <img src="https://nodei.co/npm/poru.png?downloads=true&downloadRank=true&stars=true" alt="Poru NPM Package"/>
25
+ </a>
26
26
  </p>
27
27
 
28
28
  ## Table of contents
@@ -30,9 +30,13 @@
30
30
  - [Documentation](https://poru.js.org)
31
31
  - [Installation](#installation)
32
32
  - [About](#about)
33
- - [Example](https://github.com/parasop/poru-example)
33
+ - [Implementation Repo](#implementation-repo)
34
+ - [Basic Usage](#example-usage-basic-bot)
35
+ - [Plugins List](#plugins-list)
36
+ - [Help & Support](#need-help)
37
+ - [Example Bot](https://github.com/parasop/poru-example)
34
38
 
35
- # Installation
39
+ ## Installation
36
40
 
37
41
  ```bash
38
42
  # Using npm
@@ -42,7 +46,7 @@ npm install poru
42
46
  yarn add poru
43
47
  ```
44
48
 
45
- # About
49
+ ## About
46
50
 
47
51
  To use, you need a configured [Lavalink](https://github.com/lavalink-devs/Lavalink) instance.
48
52
 
@@ -169,4 +173,4 @@ Note: Open a PR to add your plugin here
169
173
 
170
174
  Feel free to join our [Discord server](https://discord.gg/Zmmc47Nrh8). Give us suggestions and advice about errors and new features.
171
175
 
172
- With ❤️ by [Paras](https://github.com/parasop).
176
+ With ❤️ by [Paras](https://github.com/parasop).
Binary file
@@ -1,27 +1,60 @@
1
- /// <reference types="node" />
2
1
  import { Poru, PoruOptions, NodeGroup } from "../Poru";
3
2
  import WebSocket from "ws";
4
3
  import { Rest } from "./Rest";
4
+ import { Severity } from "../guild/Response";
5
5
  export interface NodeStats {
6
6
  players: number;
7
7
  playingPlayers: number;
8
+ uptime: number;
8
9
  memory: {
9
- reservable: number;
10
- used: number;
11
10
  free: number;
11
+ used: number;
12
12
  allocated: number;
13
- };
14
- frameStats: {
15
- sent: number;
16
- deficit: number;
17
- nulled: number;
13
+ reservable: number;
18
14
  };
19
15
  cpu: {
20
16
  cores: number;
21
17
  systemLoad: number;
22
18
  lavalinkLoad: number;
23
19
  };
24
- uptime: number;
20
+ frameStats: {
21
+ sent: number;
22
+ nulled: number;
23
+ deficit: number;
24
+ } | null;
25
+ }
26
+ export type NodeLinkV2LoadTypes = "short" | "album" | "artist" | "show" | "episode" | "station" | "podcast";
27
+ export type NodeLinkGetLyrics = NodeLinkGetLyricsSingle | NodeLinkGetLyricsMultiple | NodeLinkGetLyricsEmpty | NodeLinkGetLyricsError;
28
+ export interface NodeLinkGetLyricsMultiple {
29
+ loadType: "lyricsMultiple";
30
+ data: NodeLinkGetLyricsData[];
31
+ }
32
+ export interface NodeLinkGetLyricsEmpty {
33
+ loadType: "empty";
34
+ data: {};
35
+ }
36
+ interface NodeLinkGetLyricsData {
37
+ name: string;
38
+ synced: boolean;
39
+ data: {
40
+ startTime?: number;
41
+ endTime?: number;
42
+ text: string;
43
+ }[];
44
+ rtl: boolean;
45
+ }
46
+ export interface NodeLinkGetLyricsSingle {
47
+ loadType: "lyricsSingle";
48
+ data: NodeLinkGetLyricsData;
49
+ }
50
+ export interface NodeLinkGetLyricsError {
51
+ loadType: "error";
52
+ data: {
53
+ message: string;
54
+ severity: Severity;
55
+ cause: string;
56
+ trace?: string;
57
+ };
25
58
  }
26
59
  /**
27
60
  * This interface represents the LavaLink V4 Error Responses
@@ -55,15 +88,15 @@ export interface ErrorResponses {
55
88
  path: string;
56
89
  }
57
90
  export declare class Node {
91
+ readonly name: string;
58
92
  isConnected: boolean;
59
93
  poru: Poru;
60
- readonly name: string;
61
94
  readonly restURL: string;
62
95
  readonly socketURL: string;
63
96
  password: string;
64
97
  readonly secure: boolean;
65
- readonly regions: Array<string>;
66
- sessionId: string;
98
+ readonly regions: Array<string> | null;
99
+ sessionId: string | null;
67
100
  rest: Rest;
68
101
  ws: WebSocket | null;
69
102
  readonly resumeKey: string | null;
@@ -73,8 +106,10 @@ export declare class Node {
73
106
  reconnectTries: number;
74
107
  reconnectAttempt: NodeJS.Timeout | null;
75
108
  attempt: number;
76
- stats: NodeStats | null;
109
+ stats: NodeStats;
77
110
  options: NodeGroup;
111
+ clientName: string;
112
+ isNodeLink: boolean;
78
113
  /**
79
114
  * The Node class that is used to connect to a lavalink node
80
115
  * @param poru Poru
@@ -86,7 +121,7 @@ export declare class Node {
86
121
  * Connects to the lavalink node
87
122
  * @returns {void}
88
123
  */
89
- connect(): void;
124
+ connect(): Promise<boolean>;
90
125
  /**
91
126
  * Handles the message event
92
127
  * @param payload any
@@ -98,7 +133,7 @@ export declare class Node {
98
133
  * @param payload any
99
134
  * @returns {void}
100
135
  */
101
- reconnect(): void;
136
+ reconnect(): Promise<void>;
102
137
  /**
103
138
  * This function will make the node disconnect
104
139
  * @returns {Promise<void>} void
@@ -109,21 +144,31 @@ export declare class Node {
109
144
  * @returns {number} The amount of penalties
110
145
  */
111
146
  get penalties(): number;
147
+ /**
148
+ * This function will get the RoutePlanner status
149
+ * @returns {Promise<null>}
150
+ */
151
+ getRoutePlannerStatus(): Promise<null | ErrorResponses>;
152
+ /**
153
+ * This function will Unmark a failed address
154
+ * @param {string} address The address to unmark as failed. This address must be in the same ip block.
155
+ * @returns {null | ErrorResponses} This function will most likely error if you havn't enabled the route planner
156
+ */
157
+ unmarkFailedAddress(address: string): Promise<null | ErrorResponses>;
158
+ /**
159
+ * This function will get the upgrade event from the ws connection
160
+ * @param {IncomingMessage} request The request from the upgraded WS connection
161
+ */
162
+ private upgrade;
112
163
  /**
113
164
  * This function will open up again the node
114
165
  * @returns {Promise<void>} The Promise<void>
115
166
  */
116
167
  private open;
117
- /**
118
- * This function will set the stats accordingly from the NodeStats
119
- * @param {NodeStats} packet The NodeStats
120
- * @returns {void} void
121
- */
122
- private setStats;
123
168
  /**
124
169
  * This will send a message to the node
125
- * @param {any} payload any
126
- * @returns {Promise<void>} void
170
+ * @param {string} payload The sent payload we recieved in stringified form
171
+ * @returns {Promise<void>} Return void
127
172
  */
128
173
  private message;
129
174
  /**
@@ -138,15 +183,5 @@ export declare class Node {
138
183
  * @returns {void} void
139
184
  */
140
185
  private error;
141
- /**
142
- * This function will get the RoutePlanner status
143
- * @returns {Promise<null>}
144
- */
145
- getRoutePlannerStatus(): Promise<null>;
146
- /**
147
- * This function will Unmark a failed address
148
- * @param {string} address The address to unmark as failed. This address must be in the same ip block.
149
- * @returns {null | ErrorResponses} This function will most likely error if you havn't enabled the route planner
150
- */
151
- unmarkFailedAddress(address: string): Promise<null | ErrorResponses>;
152
186
  }
187
+ export {};
@@ -8,10 +8,19 @@ const ws_1 = __importDefault(require("ws"));
8
8
  const config_1 = require("../config");
9
9
  const Rest_1 = require("./Rest");
10
10
  ;
11
+ ;
12
+ ;
13
+ ;
14
+ ;
15
+ ;
16
+ ;
17
+ ;
18
+ ;
19
+ ;
11
20
  class Node {
21
+ name;
12
22
  isConnected;
13
23
  poru;
14
- name;
15
24
  restURL;
16
25
  socketURL;
17
26
  password;
@@ -29,6 +38,8 @@ class Node {
29
38
  attempt;
30
39
  stats;
31
40
  options;
41
+ clientName;
42
+ isNodeLink;
32
43
  /**
33
44
  * The Node class that is used to connect to a lavalink node
34
45
  * @param poru Poru
@@ -36,13 +47,13 @@ class Node {
36
47
  * @param options PoruOptions
37
48
  */
38
49
  constructor(poru, node, options) {
39
- this.poru = poru;
40
50
  this.name = node.name;
51
+ this.poru = poru;
41
52
  this.options = node;
53
+ this.secure = node.secure || false;
42
54
  this.restURL = `http${node.secure ? "s" : ""}://${node.host}:${node.port}`;
43
- this.socketURL = `${this.secure ? "wss" : "ws"}://${node.host}:${node.port}/v4/websocket`;
55
+ this.socketURL = `${node.secure ? "wss" : "ws"}://${node.host}:${node.port}/v4/websocket`;
44
56
  this.password = node.password || "youshallnotpass";
45
- this.secure = node.secure || false;
46
57
  this.regions = node.region || null;
47
58
  this.sessionId = null;
48
59
  this.rest = new Rest_1.Rest(poru, this);
@@ -55,37 +66,72 @@ class Node {
55
66
  this.reconnectAttempt = null;
56
67
  this.attempt = 0;
57
68
  this.isConnected = false;
58
- this.stats = null;
69
+ this.clientName = options.clientName || `${config_1.Config.clientName}/${config_1.Config.version}`;
70
+ this.isNodeLink = false;
71
+ this.stats = {
72
+ players: 0,
73
+ playingPlayers: 0,
74
+ uptime: 0,
75
+ memory: {
76
+ free: 0,
77
+ used: 0,
78
+ allocated: 0,
79
+ reservable: 0,
80
+ },
81
+ cpu: {
82
+ cores: 0,
83
+ systemLoad: 0,
84
+ lavalinkLoad: 0,
85
+ },
86
+ frameStats: {
87
+ sent: 0,
88
+ nulled: 0,
89
+ deficit: 0,
90
+ }
91
+ };
59
92
  }
93
+ ;
60
94
  /**
61
95
  * Connects to the lavalink node
62
96
  * @returns {void}
63
97
  */
64
- connect() {
65
- if (this.ws)
66
- this.ws.close();
67
- if (!this.poru.nodes.get(this.name)) {
68
- this.poru.nodes.set(this.name, this);
69
- }
70
- const headers = {
71
- Authorization: this.password,
72
- "User-Id": this.poru.userId,
73
- "Client-Name": config_1.Config.clientName,
74
- };
75
- if (this.resumeKey)
76
- headers["Resume-Key"] = this.resumeKey;
77
- this.ws = new ws_1.default(`${this.socketURL}`, { headers });
78
- this.ws.on("open", this.open.bind(this));
79
- this.ws.on("error", this.error.bind(this));
80
- this.ws.on("message", this.message.bind(this));
81
- this.ws.on("close", this.close.bind(this));
98
+ async connect() {
99
+ return new Promise((resolve) => {
100
+ if (this.isConnected)
101
+ return resolve(true);
102
+ if (this.ws)
103
+ this.ws.close();
104
+ if (!this.poru.nodes.get(this.options.name)) {
105
+ this.poru.nodes.set(this.options.name, this);
106
+ }
107
+ ;
108
+ if (!this.poru.userId)
109
+ throw new Error("[Poru Error] No user id found in the Poru instance. Consider using a supported library.");
110
+ const headers = {
111
+ Authorization: this.password,
112
+ "User-Id": this.poru.userId,
113
+ "Client-Name": this.clientName,
114
+ };
115
+ if (this.resumeKey)
116
+ headers["Resume-Key"] = this.resumeKey;
117
+ this.ws = new ws_1.default(`${this.socketURL}`, { headers });
118
+ this.ws.on("open", this.open.bind(this));
119
+ this.ws.on("error", this.error.bind(this));
120
+ this.ws.on("message", this.message.bind(this));
121
+ this.ws.on("close", this.close.bind(this));
122
+ this.ws.on("upgrade", (request) => this.upgrade(request));
123
+ resolve(true);
124
+ });
82
125
  }
126
+ ;
83
127
  /**
84
128
  * Handles the message event
85
129
  * @param payload any
86
130
  * @returns {void}
87
131
  */
88
132
  send(payload) {
133
+ if (!this.isConnected || !this.ws)
134
+ throw new Error("[Poru Error] The node is not connected");
89
135
  const data = JSON.stringify(payload);
90
136
  this.ws.send(data, (error) => {
91
137
  if (error)
@@ -98,19 +144,22 @@ class Node {
98
144
  * @param payload any
99
145
  * @returns {void}
100
146
  */
101
- reconnect() {
102
- this.reconnectAttempt = setTimeout(() => {
147
+ async reconnect() {
148
+ this.reconnectAttempt = setTimeout(async () => {
103
149
  if (this.attempt > this.reconnectTries) {
104
- throw new Error(`[Poru Websocket] Unable to connect with ${this.name} node after ${this.reconnectTries} tries`);
150
+ throw new Error(`[Poru Websocket] Unable to connect with ${this.options.name} node after ${this.reconnectTries} tries`);
105
151
  }
152
+ // Delete the ws instance
106
153
  this.isConnected = false;
107
154
  this.ws?.removeAllListeners();
108
155
  this.ws = null;
156
+ // Try to reconnect
109
157
  this.poru.emit("nodeReconnect", this);
110
- this.connect();
158
+ await this.connect();
111
159
  this.attempt++;
112
160
  }, this.reconnectTimeout);
113
161
  }
162
+ ;
114
163
  /**
115
164
  * This function will make the node disconnect
116
165
  * @returns {Promise<void>} void
@@ -124,13 +173,13 @@ class Node {
124
173
  }
125
174
  ;
126
175
  });
127
- this.ws.close(1000, "destroy");
176
+ this.ws?.close(1000, "destroy");
128
177
  this.ws?.removeAllListeners();
129
178
  this.ws = null;
130
- // this.reconnect = 1;
131
- this.poru.nodes.delete(this.name);
179
+ this.poru.nodes.delete(this.options.name);
132
180
  this.poru.emit("nodeDisconnect", this);
133
181
  }
182
+ ;
134
183
  /**
135
184
  * This function will get the penalties from the current node
136
185
  * @returns {number} The amount of penalties
@@ -139,82 +188,157 @@ class Node {
139
188
  let penalties = 0;
140
189
  if (!this.isConnected)
141
190
  return penalties;
142
- penalties += this.stats.players;
191
+ penalties += this.stats?.players ?? 0;
143
192
  penalties += Math.round(Math.pow(1.05, 100 * this.stats.cpu.systemLoad) * 10 - 10);
144
193
  if (this.stats.frameStats) {
145
194
  penalties += this.stats.frameStats.deficit;
146
195
  penalties += this.stats.frameStats.nulled * 2;
147
196
  }
197
+ ;
148
198
  return penalties;
149
199
  }
200
+ ;
201
+ /**
202
+ * This function will get the RoutePlanner status
203
+ * @returns {Promise<null>}
204
+ */
205
+ async getRoutePlannerStatus() {
206
+ if (this.isNodeLink)
207
+ return {
208
+ timestamp: Date.now(),
209
+ status: 404,
210
+ error: "Not found.",
211
+ message: "The specified node is a NodeLink. NodeLink's do not have the routeplanner feature.",
212
+ path: "/v4/routeplanner/status",
213
+ trace: new Error().stack
214
+ };
215
+ return await this.rest.get(`/v4/routeplanner/status`);
216
+ }
217
+ ;
218
+ /**
219
+ * This function will Unmark a failed address
220
+ * @param {string} address The address to unmark as failed. This address must be in the same ip block.
221
+ * @returns {null | ErrorResponses} This function will most likely error if you havn't enabled the route planner
222
+ */
223
+ async unmarkFailedAddress(address) {
224
+ if (this.isNodeLink)
225
+ return {
226
+ timestamp: Date.now(),
227
+ status: 404,
228
+ error: "Not found.",
229
+ message: "The specified node is a NodeLink. NodeLink's do not have the routeplanner feature.",
230
+ path: "/v4/routeplanner/free/address",
231
+ trace: new Error().stack
232
+ };
233
+ return this.rest.post(`/v4/routeplanner/free/address`, { address });
234
+ }
235
+ ;
236
+ /**
237
+ * This function will get the upgrade event from the ws connection
238
+ * @param {IncomingMessage} request The request from the upgraded WS connection
239
+ */
240
+ upgrade(request) {
241
+ // Checking if this node is a NodeLink or not
242
+ this.isNodeLink = this.options.isNodeLink ?? Boolean(request.headers.isnodelink) ?? false;
243
+ }
244
+ ;
150
245
  /**
151
246
  * This function will open up again the node
152
247
  * @returns {Promise<void>} The Promise<void>
153
248
  */
154
249
  async open() {
155
- if (this.reconnectAttempt) {
156
- clearTimeout(this.reconnectAttempt);
157
- delete this.reconnectAttempt;
158
- }
159
- this.poru.emit("nodeConnect", this);
160
- this.isConnected = true;
161
- this.poru.emit("debug", this.name, `[Web Socket] Connection ready ${this.socketURL}`);
162
- if (this.autoResume) {
163
- for (const player of this.poru.players.values()) {
164
- if (player.node === this) {
165
- await player.restart();
166
- }
250
+ try {
251
+ if (this.reconnectAttempt) {
252
+ clearTimeout(this.reconnectAttempt);
253
+ this.reconnectAttempt = null;
167
254
  }
255
+ ;
256
+ this.poru.emit("nodeConnect", this);
257
+ this.isConnected = true;
258
+ this.poru.emit("debug", this.options.name, `[Web Socket] Connection ready ${this.socketURL}`);
259
+ if (this.autoResume)
260
+ this.poru.players.forEach(async (player) => player.node === this ? await player.restart() : null);
168
261
  }
262
+ catch (error) {
263
+ this.poru.emit("debug", `[Web Socket] Error while opening the connection with the node ${this.options.name}.`, error);
264
+ }
265
+ ;
169
266
  }
170
- /**
171
- * This function will set the stats accordingly from the NodeStats
172
- * @param {NodeStats} packet The NodeStats
173
- * @returns {void} void
174
- */
175
- setStats(packet) {
176
- this.stats = packet;
177
- }
267
+ ;
178
268
  /**
179
269
  * This will send a message to the node
180
- * @param {any} payload any
181
- * @returns {Promise<void>} void
270
+ * @param {string} payload The sent payload we recieved in stringified form
271
+ * @returns {Promise<void>} Return void
182
272
  */
183
273
  async message(payload) {
184
- const packet = JSON.parse(payload);
185
- if (!packet?.op)
186
- return;
187
- this.poru.emit("raw", "Node", packet);
188
- this.poru.emit("debug", this.name, `[Web Socket] Lavalink Node Update : ${JSON.stringify(packet)} `);
189
- if (packet.op === "stats") {
190
- delete packet.op;
191
- this.setStats(packet);
192
- }
193
- if (packet.op === "ready") {
194
- this.rest.setSessionId(packet.sessionId);
195
- this.sessionId = packet.sessionId;
196
- this.poru.emit("debug", this.name, `[Web Socket] Ready Payload received ${JSON.stringify(packet)}`);
197
- if (this.resumeKey) {
198
- await this.rest.patch(`/v4/sessions/${this.sessionId}`, { resumingKey: this.resumeKey, timeout: this.resumeTimeout });
199
- this.poru.emit("debug", this.name, `[Lavalink Rest] Resuming configured on Lavalink`);
274
+ try {
275
+ const packet = JSON.parse(payload);
276
+ if (!packet?.op)
277
+ return;
278
+ this.poru.emit("raw", "Node", packet);
279
+ this.poru.emit("debug", this.options.name, `[Web Socket] Lavalink Node Update : ${JSON.stringify(packet)} `);
280
+ switch (packet.op) {
281
+ case "ready":
282
+ {
283
+ this.rest.setSessionId(packet.sessionId);
284
+ this.sessionId = packet.sessionId;
285
+ this.poru.emit("debug", this.options.name, `[Web Socket] Ready Payload received ${JSON.stringify(packet)}`);
286
+ // If a resume key was set use it
287
+ if (this.resumeKey) {
288
+ await this.rest.patch(`/v4/sessions/${this.sessionId}`, { resumingKey: this.resumeKey, timeout: this.resumeTimeout });
289
+ this.poru.emit("debug", this.options.name, `[Lavalink Rest] Resuming configured on Lavalink`);
290
+ }
291
+ ;
292
+ break;
293
+ }
294
+ ;
295
+ // If the packet has stats about the node in it update them on the Node's class
296
+ case "stats":
297
+ {
298
+ delete packet.op;
299
+ this.stats = packet;
300
+ break;
301
+ }
302
+ ;
303
+ // If the packet is an event or playerUpdate emit the event to the player
304
+ case "event":
305
+ case "playerUpdate":
306
+ {
307
+ const player = this.poru.players.get(packet.guildId);
308
+ if (packet.guildId && player)
309
+ player.emit(packet.op, packet);
310
+ break;
311
+ }
312
+ ;
313
+ default: break;
200
314
  }
315
+ ;
201
316
  }
202
- const player = this.poru.players.get(packet.guildId);
203
- if (packet.guildId && player)
204
- player.emit(packet.op, packet);
317
+ catch (err) {
318
+ this.poru.emit("debug", "[Web Socket] Error while parsing the payload.", err);
319
+ }
320
+ ;
205
321
  }
322
+ ;
206
323
  /**
207
324
  * This will close the connection to the node
208
325
  * @param {any} event any
209
326
  * @returns {void} void
210
327
  */
211
- close(event) {
212
- this.disconnect();
213
- this.poru.emit("nodeDisconnect", this, event);
214
- this.poru.emit("debug", this.name, `[Web Socket] Connection closed with Error code : ${event || "Unknown code"}`);
215
- if (event !== 1000)
216
- this.reconnect();
328
+ async close(event) {
329
+ try {
330
+ await this.disconnect();
331
+ this.poru.emit("nodeDisconnect", this, event);
332
+ this.poru.emit("debug", this.options.name, `[Web Socket] Connection closed with Error code: ${event || "Unknown code"}`);
333
+ if (event !== 1000)
334
+ await this.reconnect();
335
+ }
336
+ catch (error) {
337
+ this.poru.emit("debug", "[Web Socket] Error while closing the connection with the node.", error);
338
+ }
339
+ ;
217
340
  }
341
+ ;
218
342
  /**
219
343
  * This function will emit the error so that the user's listeners can get them and listen to them
220
344
  * @param {any} event any
@@ -224,23 +348,10 @@ class Node {
224
348
  if (!event)
225
349
  return;
226
350
  this.poru.emit("nodeError", this, event);
227
- this.poru.emit("debug", `[Web Socket] Connection for Lavalink Node (${this.name}) has error code: ${event.code || event}`);
228
- }
229
- /**
230
- * This function will get the RoutePlanner status
231
- * @returns {Promise<null>}
232
- */
233
- async getRoutePlannerStatus() {
234
- return await this.rest.get(`/v4/routeplanner/status`);
235
- }
236
- /**
237
- * This function will Unmark a failed address
238
- * @param {string} address The address to unmark as failed. This address must be in the same ip block.
239
- * @returns {null | ErrorResponses} This function will most likely error if you havn't enabled the route planner
240
- */
241
- async unmarkFailedAddress(address) {
242
- return this.rest.post(`/v4/routeplanner/free/address`, { address });
351
+ this.poru.emit("debug", `[Web Socket] Connection for Lavalink Node (${this.options.name}) has error code: ${event.code || event}`);
243
352
  }
353
+ ;
244
354
  }
245
355
  exports.Node = Node;
356
+ ;
246
357
  //# sourceMappingURL=Node.js.map