diff --git a/lib/db.js b/lib/db.js index 32dfe3e..c42e897 100644 --- a/lib/db.js +++ b/lib/db.js @@ -350,7 +350,14 @@ const data = { REPLY: "```Obtaining the recommendations...```", NO_VIDEOS: "```No videos could be found.```", ENTER_INPUT: "```Please enter the query you want to search for. Use the``` *.help yt* ```command for more info.```" - } + }, + vid: { + DESCRIPTION: "Download youtube videos", + EXTENDED_DESCRIPTION: "```Get any youtube video in mp4 format by using this command with the link of the video that you want to download.```", + REPLY: "```Downloading your video...```", + INVALID_LINK: "```No video found at the given link!```", + ENTER_INPUT: "```Please give the link of the video you want to download. Use the``` *.help vid* ```command for more info.```" + } }; module.exports = data; diff --git a/modules/vid.js b/modules/vid.js new file mode 100644 index 0000000..ef6d5b1 --- /dev/null +++ b/modules/vid.js @@ -0,0 +1,74 @@ +const chalk = require("chalk"); +const { MessageType, Mimetype } = require("@adiwajshing/baileys"); +const inputSanitization = require("../sidekick/input-sanitization"); +const fs = require("fs"); +const ytdl = require("ytdl-core"); +const Strings = require("../lib/db"); +const VID = Strings.vid; + +module.exports = { + name: "vid", + description: VID.DESCRIPTION, + extendedDescription: VID.EXTENDED_DESCRIPTION, + demo: { isEnabled: true, text: ".vid https://www.youtube.com/watch?v=djV11Xbc914" }, + async handle(client, chat, BotsApp, args) { + try { + if (args.length === 0) { + await client.sendMessage( + BotsApp.chatId, + VID.ENTER_INPUT, + MessageType.text + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + return; + } + + var link = args[0]; + if (!ytdl.validateURL(link)) { + var reply = await client.sendMessage( + BotsApp.chatId, + VID.INVALID_LINK, + MessageType.text + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + + await client.deleteMessage(BotsApp.chatId, { + id: reply.key.id, + remoteJid: BotsApp.chatId, + formMe: true, + }); + + return; + } + + var vid_name = './' + link.match('[^/]*$')[0] + '.mp4'; + var yt = ytdl(link, {filter: format => format.container === 'mp4' && ['720p', '480p', '360p', '240p', '144p'].map(() => true)}); + yt.pipe(fs.createWriteStream(vid_name)); + var reply = await client.sendMessage( + BotsApp.chatId, + VID.REPLY, + MessageType.text + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + + yt.on('end', async () => { + await client.sendMessage( + BotsApp.chatId, + fs.readFileSync(vid_name), + MessageType.video, { + mimetype: Mimetype.mp4 + } + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + + fs.unlink(vid_name, (err) => { + if (err) console.log('[ERROR] : %s', chalk.redBright.bold(err)); + }); + }); + + await client.deleteMessage(BotsApp.chatId, { + id: reply.key.id, + remoteJid: BotsApp.chatId, + fromMe: true, + }); + } catch (err) { + console.log('[ERROR] : %s', chalk.redBright.bold(err)); + } + }, +};