(sock, chatId, songTitle, message)
| 1 | const fetch = require('node-fetch'); |
| 2 | |
| 3 | async function lyricsCommand(sock, chatId, songTitle, message) { |
| 4 | if (!songTitle) { |
| 5 | await sock.sendMessage(chatId, { |
| 6 | text: '🔍 Please enter the song name to get the lyrics! Usage: *lyrics <song name>*' |
| 7 | },{ quoted: message }); |
| 8 | return; |
| 9 | } |
| 10 | |
| 11 | try { |
| 12 | // Use lyricsapi.fly.dev and return only the raw lyrics text |
| 13 | const apiUrl = `https://lyricsapi.fly.dev/api/lyrics?q=${encodeURIComponent(songTitle)}`; |
| 14 | const res = await fetch(apiUrl); |
| 15 | |
| 16 | if (!res.ok) { |
| 17 | const errText = await res.text(); |
| 18 | throw errText; |
| 19 | } |
| 20 | |
| 21 | const data = await res.json(); |
| 22 | |
| 23 | const lyrics = data && data.result && data.result.lyrics ? data.result.lyrics : null; |
| 24 | if (!lyrics) { |
| 25 | await sock.sendMessage(chatId, { |
| 26 | text: `❌ Sorry, I couldn't find any lyrics for "${songTitle}".` |
| 27 | },{ quoted: message }); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const maxChars = 4096; |
| 32 | const output = lyrics.length > maxChars ? lyrics.slice(0, maxChars - 3) + '...' : lyrics; |
| 33 | |
| 34 | await sock.sendMessage(chatId, { text: output }, { quoted: message }); |
| 35 | } catch (error) { |
| 36 | console.error('Error in lyrics command:', error); |
| 37 | await sock.sendMessage(chatId, { |
| 38 | text: `❌ An error occurred while fetching the lyrics for "${songTitle}".` |
| 39 | },{ quoted: message }); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | module.exports = { lyricsCommand }; |
nothing calls this directly
no outgoing calls
no test coverage detected