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

Function handler

plugins/play.js:32–77  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

30 description: 'Search and download a song as MP3 from YouTube',
31 usage: '.play <song name>',
32 async handler(sock, message, args, context) {
33 const chatId = context.chatId || message.key.remoteJid;
34 const query = args.join(' ').trim();
35 if (!query)
36 return sock.sendMessage(chatId, { text: '*Which song do you want to play?*\nUsage: .play <song name>' }, { quoted: message });
37 try {
38 await sock.sendMessage(chatId, { text: '🔍 *Searching...*' }, { quoted: message });
39 const { videos } = await yts(query);
40 if (!videos?.length)
41 return sock.sendMessage(chatId, { text: '❌ *No results found!*' }, { quoted: message });
42 const video = videos[0];
43 await sock.sendMessage(chatId, {
44 text: `✅ *Found:* ${video.title}\n⏱️ ${video.timestamp}\n👤 ${video.author.name}\n\n⏳ *Downloading... (this may take up to 30s)*`
45 }, { quoted: message });
46 const songData = await downloadWithRetry(video.url);
47 let thumbnailBuffer;
48 try {
49 const img = await axios.get(songData.thumbnail, { responseType: 'arraybuffer', timeout: 15000 });
50 thumbnailBuffer = Buffer.from(img.data);
51 }
52 catch { /* no thumbnail */ }
53 await sock.sendMessage(chatId, {
54 audio: { url: songData.downloadUrl },
55 mimetype: 'audio/mpeg',
56 fileName: `${songData.title}.mp3`,
57 contextInfo: {
58 externalAdReply: {
59 title: songData.title,
60 body: `${video.author.name} • ${video.timestamp}`,
61 thumbnail: thumbnailBuffer,
62 mediaType: 2,
63 sourceUrl: video.url
64 }
65 }
66 }, { quoted: message });
67 }
68 catch (err) {
69 console.error('Play error:', err.message);
70 const reason = err.response?.status === 408
71 ? 'Download timed out. Try again in a moment.'
72 : err.response?.status === 429
73 ? 'Rate limited. Wait a minute.'
74 : err.message;
75 await sock.sendMessage(chatId, { text: `❌ *Failed:* ${reason}` }, { quoted: message });
76 }
77 }
78};

Callers

nothing calls this directly

Calls 1

downloadWithRetryFunction · 0.70

Tested by

no test coverage detected