From de41c8b28c2766f82b3b6b6f770fd2632084686c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FAY=E3=82=B7?= <103030954+FAYStarNext@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:35:56 +0700 Subject: [PATCH 1/2] chore: Update package.json version to 1.0.2-indev --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index af6d109..b32d190 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "type": "module", - "version": "1.0.1-indev", + "version": "1.0.2-indev", "description": "Sunday a lavalink wrapper", "license": "MIT", "scripts": { From 92d38ce745407b6243e788b6ad9fed432efbb87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FAY=E3=82=B7?= <103030954+FAYStarNext@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:42:00 +0700 Subject: [PATCH 2/2] chore: Refactor Manager class to use arrow function in forEach loop and handle errors in search method --- src/structures/Manager.ts | 55 +++++++++++++++++++++------------------ src/structures/Player.ts | 6 +---- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/structures/Manager.ts b/src/structures/Manager.ts index abef611..615af45 100644 --- a/src/structures/Manager.ts +++ b/src/structures/Manager.ts @@ -150,8 +150,10 @@ export class Manager extends TypedEmitter { } if (this.options.nodes) { - for (const nodeOptions of this.options.nodes) - new (Structure.get("Node"))(nodeOptions); + this.options.nodes.forEach((nodeOptions, index) => { + const node = new (Structure.get("Node"))(nodeOptions); + this.nodes.set(index.toString(), node); + }); } } @@ -189,28 +191,28 @@ export class Manager extends TypedEmitter { * @param requester * @returns The search result. */ - public search( + public async search( query: string | SearchQuery, requester?: unknown ): Promise { - return new Promise(async (resolve, reject) => { - const node = this.leastUsedNodes.first(); - if (!node) throw new Error("No available nodes."); - - const _query: SearchQuery = typeof query === "string" ? { query } : query; - const _source = Manager.DEFAULT_SOURCES[_query.source ?? this.options.defaultSearchPlatform] ?? _query.source; - - let search = _query.query; - if (!/^https?:\/\//.test(search)) { - search = `${_source}:${search}`; - } - - const res = await node.rest.get(`/loadtracks?identifier=${encodeURIComponent(search)}`) - + const node = this.leastUsedNodes.first(); + if (!node) throw new Error("No available nodes."); + + const _query: SearchQuery = typeof query === "string" ? { query } : query; + const _source = Manager.DEFAULT_SOURCES[_query.source ?? this.options.defaultSearchPlatform] ?? _query.source; + + let search = _query.query; + if (!/^https?:\/\//.test(search)) { + search = `${_source}:${search}`; + } + + try { + const res = await node.rest.get(`/loadtracks?identifier=${encodeURIComponent(search)}`); + if (!res) { - return reject(new Error("Query not found.")); + throw new Error("Query not found."); } - + const result: SearchResult = { loadType: res.loadType, exception: res.exception ?? null, @@ -218,7 +220,7 @@ export class Manager extends TypedEmitter { TrackUtils.build(track, requester) ) ?? [], }; - + if (result.loadType === "PLAYLIST_LOADED") { result.playlist = { name: res.playlistInfo.name, @@ -231,11 +233,12 @@ export class Manager extends TypedEmitter { .reduce((acc: number, cur: Track) => acc + (cur.duration || 0), 0), }; } - - return resolve(result); - }); - } - + + return result; + } catch (error) { + throw new Error(`Search failed: ${error.message}`); + } + } /** * Decodes the base64 encoded tracks and returns a TrackData array. * @param tracks @@ -248,7 +251,7 @@ export class Manager extends TypedEmitter { if (!res) { return reject(new Error("No data returned from query.")); } - return resolve(res); + return res; }); } diff --git a/src/structures/Player.ts b/src/structures/Player.ts index 5de877d..4c016d2 100644 --- a/src/structures/Player.ts +++ b/src/structures/Player.ts @@ -144,12 +144,8 @@ export class Player { // Hacky support for providing an array if (Array.isArray(bands[0])) bands = bands[0] as unknown as EqualizerBand[] - if (!bands.length || !bands.every( - (band) => JSON.stringify(Object.keys(band).sort()) === '["band","gain"]' - ) - ) + if (!bands.length || !bands.every((band) => JSON.stringify(Object.keys(band).sort((a, b) => a.localeCompare(b))) === '["band","gain"]')) throw new TypeError("Bands must be a non-empty object array containing 'band' and 'gain' properties."); - for (const { band, gain } of bands) this.bands[band] = gain; this.node.send({