(sock, message, args, context)
| 13 | description: 'Download music from Spotify', |
| 14 | usage: '.spotify <spotify-url>', |
| 15 | async handler(sock, message, args, context) { |
| 16 | const chatId = context.chatId || message.key.remoteJid; |
| 17 | const url = args.join(' ').trim(); |
| 18 | if (!url || !url.includes('spotify.com')) { |
| 19 | return sock.sendMessage(chatId, { |
| 20 | text: '🎵 *Spotify Downloader*\n\nUsage: `.spotify <spotify track url>`\nExample: `.spotify https://open.spotify.com/track/4LMlVCXHJtCE9abhmn0mYo`' |
| 21 | }, { quoted: message }); |
| 22 | } |
| 23 | try { |
| 24 | await sock.sendMessage(chatId, { react: { text: '🎵', key: message.key } }); |
| 25 | const { data } = await axios.get(API, { |
| 26 | params: { apiKey: API_KEY, url }, |
| 27 | timeout: 30000 |
| 28 | }); |
| 29 | if (!data?.success || !data?.data) { |
| 30 | throw new Error('Invalid API response'); |
| 31 | } |
| 32 | const track = data.data; |
| 33 | if (!track.download) { |
| 34 | return sock.sendMessage(chatId, { |
| 35 | text: '❌ No downloadable audio found for this track.' |
| 36 | }, { quoted: message }); |
| 37 | } |
| 38 | const caption = [ |
| 39 | `🎵 *${track.title || 'Unknown Title'}*`, |
| 40 | track.artist ? `👤 ${track.artist}` : '', |
| 41 | track.duration ? `⏱ ${formatDuration(track.duration)}` : '', |
| 42 | track.format ? `🎧 Format: ${track.format.toUpperCase()}` : '' |
| 43 | ].filter(Boolean).join('\n'); |
| 44 | if (track.cover) { |
| 45 | await sock.sendMessage(chatId, { |
| 46 | image: { url: track.cover }, |
| 47 | caption |
| 48 | }, { quoted: message }); |
| 49 | } |
| 50 | else if (caption) { |
| 51 | await sock.sendMessage(chatId, { text: caption }, { quoted: message }); |
| 52 | } |
| 53 | await sock.sendMessage(chatId, { |
| 54 | audio: { url: track.download }, |
| 55 | mimetype: 'audio/mpeg', |
| 56 | fileName: `${(track.title || 'track').replace(/[\\/:*?"<>|]/g, '')}.mp3` |
| 57 | }, { quoted: message }); |
| 58 | } |
| 59 | catch (error) { |
| 60 | console.error('[SPOTIFY] error:', error.message); |
| 61 | await sock.sendMessage(chatId, { |
| 62 | text: '❌ Failed to download track. Please check the URL and try again.' |
| 63 | }, { quoted: message }); |
| 64 | } |
| 65 | } |
| 66 | }; |
nothing calls this directly
no test coverage detected