magmastream 2.0.3 → 2.0.5

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
@@ -1,5 +1,4 @@
1
1
  /// <reference types="node" />
2
- import { Pool } from 'undici';
3
2
  import WebSocket from 'ws';
4
3
  import { Collection } from '@discordjs/collection';
5
4
  import { EventEmitter } from 'events';
@@ -380,10 +379,10 @@ declare class Rest {
380
379
  /** Sends a DELETE request to the server to destroy the player. */
381
380
  destroyPlayer(guildId: string): Promise<unknown>;
382
381
  private request;
383
- get(endpoint: RouteLike): Promise<unknown>;
384
- patch(endpoint: RouteLike, body: unknown): Promise<unknown>;
385
- post(endpoint: RouteLike, body: unknown): Promise<unknown>;
386
- delete(endpoint: RouteLike): Promise<unknown>;
382
+ get(endpoint: string): Promise<unknown>;
383
+ patch(endpoint: string, body: unknown): Promise<unknown>;
384
+ post(endpoint: string, body: unknown): Promise<unknown>;
385
+ delete(endpoint: string): Promise<unknown>;
387
386
  }
388
387
  interface playOptions {
389
388
  guildId: string;
@@ -405,19 +404,20 @@ interface playOptions {
405
404
  /** The audio effects. */
406
405
  filters?: object;
407
406
  /** voice payload. */
408
- voice?: unknown;
407
+ voice?: {
408
+ token: string;
409
+ sessionId: string;
410
+ endpoint: string;
411
+ };
409
412
  /** Whether to not replace the track if a play payload is sent. */
410
413
  noReplace?: boolean;
411
414
  };
412
415
  }
413
- type RouteLike = `/${string}`;
414
416
 
415
417
  declare class Node {
416
418
  options: NodeOptions;
417
419
  /** The socket for the node. */
418
420
  socket: WebSocket | null;
419
- /** The HTTP pool used for rest calls. */
420
- http: Pool;
421
421
  /** The amount of rest calls the node has made. */
422
422
  calls: number;
423
423
  /** The stats for the node. */
@@ -479,8 +479,6 @@ interface NodeOptions {
479
479
  resumeTimeout?: number;
480
480
  /** The timeout used for api calls */
481
481
  requestTimeout?: number;
482
- /** Options for the undici http pool used for http requests */
483
- poolOptions?: Pool.Options;
484
482
  }
485
483
  interface NodeStats {
486
484
  /** The amount of players on the node. */
@@ -593,7 +591,7 @@ type TrackEndReason = "finished" | "loadFailed" | "stopped" | "replaced" | "clea
593
591
  type Severity = "common" | "suspicious" | "fault";
594
592
  interface TrackData {
595
593
  /** The track information. */
596
- encoded?: string;
594
+ encoded: string;
597
595
  /** The detailed information of the track. */
598
596
  info: TrackDataInfo;
599
597
  /** Addition track info provided by plugins. */
@@ -853,7 +851,7 @@ declare class Manager extends EventEmitter {
853
851
  * Sends voice data to the Lavalink server.
854
852
  * @param data
855
853
  */
856
- updateVoiceState(data: VoicePacket | VoiceServer | VoiceState): void;
854
+ updateVoiceState(data: VoicePacket | VoiceServer | VoiceState): Promise<void>;
857
855
  }
858
856
  interface Payload {
859
857
  /** The OP code */
@@ -117,22 +117,21 @@ class Manager extends events_1.EventEmitter {
117
117
  * @param requester
118
118
  * @returns The search result.
119
119
  */
120
- search(query, requester) {
121
- return new Promise(async (resolve, reject) => {
122
- const node = this.leastUsedNodes.first();
123
- if (!node)
124
- throw new Error("No available nodes.");
125
- const _query = typeof query === "string" ? { query } : query;
126
- const _source = Manager.DEFAULT_SOURCES[_query.source ?? this.options.defaultSearchPlatform] ?? _query.source;
127
- let search = _query.query;
128
- if (!/^https?:\/\//.test(search)) {
129
- search = `${_source}:${search}`;
130
- }
131
- const res = (await node.rest
132
- .get(`/v4/loadtracks?identifier=${encodeURIComponent(search)}`)
133
- .catch((err) => reject(err)));
120
+ async search(query, requester) {
121
+ const node = this.leastUsedNodes.first();
122
+ if (!node) {
123
+ throw new Error("No available nodes.");
124
+ }
125
+ const _query = typeof query === "string" ? { query } : query;
126
+ const _source = Manager.DEFAULT_SOURCES[_query.source ?? this.options.defaultSearchPlatform] ?? _query.source;
127
+ let search = _query.query;
128
+ if (!/^https?:\/\//.test(search)) {
129
+ search = `${_source}:${search}`;
130
+ }
131
+ try {
132
+ const res = (await node.rest.get(`/v4/loadtracks?identifier=${encodeURIComponent(search)}`));
134
133
  if (!res) {
135
- return reject(new Error("Query not found."));
134
+ throw new Error("Query not found.");
136
135
  }
137
136
  let searchData = [];
138
137
  let playlistData;
@@ -145,20 +144,26 @@ class Manager extends events_1.EventEmitter {
145
144
  break;
146
145
  case "playlist":
147
146
  playlistData = res.data;
147
+ break;
148
148
  }
149
+ const tracks = searchData.map((track) => Utils_1.TrackUtils.build(track, requester));
150
+ const playlist = res.loadType === "playlist"
151
+ ? {
152
+ name: playlistData.info.name,
153
+ tracks: playlistData.tracks.map((track) => Utils_1.TrackUtils.build(track, requester)),
154
+ duration: playlistData.tracks.reduce((acc, cur) => acc + (cur.info.length || 0), 0),
155
+ }
156
+ : null;
149
157
  const result = {
150
158
  loadType: res.loadType,
151
- tracks: searchData.map((track) => Utils_1.TrackUtils.build(track, requester)) ?? [],
152
- playlist: res.loadType === "playlist"
153
- ? {
154
- name: playlistData.info.name,
155
- tracks: playlistData.tracks.map((track) => Utils_1.TrackUtils.build(track, requester)) ?? [],
156
- duration: playlistData.tracks.reduce((acc, cur) => acc + (cur.info.length || 0), 0),
157
- }
158
- : null,
159
+ tracks,
160
+ playlist,
159
161
  };
160
- return resolve(result);
161
- });
162
+ return result;
163
+ }
164
+ catch (err) {
165
+ throw new Error(err);
166
+ }
162
167
  }
163
168
  /**
164
169
  * Decodes the base64 encoded tracks and returns a TrackData array.
@@ -235,16 +240,19 @@ class Manager extends events_1.EventEmitter {
235
240
  * Sends voice data to the Lavalink server.
236
241
  * @param data
237
242
  */
238
- updateVoiceState(data) {
243
+ async updateVoiceState(data) {
239
244
  if ("t" in data &&
240
- !["VOICE_STATE_UPDATE", "VOICE_SERVER_UPDATE"].includes(data.t))
245
+ !["VOICE_STATE_UPDATE", "VOICE_SERVER_UPDATE"].includes(data.t)) {
241
246
  return;
247
+ }
242
248
  const update = "d" in data ? data.d : data;
243
- if (!update || (!("token" in update) && !("session_id" in update)))
249
+ if (!update || (!("token" in update) && !("session_id" in update))) {
244
250
  return;
251
+ }
245
252
  const player = this.players.get(update.guild_id);
246
- if (!player)
253
+ if (!player) {
247
254
  return;
255
+ }
248
256
  if ("token" in update) {
249
257
  /* voice server update */
250
258
  player.voiceState.event = update;
@@ -272,7 +280,7 @@ class Manager extends events_1.EventEmitter {
272
280
  }
273
281
  if (REQUIRED_KEYS.every((key) => key in player.voiceState)) {
274
282
  const { sessionId, event: { token, endpoint }, } = player.voiceState;
275
- player.node.rest.updatePlayer({
283
+ await player.node.rest.updatePlayer({
276
284
  guildId: player.guild,
277
285
  data: { voice: { token, endpoint, sessionId } },
278
286
  });
@@ -4,7 +4,6 @@ exports.Node = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /* eslint-disable no-case-declarations */
6
6
  const Utils_1 = require("./Utils");
7
- const undici_1 = require("undici");
8
7
  const Rest_1 = require("./Rest");
9
8
  const nodeCheck_1 = tslib_1.__importDefault(require("../utils/nodeCheck"));
10
9
  const ws_1 = tslib_1.__importDefault(require("ws"));
@@ -12,8 +11,6 @@ class Node {
12
11
  options;
13
12
  /** The socket for the node. */
14
13
  socket = null;
15
- /** The HTTP pool used for rest calls. */
16
- http;
17
14
  /** The amount of rest calls the node has made. */
18
15
  calls = 0;
19
16
  /** The stats for the node. */
@@ -65,7 +62,6 @@ class Node {
65
62
  if (this.options.secure) {
66
63
  this.options.port = 443;
67
64
  }
68
- this.http = new undici_1.Pool(`http${this.options.secure ? "s" : ""}://${this.address}`, this.options.poolOptions);
69
65
  this.options.identifier = options.identifier || options.host;
70
66
  this.stats = {
71
67
  players: 0,
@@ -25,16 +25,16 @@ class Rest {
25
25
  return this.sessionId;
26
26
  }
27
27
  /** Retrieves all the players that are currently running on the node. */
28
- getAllPlayers() {
29
- return this.get(`/v4/sessions/${this.sessionId}/players`);
28
+ async getAllPlayers() {
29
+ return await this.get(`/v4/sessions/${this.sessionId}/players`);
30
30
  }
31
31
  /** Sends a PATCH request to update player related data. */
32
- updatePlayer(options) {
33
- return this.patch(`/v4/sessions/${this.sessionId}/players/${options.guildId}?noReplace=false`, options.data);
32
+ async updatePlayer(options) {
33
+ return await this.patch(`/v4/sessions/${this.sessionId}/players/${options.guildId}?noReplace=false`, options.data);
34
34
  }
35
35
  /** Sends a DELETE request to the server to destroy the player. */
36
- destroyPlayer(guildId) {
37
- return this.delete(`/v4/sessions/${this.sessionId}/players/${guildId}`);
36
+ async destroyPlayer(guildId) {
37
+ return await this.delete(`/v4/sessions/${this.sessionId}/players/${guildId}`);
38
38
  }
39
39
  /* Sends a GET request to the specified endpoint and returns the response data. */
40
40
  async request(method, endpoint, body) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magmastream",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
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,10 +16,10 @@
16
16
  "devDependencies": {
17
17
  "@favware/rollup-type-bundler": "^2.0.0",
18
18
  "@types/lodash": "^4.14.195",
19
- "@types/node": "^20.4.1",
19
+ "@types/node": "^20.4.2",
20
20
  "@types/ws": "^8.5.3",
21
- "@typescript-eslint/eslint-plugin": "^5.37.0",
22
- "@typescript-eslint/parser": "^5.37.0",
21
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
22
+ "@typescript-eslint/parser": "^6.0.0",
23
23
  "eslint": "^8.23.1",
24
24
  "npm-run-all": "^1.7.0",
25
25
  "typedoc": "^0.24.8",
@@ -32,7 +32,6 @@
32
32
  "events": "^3.3.0",
33
33
  "lodash": "^4.17.21",
34
34
  "tslib": "^2.4.0",
35
- "undici": "^5.10.0",
36
35
  "ws": "^8.8.1"
37
36
  },
38
37
  "engines": {