MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / handler

Function handler

plugins/spotify.ts:20–78  ·  view source on GitHub ↗
(sock: any, message: any, args: string[], context: BotContext)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

formatDurationFunction · 0.85

Tested by

no test coverage detected