JerryCoderr

sparky-musc.js

Feb 15th, 2026 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Sparky } = require("../lib");
  2. const axios = require("axios");
  3. const fs = require("fs");
  4. const path = require("path");
  5.  
  6. Sparky({
  7.   name: "music",
  8.   fromMe: false,
  9.   category: "downloader",
  10.   desc: "Search a song or download from YouTube URL"
  11. }, async ({ m, client, args }) => {
  12.  
  13.   args = args || m.quoted?.text;
  14.   if (!args) return await m.reply("_Please provide a song name or YouTube URL!_");
  15.  
  16.   let tempFile;
  17.   let statusMsg;
  18.  
  19.   try {
  20.     const query = args.trim();
  21.     const ytUrlRegex = /(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/[^\s]+/;
  22.     const isUrl = ytUrlRegex.test(query);
  23.  
  24.     statusMsg = await m.sendMsg(m.jid, `🔍 Searching *${query}* ...`, { quoted: m });
  25.     await m.react("🔍");
  26.  
  27.     let songTitle = "";
  28.     let mp3Url = "";
  29.  
  30.     // ================= URL MODE =================
  31.     if (isUrl) {
  32.       const { data } = await axios.get(
  33.         `https://rembg-phi.vercel.app/api/edit?url=${encodeURIComponent(query)}`
  34.       );
  35.  
  36.       if (!data.success || !data.url) {
  37.         await m.react("❌");
  38.         return await m.sendMsg(
  39.           m.jid,
  40.           "_Failed to download audio from URL!_",
  41.           { edit: statusMsg.key }
  42.         );
  43.       }
  44.  
  45.       songTitle = data.title;
  46.       mp3Url = data.url;
  47.  
  48.     // ================= SEARCH MODE =================
  49.     } else {
  50.       const searchRes = await axios.get(
  51.         `https://jerrycoder.oggyapi.workers.dev/yt-search?q=${encodeURIComponent(query)}`
  52.       );
  53.  
  54.       if (!searchRes.data.status || !searchRes.data.result?.length) {
  55.         await m.react("❌");
  56.         return await m.sendMsg(m.jid, "_No results found!_", { edit: statusMsg.key });
  57.       }
  58.  
  59.       const song = searchRes.data.result[0];
  60.       songTitle = song.title;
  61.  
  62.       const { data } = await axios.get(
  63.         `https://rembg-phi.vercel.app/api/edit?url=${encodeURIComponent(song.link)}`
  64.       );
  65.  
  66.       if (!data.success || !data.url) {
  67.         await m.react("❌");
  68.         return await m.sendMsg(
  69.           m.jid,
  70.           "_Failed to download audio from search result!_",
  71.           { edit: statusMsg.key }
  72.         );
  73.       }
  74.  
  75.       mp3Url = data.url;
  76.     }
  77.  
  78.     // ================= DOWNLOAD =================
  79.     await m.sendMsg(m.jid, `⬇️ Downloading: *${songTitle}*`, { edit: statusMsg.key });
  80.     await m.react("⬇️");
  81.  
  82.     tempFile = path.join(__dirname, `${Date.now()}.mp3`);
  83.     const writer = fs.createWriteStream(tempFile);
  84.  
  85.     const audioStream = await axios.get(mp3Url, {
  86.       responseType: "stream",
  87.       headers: {
  88.         "User-Agent": "Mozilla/5.0"
  89.       }
  90.     });
  91.  
  92.     audioStream.data.pipe(writer);
  93.  
  94.     await new Promise((resolve, reject) => {
  95.       writer.on("finish", resolve);
  96.       writer.on("error", reject);
  97.     });
  98.  
  99.     // ================= SEND AUDIO =================
  100.     await m.sendMsg(
  101.       m.jid,
  102.       fs.readFileSync(tempFile),
  103.       {
  104.         mimetype: "audio/mpeg",
  105.         fileName: `${songTitle}.mp3`,
  106.         quoted: m
  107.       },
  108.       "audio"
  109.     );
  110.  
  111.     await m.sendMsg(m.jid, `✅ *${songTitle}* sent successfully!`, {
  112.       edit: statusMsg.key
  113.     });
  114.     await m.react("✅");
  115.  
  116.   } catch (err) {
  117.     console.error("Music Command Error:", err);
  118.     await m.react("❌");
  119.     if (statusMsg) {
  120.       await m.sendMsg(m.jid, `_Error:_ ${err.message}`, { edit: statusMsg.key });
  121.     } else {
  122.       await m.reply(`_Error:_ ${err.message}`);
  123.     }
  124.   } finally {
  125.     if (tempFile && fs.existsSync(tempFile)) fs.unlinkSync(tempFile);
  126.   }
  127. });
  128.  
Advertisement
Add Comment
Please, Sign In to add comment