Skip to content

Commit

Permalink
Merge pull request #3 from FAYStarNext/dev
Browse files Browse the repository at this point in the history
Fix: Bug
  • Loading branch information
EvarinDev authored Jul 21, 2024
2 parents f0f621a + 92d38ce commit 2a0bc88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
55 changes: 29 additions & 26 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
}

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);
});
}
}

Expand Down Expand Up @@ -189,36 +191,36 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
* @param requester
* @returns The search result.
*/
public search(
public async search(
query: string | SearchQuery,
requester?: unknown
): Promise<SearchResult> {
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,
tracks: res.tracks?.map((track: TrackData) =>
TrackUtils.build(track, requester)
) ?? [],
};

if (result.loadType === "PLAYLIST_LOADED") {
result.playlist = {
name: res.playlistInfo.name,
Expand All @@ -231,11 +233,12 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
.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
Expand All @@ -248,7 +251,7 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
if (!res) {
return reject(new Error("No data returned from query."));
}
return resolve(res);
return res;
});
}

Expand Down
6 changes: 1 addition & 5 deletions src/structures/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 2a0bc88

Please sign in to comment.