magmastream 2.9.3-dev.5 → 2.9.3-dev.6

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
@@ -3466,11 +3466,22 @@ declare abstract class TrackUtils {
3466
3466
  static setTrackPartial(partial: TrackPartial[]): void;
3467
3467
  /**
3468
3468
  * Checks if the provided argument is a valid Track.
3469
- * If provided an array then every element will be checked.
3470
- * @param trackOrTracks The Track or array of Tracks to check.
3469
+ * @param value The value to check.
3471
3470
  * @returns {boolean} Whether the provided argument is a valid Track.
3472
3471
  */
3473
- static validate(trackOrTracks: unknown): boolean;
3472
+ static isTrack(track: unknown): track is Track;
3473
+ /**
3474
+ * Checks if the provided argument is a valid Track array.
3475
+ * @param value The value to check.
3476
+ * @returns {boolean} Whether the provided argument is a valid Track array.
3477
+ */
3478
+ static isTrackArray(value: unknown): value is Track[];
3479
+ /**
3480
+ * Checks if the provided argument is a valid Track or Track array.
3481
+ * @param value The value to check.
3482
+ * @returns {boolean} Whether the provided argument is a valid Track or Track array.
3483
+ */
3484
+ static validate(value: unknown): value is Track | Track[];
3474
3485
  /**
3475
3486
  * Builds a Track from the raw data from Lavalink and a optional requester.
3476
3487
  * @param data The raw data from Lavalink to build the Track from.
@@ -806,11 +806,8 @@ class Player {
806
806
  if (currentPlayingTrack) {
807
807
  await this.queue.add(currentPlayingTrack, 0);
808
808
  }
809
- await this.play(lastTrack);
810
- }
811
- else {
812
- await this.play(lastTrack);
813
809
  }
810
+ await this.play(lastTrack);
814
811
  this.manager.emit(Enums_1.ManagerEventTypes.PlayerStateUpdate, oldPlayer, this, {
815
812
  changeType: Enums_1.PlayerStateEventTypes.TrackChange,
816
813
  details: {
@@ -104,35 +104,55 @@ class Rest {
104
104
  },
105
105
  data: body,
106
106
  timeout: this.node.options.apiRequestTimeoutMs,
107
+ validateStatus: () => true,
107
108
  };
109
+ let response;
108
110
  try {
109
- const response = await (0, axios_1.default)(config);
110
- return response.data;
111
+ response = await (0, axios_1.default)(config);
111
112
  }
112
113
  catch (err) {
113
- const error = err;
114
- if (!error.response) {
115
- throw new MagmastreamError_1.MagmaStreamError({
116
- code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
117
- message: `No response from node ${this.node.options.identifier}: ${error.message}`,
118
- });
119
- }
120
- const data = error.response.data;
121
- if (data?.message === "Guild not found") {
122
- return [];
123
- }
124
- if (error.response.status === 401) {
125
- throw new MagmastreamError_1.MagmaStreamError({
126
- code: Enums_1.MagmaStreamErrorCode.REST_UNAUTHORIZED,
127
- message: `Unauthorized access to node ${this.node.options.identifier}`,
128
- });
129
- }
130
- const dataMessage = typeof data === "string" ? data : data?.message ? data.message : Utils_1.JSONUtils.safe(data, 2);
114
+ const message = err instanceof Error ? err.message : "Unknown error";
131
115
  throw new MagmastreamError_1.MagmaStreamError({
132
116
  code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
133
- message: `Request to node ${this.node.options.identifier} failed with status ${error.response.status}: ${dataMessage}`,
117
+ message: `No response from node ${this.node.options.identifier}: ${message}`,
134
118
  });
135
119
  }
120
+ const { status, data } = response;
121
+ if (status >= 200 && status < 300) {
122
+ return data;
123
+ }
124
+ // Lavalink sometimes returns "Guild not found" for inactive players
125
+ if (status === 404 && data?.message === "Guild not found") {
126
+ return [];
127
+ }
128
+ if (status === 401) {
129
+ throw new MagmastreamError_1.MagmaStreamError({
130
+ code: Enums_1.MagmaStreamErrorCode.REST_UNAUTHORIZED,
131
+ message: `Unauthorized access to node ${this.node.options.identifier}`,
132
+ });
133
+ }
134
+ if (status >= 400 && status < 500) {
135
+ const message = typeof data === "string"
136
+ ? data
137
+ : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
138
+ ? data.message
139
+ : "Unknown client error";
140
+ return {
141
+ status,
142
+ error: true,
143
+ message,
144
+ data,
145
+ };
146
+ }
147
+ const safeMessage = typeof data === "string"
148
+ ? data
149
+ : typeof data === "object" && data !== null && "message" in data && typeof data.message === "string"
150
+ ? data.message
151
+ : Utils_1.JSONUtils.safe(data, 2);
152
+ throw new MagmastreamError_1.MagmaStreamError({
153
+ code: Enums_1.MagmaStreamErrorCode.REST_REQUEST_FAILED,
154
+ message: `Request to node ${this.node.options.identifier} failed (${status}): ${safeMessage}`,
155
+ });
136
156
  }
137
157
  /**
138
158
  * Sends a GET request to the specified endpoint and returns the response data.
@@ -12,6 +12,7 @@ const MagmastreamError_1 = require("./MagmastreamError");
12
12
  // import playwright from "playwright";
13
13
  /** @hidden */
14
14
  const SIZES = ["0", "1", "2", "3", "default", "mqdefault", "hqdefault", "maxresdefault"];
15
+ const REQUIRED_TRACK_KEYS = ["track", "title", "uri"];
15
16
  class TrackUtils {
16
17
  static trackPartial = null;
17
18
  static manager;
@@ -54,25 +55,30 @@ class TrackUtils {
54
55
  }
55
56
  /**
56
57
  * Checks if the provided argument is a valid Track.
57
- * If provided an array then every element will be checked.
58
- * @param trackOrTracks The Track or array of Tracks to check.
58
+ * @param value The value to check.
59
59
  * @returns {boolean} Whether the provided argument is a valid Track.
60
60
  */
61
- static validate(trackOrTracks) {
62
- if (typeof trackOrTracks !== "object" || trackOrTracks === null) {
61
+ static isTrack(track) {
62
+ if (typeof track !== "object" || track === null)
63
63
  return false;
64
- }
65
- const isValidTrack = (track) => {
66
- if (typeof track !== "object" || track === null) {
67
- return false;
68
- }
69
- const t = track;
70
- return (typeof t.track === "string" && typeof t.title === "string" && typeof t.identifier === "string" && typeof t.isrc === "string" && typeof t.uri === "string");
71
- };
72
- if (Array.isArray(trackOrTracks)) {
73
- return trackOrTracks.every(isValidTrack);
74
- }
75
- return isValidTrack(trackOrTracks);
64
+ const t = track;
65
+ return REQUIRED_TRACK_KEYS.every((key) => typeof t[key] === "string");
66
+ }
67
+ /**
68
+ * Checks if the provided argument is a valid Track array.
69
+ * @param value The value to check.
70
+ * @returns {boolean} Whether the provided argument is a valid Track array.
71
+ */
72
+ static isTrackArray(value) {
73
+ return Array.isArray(value) && value.every(this.isTrack);
74
+ }
75
+ /**
76
+ * Checks if the provided argument is a valid Track or Track array.
77
+ * @param value The value to check.
78
+ * @returns {boolean} Whether the provided argument is a valid Track or Track array.
79
+ */
80
+ static validate(value) {
81
+ return this.isTrack(value) || this.isTrackArray(value);
76
82
  }
77
83
  /**
78
84
  * Builds a Track from the raw data from Lavalink and a optional requester.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magmastream",
3
- "version": "2.9.3-dev.5",
3
+ "version": "2.9.3-dev.6",
4
4
  "description": "A user-friendly Lavalink client designed for NodeJS.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",