Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
SxMAbel authored Feb 14, 2025
2 parents 0730d0d + f17c4cf commit c3f2434
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "magmastream",
"version": "2.8.3",
"version": "2.8.4",
"description": "A user-friendly Lavalink client designed for NodeJS.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,10 @@ export class Manager extends EventEmitter {
/**
* Initiates the Manager.
* @param clientId - The Discord client ID (required).
* @param clusterId - The cluster ID which runs the current process (required).
* @returns The manager instance.
*/
public init(clientId: string): this {
public init(clientId: string, clusterId: number = 0): this {
if (this.initiated) {
return this;
}
Expand All @@ -534,10 +535,15 @@ export class Manager extends EventEmitter {
}

this.options.clientId = clientId;
if (typeof clusterId !== "number") {
console.warn('"clusterId" is not a valid number, defaulting to 0.');
clusterId = 0;
}
this.options.clusterId = clusterId;

for (const node of this.nodes.values()) {
try {
node.connect();
node.connect(); // Connect the node
} catch (err) {
this.emit("nodeError", node, err);
}
Expand Down Expand Up @@ -924,6 +930,8 @@ export interface ManagerOptions {
clientId?: string;
/** Value to use for the `Client-Name` header. */
clientName?: string;
/** The array of shard IDs connected to this manager instance. */
clusterId?: number;
/** A array of plugins to use. */
plugins?: Plugin[];
/** Whether players should automatically play the next song. */
Expand Down
28 changes: 25 additions & 3 deletions src/structures/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export class Node {
/**
* Loads session IDs from the sessionIds.json file if it exists.
* The session IDs are used to resume sessions for each node.
*
* The session IDs are stored in the sessionIds.json file as a composite key
* of the node identifier and cluster ID. This allows multiple clusters to
* be used with the same node identifier.
*/
public loadSessionIds(): void {
// Check if the sessionIds.json file exists
Expand All @@ -160,6 +164,13 @@ export class Node {

// Parse the JSON string into an object and convert it into a Map
sessionIdsMap = new Map(Object.entries(JSON.parse(sessionIdsData)));

// Check if the session IDs Map contains the session ID for this node
const compositeKey = `${this.options.identifier}::${this.manager.options.clusterId}`;
if (sessionIdsMap.has(compositeKey)) {
// Set the session ID on this node if it exists in the session IDs Map
this.sessionId = sessionIdsMap.get(compositeKey);
}
}
}

Expand All @@ -168,13 +179,21 @@ export class Node {
*
* This method is called after the session ID has been updated, and it
* writes the new session ID to the sessionIds.json file.
*
* @remarks
* The session ID is stored in the sessionIds.json file as a composite key
* of the node identifier and cluster ID. This allows multiple clusters to
* be used with the same node identifier.
*/
public updateSessionId(): void {
// Emit a debug event indicating that the session IDs are being updated
this.manager.emit(ManagerEventTypes.Debug, `[NODE] Updating sessionIds to file: ${sessionIdsFilePath}`);

// Create a composite key using identifier and clusterId
const compositeKey = `${this.options.identifier}::${this.manager.options.clusterId}`;

// Update the session IDs Map with the new session ID
sessionIdsMap.set(this.options.identifier, this.sessionId);
sessionIdsMap.set(compositeKey, this.sessionId);

// Write the updated session IDs Map to the sessionIds.json file
fs.writeFileSync(sessionIdsFilePath, JSON.stringify(Object.fromEntries(sessionIdsMap)));
Expand All @@ -198,10 +217,13 @@ export class Node {
"Client-Name": this.manager.options.clientName,
};

// Create the composite key for the current node
const compositeKey = `${this.options.identifier}::${this.manager.options.clusterId}`;

if (this.sessionId) {
headers["Session-Id"] = this.sessionId;
} else if (this.options.resumeStatus && sessionIdsMap.has(this.options.identifier)) {
this.sessionId = sessionIdsMap.get(this.options.identifier) || null;
} else if (this.options.resumeStatus && sessionIdsMap.has(compositeKey)) {
this.sessionId = sessionIdsMap.get(compositeKey) || null;
headers["Session-Id"] = this.sessionId;
}

Expand Down
16 changes: 10 additions & 6 deletions src/structures/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,17 +1079,21 @@ export class Player {

/**
* Retrieves the current lyrics for the playing track.
* @param skipTrackSource Indicates whether to skip the track source when fetching lyrics.
* @returns {Promise<Lyrics>} The lyrics of the current track.
* @throws {RangeError} If the 'lavalyrics-plugin' is not available on the Lavalink node.
* @param skipTrackSource - Indicates whether to skip the track source when fetching lyrics.
* @returns {Promise<Lyrics>} - The lyrics of the current track.
* @throws {RangeError} - If the 'lavalyrics-plugin' is not available on the Lavalink node.
*/
public async getCurrentLyrics(skipTrackSource: boolean = false): Promise<Lyrics> {
// Check if the 'lavalyrics-plugin' is available on the node
if (!this.node.info.plugins.some((plugin: { name: string }) => plugin.name === "lavalyrics-plugin")) {
const hasLyricsPlugin = this.node.info.plugins.some((plugin: { name: string }) => plugin.name === "lavalyrics-plugin");
if (!hasLyricsPlugin) {
throw new RangeError(`There is no lavalyrics-plugin available in the Lavalink node: ${this.node.options.identifier}`);
}

let result = (await this.node.rest.get(`/v4/lyrics?track=${encodeURIComponent(this.queue.current.track)}&skipTrackSource=${skipTrackSource}`)) as Lyrics;
// Fetch the lyrics for the current track from the Lavalink node
let result = await this.node.getLyrics(this.queue.current, skipTrackSource) as Lyrics;

// If no lyrics are found, return a default empty lyrics object
if (!result) {
result = {
source: null,
Expand All @@ -1099,7 +1103,7 @@ export class Player {
plugin: [],
};
}

return result;
}
}
Expand Down

0 comments on commit c3f2434

Please sign in to comment.