magmastream 2.9.3-dev.24 → 2.9.3-dev.26

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.
@@ -0,0 +1,259 @@
1
+ import { Collection } from "@discordjs/collection";
2
+ import { GatewayVoiceStateUpdate } from "discord-api-types/v10";
3
+ import { EventEmitter } from "events";
4
+ import { Node } from "./Node";
5
+ import { Player } from "./Player";
6
+ import { Redis as RedisClient } from "ioredis";
7
+ import { AnyGuild, AnyUser, ManagerEvents, ManagerInitOptions, ManagerOptions, NodeOptions, PlayerOptions, SearchQuery, SearchResult, TrackData, VoicePacket, VoiceServer, VoiceState } from "./Types";
8
+ /**
9
+ * The main hub for interacting with Lavalink and using Magmastream.
10
+ */
11
+ export declare class Manager extends EventEmitter {
12
+ /** The map of players. */
13
+ readonly players: Collection<string, Player>;
14
+ /** The map of nodes. */
15
+ readonly nodes: Collection<string, Node>;
16
+ /** The options that were set. */
17
+ readonly options: ManagerOptions;
18
+ initiated: boolean;
19
+ redis?: RedisClient;
20
+ private _send;
21
+ private _getUser?;
22
+ private _getGuild?;
23
+ private loadedPlugins;
24
+ /**
25
+ * Initiates the Manager class.
26
+ * @param options
27
+ * @param options.enabledPlugins - An array of enabledPlugins to load.
28
+ * @param options.nodes - An array of node options to create nodes from.
29
+ * @param options.playNextOnEnd - Whether to automatically play the first track in the queue when the player is created.
30
+ * @param options.autoPlaySearchPlatforms - The search platform autoplay will use. Fallback to Youtube if not found.
31
+ * @param options.enablePriorityMode - Whether to use the priority when selecting a node to play on.
32
+ * @param options.clientName - The name of the client to send to Lavalink.
33
+ * @param options.defaultSearchPlatform - The default search platform to use when searching for tracks.
34
+ * @param options.useNode - The strategy to use when selecting a node to play on.
35
+ * @param options.trackPartial - The partial track search results to use when searching for tracks. This partials will always be presented on each track.
36
+ * @param options.eventBatchDuration - The duration to wait before processing the collected player state events.
37
+ * @param options.eventBatchInterval - The interval to wait before processing the collected player state events.
38
+ */
39
+ constructor(options: ManagerOptions);
40
+ /**
41
+ * Initiates the Manager.
42
+ * @param clientId - The Discord client ID (only required when not using any of the magmastream wrappers).
43
+ * @param clusterId - The cluster ID which runs the current process (required).
44
+ * @returns The manager instance.
45
+ */
46
+ init(options?: ManagerInitOptions): Promise<this>;
47
+ /**
48
+ * Searches the enabled sources based off the URL or the `source` property.
49
+ * @param query
50
+ * @param requester
51
+ * @returns The search result.
52
+ */
53
+ search<T = unknown>(query: string | SearchQuery, requester?: T): Promise<SearchResult>;
54
+ /**
55
+ * Returns a player or undefined if it does not exist.
56
+ * @param guildId The guild ID of the player to retrieve.
57
+ * @returns The player if it exists, undefined otherwise.
58
+ */
59
+ getPlayer(guildId: string): Player | undefined;
60
+ /**
61
+ * Creates a player or returns one if it already exists.
62
+ * @param options The options to create the player with.
63
+ * @returns The created player.
64
+ */
65
+ create(options: PlayerOptions): Player;
66
+ /**
67
+ * Destroys a player.
68
+ * @param guildId The guild ID of the player to destroy.
69
+ * @returns A promise that resolves when the player has been destroyed.
70
+ */
71
+ destroy(guildId: string): Promise<void>;
72
+ /**
73
+ * Creates a new node or returns an existing one if it already exists.
74
+ * @param options - The options to create the node with.
75
+ * @returns The created node.
76
+ */
77
+ createNode(options: NodeOptions): Node;
78
+ /**
79
+ * Destroys a node if it exists. Emits a debug event if the node is found and destroyed.
80
+ * @param identifier - The identifier of the node to destroy.
81
+ * @returns {void}
82
+ * @emits {debug} - Emits a debug message indicating the node is being destroyed.
83
+ */
84
+ destroyNode(identifier: string): Promise<void>;
85
+ /**
86
+ * Attaches an event listener to the manager.
87
+ * @param event The event to listen for.
88
+ * @param listener The function to call when the event is emitted.
89
+ * @returns The manager instance for chaining.
90
+ */
91
+ on<T extends keyof ManagerEvents>(event: T, listener: (...args: ManagerEvents[T]) => void): this;
92
+ /**
93
+ * Updates the voice state of a player based on the provided data.
94
+ * @param data - The data containing voice state information, which can be a VoicePacket, VoiceServer, or VoiceState.
95
+ * @returns A promise that resolves when the voice state update is handled.
96
+ * @emits {debug} - Emits a debug message indicating the voice state is being updated.
97
+ */
98
+ updateVoiceState(data: VoicePacket | VoiceServer | VoiceState): Promise<void>;
99
+ /**
100
+ * Decodes an array of base64 encoded tracks and returns an array of TrackData.
101
+ * Emits a debug event with the tracks being decoded.
102
+ * @param tracks - An array of base64 encoded track strings.
103
+ * @returns A promise that resolves to an array of TrackData objects.
104
+ * @throws Will throw an error if no nodes are available or if the API request fails.
105
+ */
106
+ decodeTracks(tracks: string[]): Promise<TrackData[]>;
107
+ /**
108
+ * Decodes a base64 encoded track and returns a TrackData.
109
+ * @param track - The base64 encoded track string.
110
+ * @returns A promise that resolves to a TrackData object.
111
+ * @throws Will throw an error if no nodes are available or if the API request fails.
112
+ */
113
+ decodeTrack(track: string): Promise<TrackData>;
114
+ /**
115
+ * Saves player states.
116
+ * @param {string} guildId - The guild ID of the player to save
117
+ */
118
+ savePlayerState(guildId: string): Promise<void>;
119
+ /**
120
+ * Sleeps for a specified amount of time.
121
+ * @param ms The amount of time to sleep in milliseconds.
122
+ * @returns A promise that resolves after the specified amount of time.
123
+ */
124
+ private sleep;
125
+ private restorePlayerFromState;
126
+ private restoreQueue;
127
+ private restorePreviousQueue;
128
+ private restoreRepeatState;
129
+ private restorePlayerData;
130
+ private restoreFilters;
131
+ /**
132
+ * Loads player states from the JSON file.
133
+ * @param nodeId The ID of the node to load player states from.
134
+ * @returns A promise that resolves when the player states have been loaded.
135
+ */
136
+ loadPlayerStates(nodeId: string): Promise<void>;
137
+ /**
138
+ * Returns the node to use based on the configured `useNode` and `enablePriorityMode` options.
139
+ * If `enablePriorityMode` is true, the node is chosen based on priority, otherwise it is chosen based on the `useNode` option.
140
+ * If `useNode` is "leastLoad", the node with the lowest load is chosen, if it is "leastPlayers", the node with the fewest players is chosen.
141
+ * If `enablePriorityMode` is false and `useNode` is not set, the node with the lowest load is chosen.
142
+ * @returns {Node} The node to use.
143
+ */
144
+ get useableNode(): Node;
145
+ /**
146
+ * Handles the shutdown of the process by saving all active players' states and optionally cleaning up inactive players.
147
+ * This function is called when the process is about to exit.
148
+ * It iterates through all players and calls {@link savePlayerState} to save their states.
149
+ * Optionally, it also calls {@link cleanupInactivePlayers} to remove any stale player state files.
150
+ * After saving and cleaning up, it exits the process.
151
+ */
152
+ handleShutdown(): Promise<void>;
153
+ /**
154
+ * Parses a YouTube title into a clean title and author.
155
+ * @param title - The original title of the YouTube video.
156
+ * @param originalAuthor - The original author of the YouTube video.
157
+ * @returns An object with the clean title and author.
158
+ */
159
+ private parseYouTubeTitle;
160
+ /**
161
+ * Balances brackets in a given string by ensuring all opened brackets are closed correctly.
162
+ * @param str - The input string that may contain unbalanced brackets.
163
+ * @returns A new string with balanced brackets.
164
+ */
165
+ private balanceBrackets;
166
+ /**
167
+ * Escapes a string by replacing special regex characters with their escaped counterparts.
168
+ * @param string - The string to escape.
169
+ * @returns The escaped string.
170
+ */
171
+ private escapeRegExp;
172
+ /**
173
+ * Checks if the given data is a voice update.
174
+ * @param data The data to check.
175
+ * @returns Whether the data is a voice update.
176
+ */
177
+ private isVoiceUpdate;
178
+ /**
179
+ * Determines if the provided update is a valid voice update.
180
+ * A valid update must contain either a token or a session_id.
181
+ *
182
+ * @param update - The voice update data to validate, which can be a VoicePacket, VoiceServer, or VoiceState.
183
+ * @returns {boolean} - True if the update is valid, otherwise false.
184
+ */
185
+ private isValidUpdate;
186
+ /**
187
+ * Handles a voice server update by updating the player's voice state and sending the voice state to the Lavalink node.
188
+ * @param player The player for which the voice state is being updated.
189
+ * @param update The voice server data received from Discord.
190
+ * @returns A promise that resolves when the voice state update is handled.
191
+ * @emits {debug} - Emits a debug message indicating the voice state is being updated.
192
+ */
193
+ private handleVoiceServerUpdate;
194
+ /**
195
+ * Handles a voice state update by updating the player's voice channel and session ID if provided, or by disconnecting and destroying the player if the channel ID is null.
196
+ * @param player The player for which the voice state is being updated.
197
+ * @param update The voice state data received from Discord.
198
+ * @emits {playerMove} - Emits a player move event if the channel ID is provided and the player is currently connected to a different voice channel.
199
+ * @emits {playerDisconnect} - Emits a player disconnect event if the channel ID is null.
200
+ */
201
+ private handleVoiceStateUpdate;
202
+ /**
203
+ * Cleans up an inactive player by removing its state data.
204
+ * This is done to prevent stale state data from accumulating.
205
+ * @param guildId The guild ID of the player to clean up.
206
+ */
207
+ cleanupInactivePlayer(guildId: string): Promise<void>;
208
+ /**
209
+ * Loads the enabled plugins.
210
+ */
211
+ private loadPlugins;
212
+ /**
213
+ * Unloads the enabled plugins.
214
+ */
215
+ private unloadPlugins;
216
+ /**
217
+ * Clears all player states from the file system.
218
+ * This is done to prevent stale state files from accumulating on the file system.
219
+ */
220
+ private clearAllStoredPlayers;
221
+ /**
222
+ * Returns the nodes that has the least load.
223
+ * The load is calculated by dividing the lavalink load by the number of cores.
224
+ * The result is multiplied by 100 to get a percentage.
225
+ * @returns {Collection<string, Node>}
226
+ */
227
+ private get leastLoadNode();
228
+ /**
229
+ * Returns the nodes that have the least amount of players.
230
+ * Filters out disconnected nodes and sorts the remaining nodes
231
+ * by the number of players in ascending order.
232
+ * @returns {Collection<string, Node>} A collection of nodes sorted by player count.
233
+ */
234
+ private get leastPlayersNode();
235
+ /**
236
+ * Returns a node based on priority.
237
+ * The nodes are sorted by priority in descending order, and then a random number
238
+ * between 0 and 1 is generated. The node that has a cumulative weight greater than or equal to the
239
+ * random number is returned.
240
+ * If no node has a cumulative weight greater than or equal to the random number, the node with the
241
+ * lowest load is returned.
242
+ * @returns {Node} The node to use.
243
+ */
244
+ private get priorityNode();
245
+ protected send(packet: GatewayVoiceStateUpdate): unknown;
246
+ protected getUserFromCache(id: string): AnyUser | undefined;
247
+ protected getGuildFromCache(id: string): AnyGuild | undefined;
248
+ sendPacket(packet: GatewayVoiceStateUpdate): unknown;
249
+ /**
250
+ * Resolves a PortableUser or ID to a real user object.
251
+ * Can be overridden by wrapper managers to return wrapper-specific User classes.
252
+ */
253
+ resolveUser(user: AnyUser | string): Promise<AnyUser>;
254
+ /**
255
+ * Resolves a Guild ID to a real guild object.
256
+ * Can be overridden by wrapper managers to return wrapper-specific Guild classes.
257
+ */
258
+ resolveGuild(guildId: string): AnyGuild;
259
+ }