I’m trying to build a Discord bot that fetches song lyrics. The bot uses DisTube to play music and I want to grab the current song’s name to search for lyrics.
I’m using a lyrics API package but I’m running into issues when trying to use a variable for the song name. Here’s what I’ve tried:
const songTitle = getCurrentSongTitle();
const results = await lyricsClient.searchSongs(songTitle);
const firstResult = results[0];
console.log('Song info:', firstResult);
const songLyrics = await firstResult.getLyrics();
console.log('Lyrics:', songLyrics);
This code works fine if I replace songTitle with a hardcoded string. But when I use the variable, it doesn’t work. I’ve checked and songTitle definitely contains the correct song name.
Any ideas on how to make this work with a variable? Also, I’m getting a 403 error sometimes - could this be related?
Thanks for any help!
hey man, i had similar probs with my bot. try using template literals instead of normal strings, like this:
const results = await lyricsClient.searchSongs(${songTitle});
also, make sure ur not hitting the api too much. some have pretty strict limits n will block u if u go over. maybe add a cooldown or somethin to avoid that 403 error ur getting
I ran into a similar issue when building my lyrics bot. The problem might be with how you’re passing the variable to the API. Try wrapping the songTitle in encodeURIComponent() before sending it:
const encodedTitle = encodeURIComponent(songTitle);
const results = await lyricsClient.searchSongs(encodedTitle);
This ensures special characters in song titles don’t mess up the API request. As for the 403 error, that’s likely due to rate limiting. Most lyrics APIs have strict limits on requests. I’d suggest implementing a delay between requests and caching results to avoid hitting the API too frequently.
Also, double-check your API key is valid and you’re not exceeding usage limits. Some services require paid plans for higher volume. Hope this helps troubleshoot your issue!
I’ve encountered similar challenges with lyrics APIs. One potential issue could be character encoding. Try using the ‘utf-8’ encoding when sending the request:
const songTitle = getCurrentSongTitle();
const results = await lyricsClient.searchSongs(songTitle, { encoding: ‘utf-8’ });
If that doesn’t work, you might need to check the API documentation for any specific formatting requirements for song titles. Some APIs prefer artist and title separated, like ‘artist - title’.
Regarding the 403 error, it’s likely an authentication issue. Ensure your API key is correct and hasn’t expired. Also, check if you’ve exceeded your daily request limit. Many free tiers have strict caps.
Lastly, consider implementing error handling and retries in your code to make it more robust against temporary API issues.