Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added vid command (downloads video): vid.js module <issue #102> #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
74 changes: 74 additions & 0 deletions modules/vid.js
Original file line number Diff line number Diff line change
@@ -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));
}
},
};