Skip to content

Commit

Permalink
Enhance error handling and add artwork fallback to prevent freezes (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiibe authored Aug 28, 2024
1 parent 66c0934 commit 4ec6ae4
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions music-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ while (true) {
}

async function main(rpc: Client) {
await rpc.connect();
console.log(rpc);
while (true) {
const timeout = await setActivity(rpc);
await sleep(timeout);
try {
await rpc.connect();
console.log("Connected to Discord RPC");
while (true) {
const timeout = await setActivity(rpc);
await sleep(timeout);
}
} catch (err) {
console.error("Error in main loop:", err);
await rpc.close(); // Ensure the connection is properly closed

console.log("Attempting to reconnect...");
await sleep(DEFAULT_TIMEOUT); // wait before attempting to reconnect
}
}

Expand Down Expand Up @@ -100,7 +108,18 @@ async function _getTrackExtras(
entity: "song",
term: query,
});
const resp = await fetch(`https://itunes.apple.com/search?${params}`);
const url = `https://itunes.apple.com/search?${params}`;
const resp = await fetch(url);

if (!resp.ok) {
console.error("iTunes API error:", resp.statusText, url);

return {
artworkUrl: await _getMBArtwork(artist, song, album) ?? null,
iTunesUrl: null,
};
}

const json: iTunesSearchResponse = await resp.json();

let result: iTunesSearchResult | undefined;
Expand Down Expand Up @@ -129,6 +148,7 @@ async function _getTrackExtras(
result?.artworkUrl100 ?? (await _getMBArtwork(artist, song, album)) ?? null;

const iTunesUrl = result?.trackViewUrl ?? null;

return { artworkUrl, iTunesUrl };
}
//#endregion
Expand Down

0 comments on commit 4ec6ae4

Please sign in to comment.