JerryCoderr

stick-audio.js

Jul 30th, 2026 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Module } = require("../main");
  2. const fs = require("fs");
  3. const ffmpeg = require("fluent-ffmpeg");
  4. const {
  5.   bass,
  6.   sticker,
  7.   addExif,
  8.   attp,
  9.   gtts,
  10.   gis,
  11.   aiTTS,
  12.   getBuffer,
  13. } = require("./utils");
  14. const config = require("../config");
  15. const axios = require("axios");
  16. const fileType = require("file-type");
  17. const { getTempPath, getTempSubdir } = require("../core/helpers");
  18.  
  19. const getFileType = async (buffer) => {
  20.   try {
  21.     if (fileType.fileTypeFromBuffer) {
  22.       return await fileType.fileTypeFromBuffer(buffer);
  23.     }
  24.  
  25.     if (fileType.fromBuffer) {
  26.       return await fileType.fromBuffer(buffer);
  27.     }
  28.  
  29.     return await fileType(buffer);
  30.   } catch (error) {
  31.     console.log("file-type detection failed:", error);
  32.     return null;
  33.   }
  34. };
  35. let MODE = config.MODE,
  36.   STICKER_DATA = config.STICKER_DATA;
  37. const { getString } = require("./utils/lang");
  38. const Lang = getString("converters");
  39.  
  40. Module(
  41.   {
  42.     pattern: "look ?(.*)",
  43.     fromMe: false,
  44.     use: "search",
  45.     desc: "Searches for an image on Google Images and sends the requested number of results.",
  46.   },
  47.   async (message, match) => {
  48.     if (!match[1]) return await message.send("*_Need a search term!_*");
  49.     let splitInput = match[1].split(",");
  50.     let count = parseInt(splitInput[1] || 5);
  51.     await message.send(`*_Searching for ${count} images..._*`);
  52.  
  53.     const buffer = Math.ceil(count * 0.5);
  54.     let results = await gis(splitInput[0], count + buffer);
  55.     if (results.length < 1) return await message.send("*_No results found!_*");
  56.  
  57.     // buffer and send with success tracking since many URLs have access issues
  58.     let successCount = 0;
  59.     let i = 0;
  60.     const imagesToSend = [];
  61.  
  62.     while (successCount < count && i < results.length) {
  63.       try {
  64.         const imageBuffer = await getBuffer(results[i]);
  65.         imagesToSend.push({ image: imageBuffer });
  66.         successCount++;
  67.       } catch (e) {
  68.         console.log(`Failed to buffer image ${i + 1}:`, e.message);
  69.         if (i === results.length - 1 && successCount < count) {
  70.           let moreResults = await gis(splitInput[0], buffer, {
  71.             page: Math.floor(i / 10) + 1,
  72.           });
  73.           if (moreResults.length > 0) {
  74.             results = results.concat(moreResults);
  75.           }
  76.         }
  77.       }
  78.       i++;
  79.     }
  80.  
  81.     if (imagesToSend.length === 0) {
  82.       return await message.send("*_Failed to download any images_*");
  83.     }
  84.  
  85.     try {
  86.       await message.client.albumMessage(
  87.         message.jid,
  88.         imagesToSend,
  89.         message.data
  90.       );
  91.     } catch (e) {
  92.       console.log("Album send failed:", e.message);
  93.       for (const img of imagesToSend) {
  94.         try {
  95.           await message.sendMessage(img, "image", { quoted: message.data });
  96.         } catch (sendErr) {
  97.           console.log("Failed to send individual image:", sendErr.message);
  98.         }
  99.       }
  100.     }
  101.  
  102.     if (successCount < count) {
  103.       await message.send(
  104.         `*_Only able to download ${successCount}/${count} images. Some URLs had access issues._*`
  105.       );
  106.     }
  107.   }
  108. );
  109.  
  110. Module(
  111.   {
  112.     pattern: "stc ?(.*)",
  113.     fromMe: false,
  114.     use: "edit",
  115.     desc: Lang.STICKER_DESC,
  116.   },
  117.   async (message, match) => {
  118.     if (match[1] && match[1].trim() !== "") {
  119.       var result = await attp(match[1].trim());
  120.       var exif = {
  121.         author: STICKER_DATA.split(";")[1] || "",
  122.         packname: message.senderName,
  123.         categories: STICKER_DATA.split(";")[2] || "๐Ÿ˜‚",
  124.         android: "https://github.com/souravkl11/Raganork-md/",
  125.         ios: "https://github.com/souravkl11/Raganork-md/",
  126.       };
  127.       return await message.sendMessage(
  128.         fs.readFileSync(await addExif(result, exif)),
  129.         "sticker"
  130.       );
  131.     }
  132.  
  133.     if (message.reply_message === false)
  134.       return await message.send(Lang.STICKER_NEED_REPLY);
  135.  
  136.     var exif = {
  137.       author: STICKER_DATA.split(";")[1] || "",
  138.       packname: message.senderName,
  139.       categories: STICKER_DATA.split(";")[2] || "๐Ÿ˜‚",
  140.       android: "https://github.com/souravkl11/Raganork-md/",
  141.       ios: "https://github.com/souravkl11/Raganork-md/",
  142.     };
  143.  
  144.     // handle album
  145.     if (message.reply_message.album) {
  146.       const albumData = await message.reply_message.download();
  147.       const allFiles = [...(albumData.images || []), ...(albumData.videos || [])];
  148.       if (allFiles.length === 0) return await message.send("_No media in album_");
  149.  
  150.       await message.send(`_Converting ${allFiles.length} stickers..._`);
  151.       for (const file of allFiles) {
  152.         try {
  153.           const isVideo = albumData.videos?.includes(file);
  154.           const stickerFile = fs.readFileSync(
  155.             await addExif(
  156.               await sticker(file, isVideo ? "video" : "image"),
  157.               exif
  158.             )
  159.           );
  160.           await message.sendMessage(stickerFile, "sticker", {
  161.             quoted: message.quoted,
  162.           });
  163.         } catch (err) {
  164.           console.error("Failed to convert album sticker:", err);
  165.         }
  166.       }
  167.       return;
  168.     }
  169.  
  170.     var savedFile = await message.reply_message.download();
  171.     if (message.reply_message.image === true) {
  172.       return await message.sendMessage(
  173.         fs.readFileSync(await addExif(await sticker(savedFile), exif)),
  174.         "sticker",
  175.         { quoted: message.quoted }
  176.       );
  177.     } else {
  178.       return await message.sendMessage(
  179.         fs.readFileSync(await addExif(await sticker(savedFile, "video"), exif)),
  180.         "sticker",
  181.         { quoted: message.quoted }
  182.       );
  183.     }
  184.   }
  185. );
  186. Module(
  187.   {
  188.     pattern: "audio ?(.*)",
  189.     fromMe: false,
  190.     use: "edit",
  191.     desc: Lang.MP3_DESC,
  192.   },
  193.   async (message) => {
  194.     if (
  195.       !message.reply_message ||
  196.       (!message.reply_message.video &&
  197.         !message.reply_message.audio &&
  198.         !message.reply_message.document &&
  199.         !message.reply_message.album)
  200.     )
  201.       return await message.sendReply(Lang.MP3_NEED_REPLY);
  202.  
  203.     // handle album
  204.     if (message.reply_message.album) {
  205.       const albumData = await message.reply_message.download();
  206.       const videoFiles = albumData.videos || [];
  207.      
  208.       if (videoFiles.length === 0) {
  209.         return await message.send("_No video files in album. MP3 requires video/audio files._");
  210.       }
  211.  
  212.       await message.send(`_Converting ${videoFiles.length} files to mp3..._`);
  213.       for (let i = 0; i < videoFiles.length; i++) {
  214.         try {
  215.           const file = videoFiles[i];
  216.           const outputPath = getTempPath(`album_${i}.mp3`);
  217.           await new Promise((resolve, reject) => {
  218.             ffmpeg(file)
  219.               .save(outputPath)
  220.               .on("end", resolve)
  221.               .on("error", reject);
  222.           });
  223.           await message.sendMessage(
  224.             fs.readFileSync(outputPath),
  225.             "audio",
  226.             { quoted: message.quoted }
  227.           );
  228.         } catch (err) {
  229.           console.error("Failed to convert album mp3:", err);
  230.         }
  231.       }
  232.       return;
  233.     }
  234.  
  235.     let savedFile = await message.reply_message.download();
  236.     const audioPath = getTempPath(`audio_${Date.now()}.m4a`);
  237.    
  238.     ffmpeg(savedFile)
  239.       .audioCodec("aac")
  240.       .outputOptions(["-movflags", "faststart", "-q:a", "100"])
  241.       .noVideo()
  242.       .output(audioPath)
  243.       .on("end", async () => {
  244.         try {
  245.           const stream = fs.createReadStream(audioPath);
  246.           await message.sendReply({ stream }, "audio", {
  247.             mimetype: "audio/mp4",
  248.           });
  249.           stream.destroy();
  250.  
  251.           await new Promise((resolve) => setTimeout(resolve, 100));
  252.           if (fs.existsSync(audioPath)) {
  253.             fs.unlinkSync(audioPath);
  254.           }
  255.         } catch (err) {
  256.           console.error("Error sending audio:", err);
  257.         }
  258.       })
  259.       .on("error", (err) => {
  260.         console.error("FFmpeg error:", err);
  261.         if (fs.existsSync(audioPath)) fs.unlinkSync(audioPath);
  262.       })
  263.       .run();
  264.   }
  265. );
  266.  
  267. Module(
  268.   {
  269.     pattern: "tke ?(.*)",
  270.     use: "edit",
  271.     fromMe: false,
  272.     desc: "Changes sticker/audio pack & author name. Title, artist, thumbnail etc.",
  273.   },
  274.   async (m, match) => {
  275.     if (!m.reply_message)
  276.       return await m.sendMessage("_Reply to an audio or a sticker_");
  277.     var audiomsg = m.reply_message.audio;
  278.     var stickermsg = m.reply_message.sticker;
  279.     var q = await m.reply_message.download();
  280.     if (stickermsg) {
  281.       if (match[1] !== "") {
  282.         var exif = {
  283.           author: match[1].includes(";") ? match[1].split(";")[1] : "",
  284.           packname: match[1].includes(";") ? match[1].split(";")[0] : match[1],
  285.           categories: config.STICKER_DATA.split(";")[2] || "๐Ÿ˜‚",
  286.           android: "https://github.com/souravkl11/raganork-md/",
  287.           ios: "https://github.com/souravkl11/raganork-md/",
  288.         };
  289.       } else {
  290.         var exif = {
  291.           author: config.STICKER_DATA.split(";")[1] || "",
  292.           packname: config.STICKER_DATA.split(";")[0] || "",
  293.           categories: config.STICKER_DATA.split(";")[2] || "๐Ÿ˜‚",
  294.           android: "https://github.com/souravkl11/raganork-md/",
  295.           ios: "https://github.com/souravkl11/raganork-md/",
  296.         };
  297.       }
  298.       return await m.client.sendMessage(
  299.         m.jid,
  300.         { sticker: fs.readFileSync(await addExif(q, exif)) },
  301.         { quoted: m.quoted }
  302.       );
  303.     }
  304.     if (!stickermsg && audiomsg) {
  305.       let inf =
  306.         match[1] !== ""
  307.           ? match[1]
  308.           : config.AUDIO_DATA === "default"
  309.           ? "Ryzn- Audio title here;Raganork - Artist;https://i.ibb.co/s98DyMMq/NL-1.png"
  310.           : config.AUDIO_DATA;
  311.       if (config.AUDIO_DATA == "default") {
  312.         await m.sendReply(
  313.           `_Using default audio metadata, use .setvar AUDIO_INFO=title;artist;imageurl to change_`
  314.         );
  315.       }
  316.       let spl = inf.split(";"),
  317.         image = spl[2]
  318.           ? await getBuffer(spl[2])
  319.           : await getBuffer(
  320.               config.BOT_INFO.split(";")?.[3] === "default"
  321.                 ? "https://i.ibb.co/s98DyMMq/NL-1.png"
  322.                 : config.BOT_INFO.split(";")[3]
  323.             ),
  324.         res = await addID3(
  325.           q,
  326.           spl[0],
  327.           spl[1] ? spl[1] : config.AUDIO_DATA.split(";")[1],
  328.           "Raganork Engine",
  329.           image
  330.         );
  331.       await m.client.sendMessage(
  332.         m.jid,
  333.         {
  334.           audio: res,
  335.           mimetype: "audio/mp4",
  336.         },
  337.         {
  338.           quoted: m.quoted,
  339.           ptt: false,
  340.         }
  341.       );
  342.     }
  343.     if (!audiomsg && !stickermsg)
  344.       return await m.client.sendMessage(
  345.         m.jid,
  346.         {
  347.           text: "_Reply to an audio or a sticker_",
  348.         },
  349.         {
  350.           quoted: m.data,
  351.         }
  352.       );
  353.   }
  354. );
Advertisement
Add Comment
Please, Sign In to add comment