magmastream 2.9.3-dev.3 → 2.9.3-dev.30
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/config/blockedWords.d.ts +1 -0
- package/dist/index.d.ts +19 -3687
- package/dist/index.js +1 -1
- package/dist/statestorage/JsonQueue.d.ts +173 -0
- package/dist/statestorage/JsonQueue.js +32 -4
- package/dist/statestorage/MemoryQueue.d.ts +154 -0
- package/dist/statestorage/MemoryQueue.js +56 -36
- package/dist/statestorage/RedisQueue.d.ts +178 -0
- package/dist/statestorage/RedisQueue.js +29 -7
- package/dist/structures/Enums.d.ts +310 -0
- package/dist/structures/Enums.js +6 -0
- package/dist/structures/Filters.d.ts +352 -0
- package/dist/structures/Filters.js +5 -4
- package/dist/structures/MagmastreamError.d.ts +14 -0
- package/dist/structures/Manager.d.ts +259 -0
- package/dist/structures/Manager.js +297 -555
- package/dist/structures/Node.d.ts +390 -0
- package/dist/structures/Node.js +98 -143
- package/dist/structures/Player.d.ts +347 -0
- package/dist/structures/Player.js +53 -127
- package/dist/structures/Plugin.d.ts +23 -0
- package/dist/structures/Rest.d.ts +93 -0
- package/dist/structures/Rest.js +41 -21
- package/dist/structures/Types.d.ts +1315 -0
- package/dist/structures/Utils.d.ts +169 -0
- package/dist/structures/Utils.js +145 -71
- package/dist/utils/filtersEqualizers.d.ts +16 -0
- package/dist/utils/managerCheck.d.ts +7 -0
- package/dist/utils/nodeCheck.d.ts +7 -0
- package/dist/utils/playerCheck.d.ts +7 -0
- package/dist/wrappers/discord.js.d.ts +15 -0
- package/dist/wrappers/discord.js.js +19 -4
- package/dist/wrappers/discordeno.d.ts +19 -0
- package/dist/wrappers/discordeno.js +77 -0
- package/dist/wrappers/eris.d.ts +15 -0
- package/dist/wrappers/eris.js +20 -3
- package/dist/wrappers/oceanic.d.ts +15 -0
- package/dist/wrappers/oceanic.js +22 -4
- package/dist/wrappers/seyfert.d.ts +37 -0
- package/dist/wrappers/seyfert.js +25 -1
- package/package.json +106 -98
- package/dist/wrappers/detritus.js +0 -52
|
@@ -82,7 +82,7 @@ class Player {
|
|
|
82
82
|
message: "Manager instance is required.",
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
-
this.clusterId = this.manager.options.clusterId
|
|
85
|
+
this.clusterId = this.manager.options.clusterId ?? 0;
|
|
86
86
|
// Check the player options for errors.
|
|
87
87
|
(0, playerCheck_1.default)(options);
|
|
88
88
|
this.options = {
|
|
@@ -260,21 +260,30 @@ class Player {
|
|
|
260
260
|
*/
|
|
261
261
|
async destroy(disconnect = true) {
|
|
262
262
|
this.state = Enums_1.StateTypes.Destroying;
|
|
263
|
+
if (this.dynamicLoopInterval) {
|
|
264
|
+
clearInterval(this.dynamicLoopInterval);
|
|
265
|
+
this.dynamicLoopInterval = null;
|
|
266
|
+
}
|
|
267
|
+
if (this.voiceReceiverReconnectTimeout) {
|
|
268
|
+
clearTimeout(this.voiceReceiverReconnectTimeout);
|
|
269
|
+
this.voiceReceiverReconnectTimeout = null;
|
|
270
|
+
}
|
|
271
|
+
if (this.voiceReceiverWsClient) {
|
|
272
|
+
this.voiceReceiverWsClient.removeAllListeners();
|
|
273
|
+
this.voiceReceiverWsClient.close();
|
|
274
|
+
this.voiceReceiverWsClient = null;
|
|
275
|
+
}
|
|
263
276
|
if (disconnect) {
|
|
264
|
-
await this.disconnect().catch((
|
|
265
|
-
console.warn(`[Player#destroy] Failed to disconnect player ${this.guildId}:`, err);
|
|
266
|
-
});
|
|
277
|
+
await this.disconnect().catch(() => { });
|
|
267
278
|
}
|
|
268
|
-
await this.node.rest.destroyPlayer(this.guildId).catch((
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
await this.queue.clear();
|
|
272
|
-
await this.queue.clearPrevious();
|
|
273
|
-
await this.queue.setCurrent(null);
|
|
279
|
+
await this.node.rest.destroyPlayer(this.guildId).catch(() => { });
|
|
280
|
+
await this.queue.destroy();
|
|
281
|
+
this.nowPlayingMessage = undefined;
|
|
274
282
|
this.manager.emit(Enums_1.ManagerEventTypes.PlayerDestroy, this);
|
|
275
283
|
const deleted = this.manager.players.delete(this.guildId);
|
|
276
|
-
if (this.manager.options.stateStorage.
|
|
284
|
+
if (this.manager.options.stateStorage.deleteDestroyedPlayers) {
|
|
277
285
|
await this.manager.cleanupInactivePlayer(this.guildId);
|
|
286
|
+
}
|
|
278
287
|
return deleted;
|
|
279
288
|
}
|
|
280
289
|
/**
|
|
@@ -387,7 +396,7 @@ class Player {
|
|
|
387
396
|
},
|
|
388
397
|
});
|
|
389
398
|
this.playing = true;
|
|
390
|
-
this.position = 0;
|
|
399
|
+
this.position = finalOptions.startTime || 0;
|
|
391
400
|
return this;
|
|
392
401
|
}
|
|
393
402
|
/**
|
|
@@ -806,11 +815,8 @@ class Player {
|
|
|
806
815
|
if (currentPlayingTrack) {
|
|
807
816
|
await this.queue.add(currentPlayingTrack, 0);
|
|
808
817
|
}
|
|
809
|
-
await this.play(lastTrack);
|
|
810
|
-
}
|
|
811
|
-
else {
|
|
812
|
-
await this.play(lastTrack);
|
|
813
818
|
}
|
|
819
|
+
await this.play(lastTrack);
|
|
814
820
|
this.manager.emit(Enums_1.ManagerEventTypes.PlayerStateUpdate, oldPlayer, this, {
|
|
815
821
|
changeType: Enums_1.PlayerStateEventTypes.TrackChange,
|
|
816
822
|
details: {
|
|
@@ -922,7 +928,8 @@ class Player {
|
|
|
922
928
|
const sessionId = this.voiceState?.sessionId;
|
|
923
929
|
const token = this.voiceState?.event?.token;
|
|
924
930
|
const endpoint = this.voiceState?.event?.endpoint;
|
|
925
|
-
|
|
931
|
+
const channelId = this.voiceState?.channelId;
|
|
932
|
+
if (!sessionId || !token || !endpoint || !channelId) {
|
|
926
933
|
this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[MANAGER] Voice state is not properly initialized for player ${this.guildId}. The bot might not be connected to a voice channel.`);
|
|
927
934
|
throw new MagmastreamError_1.MagmaStreamError({
|
|
928
935
|
code: Enums_1.MagmaStreamErrorCode.PLAYER_STATE_INVALID,
|
|
@@ -936,7 +943,7 @@ class Player {
|
|
|
936
943
|
this.manager.players.set(this.guildId, this);
|
|
937
944
|
await this.node.rest.updatePlayer({
|
|
938
945
|
guildId: this.guildId,
|
|
939
|
-
data: { paused: this.paused, volume: this.volume, position: playerPosition, encodedTrack: currentTrack?.track, voice: { token, endpoint, sessionId } },
|
|
946
|
+
data: { paused: this.paused, volume: this.volume, position: playerPosition, encodedTrack: currentTrack?.track, voice: { token, endpoint, sessionId, channelId } },
|
|
940
947
|
});
|
|
941
948
|
await this.filters.updateFilters();
|
|
942
949
|
}
|
|
@@ -953,115 +960,6 @@ class Player {
|
|
|
953
960
|
console.error(error);
|
|
954
961
|
}
|
|
955
962
|
}
|
|
956
|
-
/**
|
|
957
|
-
* Transfers the player to a new server. If the player already exists on the new server
|
|
958
|
-
* and force is false, this method will return the existing player. Otherwise, a new player
|
|
959
|
-
* will be created and the current player will be destroyed.
|
|
960
|
-
* @param {PlayerOptions} newOptions - The new options for the player.
|
|
961
|
-
* @param {boolean} force - Whether to force the creation of a new player.
|
|
962
|
-
* @returns {Promise<Player>} - The new player instance.
|
|
963
|
-
*/
|
|
964
|
-
async switchGuild(newOptions, force = false) {
|
|
965
|
-
if (!newOptions.guildId) {
|
|
966
|
-
throw new MagmastreamError_1.MagmaStreamError({
|
|
967
|
-
code: Enums_1.MagmaStreamErrorCode.PLAYER_INVALID_CONFIG,
|
|
968
|
-
message: "guildId is required for switchGuild",
|
|
969
|
-
});
|
|
970
|
-
}
|
|
971
|
-
if (!newOptions.voiceChannelId) {
|
|
972
|
-
throw new MagmastreamError_1.MagmaStreamError({
|
|
973
|
-
code: Enums_1.MagmaStreamErrorCode.PLAYER_INVALID_CONFIG,
|
|
974
|
-
message: "voiceChannelId is required for switchGuild",
|
|
975
|
-
});
|
|
976
|
-
}
|
|
977
|
-
if (!newOptions.textChannelId) {
|
|
978
|
-
throw new MagmastreamError_1.MagmaStreamError({
|
|
979
|
-
code: Enums_1.MagmaStreamErrorCode.PLAYER_INVALID_CONFIG,
|
|
980
|
-
message: "textChannelId is required for switchGuild",
|
|
981
|
-
});
|
|
982
|
-
}
|
|
983
|
-
// Check if a player already exists for the new guild
|
|
984
|
-
let newPlayer = this.manager.getPlayer(newOptions.guildId);
|
|
985
|
-
// If the player already exists and force is false, return the existing player
|
|
986
|
-
if (newPlayer && !force)
|
|
987
|
-
return newPlayer;
|
|
988
|
-
const oldPlayerProperties = {
|
|
989
|
-
paused: this.paused,
|
|
990
|
-
selfMute: this.options.selfMute,
|
|
991
|
-
selfDeafen: this.options.selfDeafen,
|
|
992
|
-
volume: this.volume,
|
|
993
|
-
position: this.position,
|
|
994
|
-
queue: {
|
|
995
|
-
current: await this.queue.getCurrent(),
|
|
996
|
-
tracks: [...(await this.queue.getTracks())],
|
|
997
|
-
previous: [...(await this.queue.getPrevious())],
|
|
998
|
-
},
|
|
999
|
-
trackRepeat: this.trackRepeat,
|
|
1000
|
-
queueRepeat: this.queueRepeat,
|
|
1001
|
-
dynamicRepeat: this.dynamicRepeat,
|
|
1002
|
-
dynamicRepeatIntervalMs: this.dynamicRepeatIntervalMs,
|
|
1003
|
-
ClientUser: this.get("Internal_AutoplayUser"),
|
|
1004
|
-
filters: this.filters,
|
|
1005
|
-
nowPlayingMessage: this.nowPlayingMessage,
|
|
1006
|
-
isAutoplay: this.isAutoplay,
|
|
1007
|
-
applyVolumeAsFilter: this.options.applyVolumeAsFilter,
|
|
1008
|
-
pauseOnDisconnect: this.options.pauseOnDisconnect,
|
|
1009
|
-
};
|
|
1010
|
-
// If force is true, destroy the existing player for the new guild
|
|
1011
|
-
if (force && newPlayer) {
|
|
1012
|
-
await newPlayer.destroy();
|
|
1013
|
-
}
|
|
1014
|
-
newOptions.nodeIdentifier = newOptions.nodeIdentifier ?? this.options.nodeIdentifier;
|
|
1015
|
-
newOptions.selfDeafen = newOptions.selfDeafen ?? oldPlayerProperties.selfDeafen;
|
|
1016
|
-
newOptions.selfMute = newOptions.selfMute ?? oldPlayerProperties.selfMute;
|
|
1017
|
-
newOptions.volume = newOptions.volume ?? oldPlayerProperties.volume;
|
|
1018
|
-
newOptions.applyVolumeAsFilter = newOptions.applyVolumeAsFilter ?? oldPlayerProperties.applyVolumeAsFilter;
|
|
1019
|
-
newOptions.pauseOnDisconnect = newOptions.pauseOnDisconnect ?? oldPlayerProperties.pauseOnDisconnect;
|
|
1020
|
-
// Deep clone the current player
|
|
1021
|
-
const clonedPlayer = this.manager.create(newOptions);
|
|
1022
|
-
// Connect the cloned player to the new voice channel
|
|
1023
|
-
clonedPlayer.connect();
|
|
1024
|
-
// Update the player's state on the Lavalink node
|
|
1025
|
-
await clonedPlayer.node.rest.updatePlayer({
|
|
1026
|
-
guildId: clonedPlayer.guildId,
|
|
1027
|
-
data: {
|
|
1028
|
-
paused: oldPlayerProperties.paused,
|
|
1029
|
-
volume: oldPlayerProperties.volume,
|
|
1030
|
-
position: oldPlayerProperties.position,
|
|
1031
|
-
encodedTrack: oldPlayerProperties.queue.current?.track,
|
|
1032
|
-
},
|
|
1033
|
-
});
|
|
1034
|
-
await clonedPlayer.queue.setCurrent(oldPlayerProperties.queue.current);
|
|
1035
|
-
await clonedPlayer.queue.addPrevious(oldPlayerProperties.queue.previous);
|
|
1036
|
-
await clonedPlayer.queue.add(oldPlayerProperties.queue.tracks);
|
|
1037
|
-
clonedPlayer.filters = oldPlayerProperties.filters;
|
|
1038
|
-
clonedPlayer.isAutoplay = oldPlayerProperties.isAutoplay;
|
|
1039
|
-
clonedPlayer.nowPlayingMessage = oldPlayerProperties.nowPlayingMessage;
|
|
1040
|
-
clonedPlayer.trackRepeat = oldPlayerProperties.trackRepeat;
|
|
1041
|
-
clonedPlayer.queueRepeat = oldPlayerProperties.queueRepeat;
|
|
1042
|
-
clonedPlayer.dynamicRepeat = oldPlayerProperties.dynamicRepeat;
|
|
1043
|
-
clonedPlayer.dynamicRepeatIntervalMs = oldPlayerProperties.dynamicRepeatIntervalMs;
|
|
1044
|
-
clonedPlayer.set("Internal_AutoplayUser", oldPlayerProperties.ClientUser);
|
|
1045
|
-
clonedPlayer.paused = oldPlayerProperties.paused;
|
|
1046
|
-
// Update filters for the cloned player
|
|
1047
|
-
await clonedPlayer.filters.updateFilters();
|
|
1048
|
-
// Debug information
|
|
1049
|
-
const debugInfo = {
|
|
1050
|
-
success: true,
|
|
1051
|
-
message: `Transferred ${await clonedPlayer.queue.size()} tracks successfully to <#${newOptions.voiceChannelId}> bound to <#${newOptions.textChannelId}>.`,
|
|
1052
|
-
player: {
|
|
1053
|
-
guildId: clonedPlayer.guildId,
|
|
1054
|
-
voiceChannelId: clonedPlayer.voiceChannelId,
|
|
1055
|
-
textChannelId: clonedPlayer.textChannelId,
|
|
1056
|
-
volume: clonedPlayer.volume,
|
|
1057
|
-
playing: clonedPlayer.playing,
|
|
1058
|
-
queueSize: clonedPlayer.queue.size,
|
|
1059
|
-
},
|
|
1060
|
-
};
|
|
1061
|
-
this.manager.emit(Enums_1.ManagerEventTypes.Debug, `[PLAYER] Transferred player to a new server: ${Utils_1.JSONUtils.safe(debugInfo, 2)}.`);
|
|
1062
|
-
// Return the cloned player
|
|
1063
|
-
return clonedPlayer;
|
|
1064
|
-
}
|
|
1065
963
|
/**
|
|
1066
964
|
* Retrieves the data associated with the player.
|
|
1067
965
|
* @returns {Record<string, unknown>} - The data associated with the player.
|
|
@@ -1076,6 +974,13 @@ class Player {
|
|
|
1076
974
|
getDynamicLoopIntervalPublic() {
|
|
1077
975
|
return this.dynamicLoopInterval;
|
|
1078
976
|
}
|
|
977
|
+
/**
|
|
978
|
+
* Retrieves the data associated with the player.
|
|
979
|
+
* @returns {Record<string, unknown>} - The data associated with the player.
|
|
980
|
+
*/
|
|
981
|
+
getSerializableData() {
|
|
982
|
+
return { ...this.data };
|
|
983
|
+
}
|
|
1079
984
|
/**
|
|
1080
985
|
* Retrieves the current lyrics for the playing track.
|
|
1081
986
|
* @param skipTrackSource - Indicates whether to skip the track source when fetching lyrics.
|
|
@@ -1239,5 +1144,26 @@ class Player {
|
|
|
1239
1144
|
this.manager.emit(Enums_1.ManagerEventTypes.Debug, `VoiceReceiver error for player ${this.guildId}: ${error.message}`);
|
|
1240
1145
|
this.manager.emit(Enums_1.ManagerEventTypes.VoiceReceiverError, this, error);
|
|
1241
1146
|
}
|
|
1147
|
+
/**
|
|
1148
|
+
* Updates the voice state for the player.
|
|
1149
|
+
* @returns {Promise<void>} - A promise that resolves when the voice state is updated.
|
|
1150
|
+
*/
|
|
1151
|
+
async updateVoice() {
|
|
1152
|
+
const vs = this.voiceState;
|
|
1153
|
+
const ev = vs?.event;
|
|
1154
|
+
if (!vs?.channelId || !vs?.sessionId || !ev?.token || !ev?.endpoint)
|
|
1155
|
+
return;
|
|
1156
|
+
await this.node.rest.updatePlayer({
|
|
1157
|
+
guildId: this.options.guildId,
|
|
1158
|
+
data: {
|
|
1159
|
+
voice: {
|
|
1160
|
+
token: ev.token,
|
|
1161
|
+
endpoint: ev.endpoint,
|
|
1162
|
+
sessionId: vs.sessionId,
|
|
1163
|
+
channelId: vs.channelId,
|
|
1164
|
+
},
|
|
1165
|
+
},
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1242
1168
|
}
|
|
1243
1169
|
exports.Player = Player;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Manager } from "./Manager";
|
|
2
|
+
/**
|
|
3
|
+
* Base abstract class for all plugins.
|
|
4
|
+
* Users must extend this and implement load and unload methods.
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class Plugin {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
/**
|
|
9
|
+
* @param name The name of the plugin
|
|
10
|
+
*/
|
|
11
|
+
constructor(name: string);
|
|
12
|
+
/**
|
|
13
|
+
* Load the plugin.
|
|
14
|
+
* @param manager The MagmaStream Manager instance
|
|
15
|
+
*/
|
|
16
|
+
abstract load(manager: Manager): void;
|
|
17
|
+
/**
|
|
18
|
+
* Unload the plugin.
|
|
19
|
+
* Called on shutdown to gracefully cleanup resources or detach listeners.
|
|
20
|
+
* @param manager The MagmaStream Manager instance
|
|
21
|
+
*/
|
|
22
|
+
abstract unload(manager: Manager): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Node } from "./Node";
|
|
2
|
+
import { Manager } from "./Manager";
|
|
3
|
+
import { LavaPlayer, RestPlayOptions } from "./Types";
|
|
4
|
+
/** Handles the requests sent to the Lavalink REST API. */
|
|
5
|
+
export declare class Rest {
|
|
6
|
+
/** The Node that this Rest instance is connected to. */
|
|
7
|
+
private node;
|
|
8
|
+
/** The ID of the current session. */
|
|
9
|
+
private sessionId;
|
|
10
|
+
/** The password for the Node. */
|
|
11
|
+
private readonly password;
|
|
12
|
+
/** The URL of the Node. */
|
|
13
|
+
private readonly url;
|
|
14
|
+
/** The Manager instance. */
|
|
15
|
+
manager: Manager;
|
|
16
|
+
/** Whether the node is a NodeLink. */
|
|
17
|
+
isNodeLink: boolean;
|
|
18
|
+
constructor(node: Node, manager: Manager);
|
|
19
|
+
/**
|
|
20
|
+
* Sets the session ID.
|
|
21
|
+
* This method is used to set the session ID after a resume operation is done.
|
|
22
|
+
* @param {string} sessionId The session ID to set.
|
|
23
|
+
* @returns {string} Returns the set session ID.
|
|
24
|
+
*/
|
|
25
|
+
setSessionId(sessionId: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves one the player that is currently running on the node.
|
|
28
|
+
* @returns {Promise<unknown>} Returns the result of the GET request.
|
|
29
|
+
*/
|
|
30
|
+
getPlayer(guildId: string): Promise<LavaPlayer>;
|
|
31
|
+
/**
|
|
32
|
+
* Sends a PATCH request to update player related data.
|
|
33
|
+
* @param {RestPlayOptions} options The options to update the player with.
|
|
34
|
+
* @returns {Promise<unknown>} Returns the result of the PATCH request.
|
|
35
|
+
*/
|
|
36
|
+
updatePlayer(options: RestPlayOptions): Promise<unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* Sends a DELETE request to the server to destroy the player.
|
|
39
|
+
* @param {string} guildId The guild ID of the player to destroy.
|
|
40
|
+
* @returns {Promise<unknown>} Returns the result of the DELETE request.
|
|
41
|
+
*/
|
|
42
|
+
destroyPlayer(guildId: string): Promise<unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Updates the session status for resuming.
|
|
45
|
+
* This method sends a PATCH request to update the session's resuming status and timeout.
|
|
46
|
+
*
|
|
47
|
+
* @param {boolean} resuming - Indicates whether the session should be set to resuming.
|
|
48
|
+
* @param {number} timeout - The timeout duration for the session resume.
|
|
49
|
+
* @returns {Promise<unknown>} The result of the PATCH request.
|
|
50
|
+
*/
|
|
51
|
+
updateSession(resuming: boolean, timeout: number): Promise<unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* Sends a request to the specified endpoint and returns the response data.
|
|
54
|
+
* @param {string} method The HTTP method to use for the request.
|
|
55
|
+
* @param {string} endpoint The endpoint to send the request to.
|
|
56
|
+
* @param {unknown} [body] The data to send in the request body.
|
|
57
|
+
* @returns {Promise<unknown>} The response data of the request.
|
|
58
|
+
*/
|
|
59
|
+
private request;
|
|
60
|
+
/**
|
|
61
|
+
* Sends a GET request to the specified endpoint and returns the response data.
|
|
62
|
+
* @param {string} endpoint The endpoint to send the GET request to.
|
|
63
|
+
* @returns {Promise<unknown>} The response data of the GET request.
|
|
64
|
+
*/
|
|
65
|
+
get(endpoint: string): Promise<unknown>;
|
|
66
|
+
/**
|
|
67
|
+
* Sends a PATCH request to the specified endpoint and returns the response data.
|
|
68
|
+
* @param {string} endpoint The endpoint to send the PATCH request to.
|
|
69
|
+
* @param {unknown} body The data to send in the request body.
|
|
70
|
+
* @returns {Promise<unknown>} The response data of the PATCH request.
|
|
71
|
+
*/
|
|
72
|
+
patch(endpoint: string, body: unknown): Promise<unknown>;
|
|
73
|
+
/**
|
|
74
|
+
* Sends a POST request to the specified endpoint and returns the response data.
|
|
75
|
+
* @param {string} endpoint The endpoint to send the POST request to.
|
|
76
|
+
* @param {unknown} body The data to send in the request body.
|
|
77
|
+
* @returns {Promise<unknown>} The response data of the POST request.
|
|
78
|
+
*/
|
|
79
|
+
post(endpoint: string, body: unknown): Promise<unknown>;
|
|
80
|
+
/**
|
|
81
|
+
* Sends a PUT request to the specified endpoint and returns the response data.
|
|
82
|
+
* @param {string} endpoint The endpoint to send the PUT request to.
|
|
83
|
+
* @param {unknown} body The data to send in the request body.
|
|
84
|
+
* @returns {Promise<unknown>} The response data of the PUT request.
|
|
85
|
+
*/
|
|
86
|
+
put(endpoint: string, body: unknown): Promise<unknown>;
|
|
87
|
+
/**
|
|
88
|
+
* Sends a DELETE request to the specified endpoint.
|
|
89
|
+
* @param {string} endpoint - The endpoint to send the DELETE request to.
|
|
90
|
+
* @returns {Promise<unknown>} The response data of the DELETE request.
|
|
91
|
+
*/
|
|
92
|
+
delete(endpoint: string): Promise<unknown>;
|
|
93
|
+
}
|
package/dist/structures/Rest.js
CHANGED
|
@@ -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
|
-
|
|
110
|
-
return response.data;
|
|
111
|
+
response = await (0, axios_1.default)(config);
|
|
111
112
|
}
|
|
112
113
|
catch (err) {
|
|
113
|
-
const
|
|
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: `
|
|
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.
|