Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Module } = require('../main');
- const config = require('../config');
- const axios = require('axios');
- const fs = require('fs');
- const path = require('path');
- Module({
- pattern: 'music ?(.*)',
- fromMe: config.MODE !== 'public',
- desc: 'Search a song and download as MP3',
- type: 'downloader'
- }, async (message, match) => {
- const query = match[1];
- if (!query) return await message.sendReply('_Please provide a song name!_');
- try {
- // Step 1: Search
- const searchRes = await axios.get(`https://jerrycoder.oggyapi.workers.dev/yt-search?q=${encodeURIComponent(query)}`);
- if (!searchRes.data.status || !searchRes.data.result.length) {
- return await message.sendReply('_No results found!_');
- }
- const song = searchRes.data.result[0];
- // Step 2: Send "Found & Downloading" message
- const downloadMsg = await message.client.sendMessage(message.jid, {
- text: `_Found:_ *${song.title}*\n_Downloading..._`
- });
- // Step 3: Download MP3
- const dlRes = await axios.get(`https://jerrycoder.oggyapi.workers.dev/ytmp3?url=${encodeURIComponent(song.link)}`);
- if (!dlRes.data.status || !dlRes.data.url) {
- return await message.sendReply('_Failed to download audio!_');
- }
- const mp3Url = dlRes.data.url;
- const tempFile = path.join(__dirname, `${Date.now()}.mp3`);
- const writer = fs.createWriteStream(tempFile);
- const audioStream = await axios.get(mp3Url, { responseType: 'stream' });
- audioStream.data.pipe(writer);
- await new Promise((resolve, reject) => {
- writer.on('finish', resolve);
- writer.on('error', reject);
- });
- // Step 4: Send MP3
- await message.client.sendMessage(message.jid, {
- audio: { stream: fs.createReadStream(tempFile) },
- mimetype: 'audio/mp4',
- fileName: `${song.title}.mp3`
- });
- fs.unlinkSync(tempFile);
- // Step 5: Delete the "Downloading" message
- await message.client.sendMessage(message.jid, { delete: downloadMsg.key });
- } catch (err) {
- console.error(err);
- await message.sendReply('_Something went wrong while fetching the song!_');
- }
- });
Advertisement