Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Bug #3

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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