// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const yts = require("yt-search"); const fs = require("fs-extra"); const path = require("path"); const { pipeline } = require("stream/promises"); const { Transform } = require("stream"); module.exports = {   config: {     name: "mili",     version: "1.0.0",     author: "ChatGPT",     aliases: ["mili","mimu", "মিলি", "মিমু"],     role: 0,     countDown: 3,     category: "ai",     description: "Mili AI Chat + Song/Video Downloader",     guide: {       en: "{pn} \n{pn} song \n{pn} video "     }   },   chatMemory: {},   AI_API: "https://uzairrajputapis.qzz.io/api/ai/gemini",   SONG_API: "https://yt-song-api.vercel.app/api/song",   VIDEO_API: "https://video-dl-api-tan.vercel.app",   MAX_FILE_SIZE: 25 * 1024 * 1024,   async deleteFile(file) {     try {       if (file && fs.existsSync(file)) {         await fs.unlink(file);       }     } catch {}   },   async searchYoutube(query) {     try {       const result = await yts(query);       if (!result.videos || !result.videos.length) {         return null;       }       const video = result.videos[0];       return {         title: video.title,         url: video.url,         videoId: video.videoId       };     } catch {       return null;     }   },   sizeGuard(maxSize) {     let size = 0;     return new Transform({       transform(chunk, enc, callback) {         size += chunk.length;         if (size > maxSize) {           const error = new Error("FILE_TOO_LARGE");           error.code = "FILE_TOO_LARGE";           return callback(error);         }         callback(null, chunk);       }     });   },   async downloadSong(api, event, query) {     const { threadID, messageID, senderID } = event;     const cache = path.join(__dirname, "cache");     await fs.ensureDir(cache);     let file = null;     try {       api.setMessageReaction(         "⏳",         messageID,         () => {},         true       );       const response = await axios.get(         this.SONG_API,         {           params: {             q: query           },           timeout: 60000         }       );       const data = response.data;       const audio =         data?.download ||         data?.audio ||         data?.audio_url ||         data?.url;       if (!audio) {         throw new Error("NO_AUDIO_URL");       }       const ext =         String(data?.format || "mp3")           .replace(/[^a-z0-9]/gi, "") ||         "mp3";       file = path.join(         cache,         `mili_${senderID}_${Date.now()}.${ext}`       );       const stream = await axios.get(         audio,         {           responseType: "stream",           timeout: 120000         }       );       await pipeline(         stream.data,         this.sizeGuard(this.MAX_FILE_SIZE),         fs.createWriteStream(file)       );       api.setMessageReaction(         "✅",         messageID,         () => {},         true       );       return api.sendMessage(         {           body:             "╭━━〔 🎵 MILI MUSIC 〕━━╮\n" +             `┃ 🎶 ${data?.title || query}\n` +             "┃\n" +             "┃ ❤️ তোমার গান চলে এসেছে!\n" +             "╰━━━━━━━━━━━━━━━━━━╯",           attachment: fs.createReadStream(file)         },         threadID,         async () => {           await this.deleteFile(file);         },         messageID       );     } catch (error) {       console.log(         "[MILI SONG ERROR]",         error.message       );       await this.deleteFile(file);       api.setMessageReaction(         "❌",         messageID,         () => {},         true       );       return api.sendMessage(         "╭━━〔 ❌ SONG ERROR 〕━━╮\n" +         "┃ গানটি ডাউনলোড করা যায়নি।\n" +         "┃\n" +         "┃ 🔄 অন্য গানের নাম দিয়ে চেষ্টা করুন।\n" +         "╰━━━━━━━━━━━━━━━━━━╯",         threadID,         messageID       );     }   },   async downloadVideo(api, event, query) {     const { threadID, messageID, senderID } = event;     const cache = path.join(__dirname, "cache");     await fs.ensureDir(cache);     let file = null;     try {       api.setMessageReaction(         "⏳",         messageID,         () => {},         true       );       const video = await this.searchYoutube(query);       if (!video) {         throw new Error("VIDEO_NOT_FOUND");       }       file = path.join(         cache,         `mili_${senderID}_${Date.now()}.mp4`       );       const url =         `${this.VIDEO_API}/stream?url=` +         `${encodeURIComponent(video.url)}` +         `&type=video&quality=720`;       const response = await axios.get(         url,         {           responseType: "stream",           timeout: 120000         }       );       await pipeline(         response.data,         this.sizeGuard(this.MAX_FILE_SIZE),         fs.createWriteStream(file)       );       api.setMessageReaction(         "✅",         messageID,         () => {},         true       );       return api.sendMessage(         {           body:             "╭━━〔 🎬 MILI VIDEO 〕━━╮\n" +             `┃ 🎥 ${video.title}\n` +             "┃\n" +             "┃ ❤️ ভিডিওটি চলে এসেছে!\n" +             "╰━━━━━━━━━━━━━━━━━━╯",           attachment: fs.createReadStream(file)         },         threadID,         async () => {           await this.deleteFile(file);         },         messageID       );     } catch (error) {       console.log(         "[MILI VIDEO ERROR]",         error.message       );       await this.deleteFile(file);       api.setMessageReaction(         "❌",         messageID,         () => {},         true       );       return api.sendMessage(         "╭━━〔 ❌ VIDEO ERROR 〕━━╮\n" +         "┃ ভিডিওটি ডাউনলোড করা যায়নি।\n" +         "┃\n" +         "┃ 🔄 অন্য ভিডিওর নাম দিয়ে চেষ্টা করুন।\n" +         "╰━━━━━━━━━━━━━━━━━━╯",         threadID,         messageID       );     }   },   async aiChat(api, event, text) {     const { threadID, messageID } = event;     if (!this.chatMemory[threadID]) {       this.chatMemory[threadID] = [];     }     this.chatMemory[threadID].push(       `User: ${text}`     );     if (this.chatMemory[threadID].length > 8) {       this.chatMemory[threadID].shift();     }     const prompt =       `তুমি Mili নামে একজন বন্ধুসুলভ বাংলা চ্যাট সহকারী।\n` +       `সহজ ও স্বাভাবিক বাংলায় উত্তর দাও।\n` +       `উত্তর ছোট এবং সুন্দর রাখো।\n` +       `প্রয়োজনে ১-২টি emoji ব্যবহার করো।\n\n` +       `Previous chat:\n` +       `${this.chatMemory[threadID].join("\n")}\n\n` +       `Mili:`;     try {       const response = await axios.post(         this.AI_API,         {           prompt: prompt         },         {           timeout: 30000         }       );       const data = response.data;       let reply =         data?.result?.answer ||         data?.answer ||         data?.reply ||         data?.response ||         data?.text;       if (!reply) {         reply = "হুম বলো তো 😊";       }       reply = String(reply).trim();       this.chatMemory[threadID].push(         `Mili: ${reply}`       );       return api.sendMessage(         reply,         threadID,         (err, info) => {           if (             !err &&             info &&             global.GoatBot &&             global.GoatBot.onReply           ) {             global.GoatBot.onReply.set(               info.messageID,               {                 commandName: this.config.name,                 author: event.senderID,                 messageID: info.messageID               }             );           }         },         messageID       );     } catch (error) {       console.log(         "[MILI AI ERROR]",         error.message       );       return api.sendMessage(         "নেটওয়ার্কে একটু সমস্যা হচ্ছে 🥺\nকিছুক্ষণ পরে আবার বলো।",         threadID,         messageID       );     }   },   async handleMessage(api, event, text, message) {     const input = String(text || "").trim();     if (!input) {       return message.reply(         "হুম বলো তো 😊"       );     }     const lower = input.toLowerCase();     const videoMode =       /\b(video|vdo|mp4)\b/i.test(lower) ||       /ভিডিও/.test(input);     const songMode =       /\b(song|music|audio|mp3|play)\b/i.test(lower) ||       /গান/.test(input);     let query = input       .replace(         /\b(video|vdo|mp4|song|music|audio|mp3|play)\b/gi,         ""       )       .replace(/ভিডিও|গান/gi, "")       .trim();     if (videoMode) {       if (!query) {         return message.reply(           "🎬 ভিডিওর নামটা বলো।"         );       }       return this.downloadVideo(         api,         event,         query       );     }     if (songMode) {       if (!query) {         return message.reply(           "🎵 গানের নামটা বলো।"         );       }       return this.downloadSong(         api,         event,         query       );     }     return this.aiChat(       api,       event,       input     );   },   async onStart({     api,     event,     args,     message   }) {     return this.handleMessage(       api,       event,       args.join(" "),       message     );   },   async onChat({     api,     event,     message   }) {     const body =       String(event.body || "").trim();     if (!body) return;     const lower = body.toLowerCase();     const triggered =       lower.includes("mili") ||       lower.includes("mimu") ||       body.includes("মিলি") ||       body.includes("মিমু");     if (!triggered) return;     return this.handleMessage(       api,       event,       body,       message     );   },   async onReply({     api,     event,     message,     Reply   }) {     if (       !Reply ||       event.senderID !== Reply.author     ) {       return;     }     const text =       String(event.body || "").trim();     if (!text) return;     return this.handleMessage(       api,       event,       text,       message     );   } };