Trouble using variables with music lyric API in Discord bot

Hey everyone, I’m working on a Discord bot that plays music. I want to add a feature where it shows lyrics for the current song. I’m using a lyric API, but I’m having some issues.

My bot can play songs fine, and I can get the current song name. But when I try to use that name to search for lyrics, it’s not working. Here’s what I’ve tried:

let nowPlaying = getCurrentSong();
let lyricSearch = await lyricAPI.findLyrics(nowPlaying);

if (lyricSearch.length > 0) {
  let songInfo = lyricSearch[0];
  console.log('Song details:', songInfo);

  let songLyrics = await songInfo.getLyrics();
  console.log('Lyrics:', songLyrics);
}

This code works if I replace nowPlaying with a hardcoded song title. But it fails when I use the variable. I’ve checked, and nowPlaying definitely contains the song name.

Also, I’m getting a ‘403 Forbidden’ error sometimes. Any ideas what might be causing these issues? Thanks for any help!

I’ve dealt with similar API quirks in my Discord bot projects. One thing to check is the exact format of nowPlaying - the API might be picky about things like capitalization or extra spaces. You could try trimming whitespace and converting to lowercase before passing it to the API.

For the 403 errors, I’d suggest implementing some rate limiting on your requests. Most lyric APIs have usage limits, so spacing out calls with a small delay can help avoid getting blocked.

Also, make sure your API key is valid and you’re not accidentally exposing it in your code. I once spent hours debugging only to realize my key had expired!

Logging the full request URL and response headers can provide valuable clues too. Good luck with your project!

hav u tried url encoding the nowPlaying variable? sometimes APIs get finicky with spaces n stuff. for the 403 error, maybe ur hitting rate limits? try adding a lil delay between requests. also double check ur using the right API key. good luck with ur bot dude!

I have experienced similar issues with music APIs when working on Discord bots. It is possible that problems arise from differences in how the song title is formatted or encoded, so you might want to ensure that the variable is sanitized, perhaps using functions like encodeURIComponent() to handle spaces and special characters. Additionally, receiving a 403 Forbidden error can indicate an authentication issue or an exceeded request limit. Implementing error handling with try/catch blocks and logging the exact request string may help diagnose the discrepancy between a hardcoded title and the variable usage.