Compare commits
6 Commits
2.0.0
...
dependenci
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c481ae3519 | ||
|
|
b94d603572 | ||
|
|
5ba68c9ee3 | ||
|
|
240c392fad | ||
|
|
b96332e618 | ||
|
|
c95d66a763 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,5 +3,3 @@ env.js
|
|||||||
.wwebjs_cache
|
.wwebjs_cache
|
||||||
downloads
|
downloads
|
||||||
node_modules/
|
node_modules/
|
||||||
cookies.txt
|
|
||||||
bin/
|
|
||||||
230
main.js
230
main.js
@@ -5,12 +5,8 @@ const { Client, LocalAuth, MessageMedia } = pkg;
|
|||||||
|
|
||||||
import qrcode from 'qrcode-terminal';
|
import qrcode from 'qrcode-terminal';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { exec, spawn } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
import os from 'os';
|
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
console.log(os.platform());
|
|
||||||
|
|
||||||
import { CHATS } from "./env.js";
|
import { CHATS } from "./env.js";
|
||||||
|
|
||||||
@@ -34,8 +30,6 @@ function getChatId(chat) {
|
|||||||
return chat.id._serialized;
|
return chat.id._serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Eventos ----------------
|
|
||||||
|
|
||||||
client.on("qr", qr => {
|
client.on("qr", qr => {
|
||||||
console.log("[BOT] Escaneie o QR Code");
|
console.log("[BOT] Escaneie o QR Code");
|
||||||
qrcode.generate(qr, { small: true });
|
qrcode.generate(qr, { small: true });
|
||||||
@@ -53,6 +47,7 @@ client.on("disconnected", reason => {
|
|||||||
|
|
||||||
client.on("message_create", async msg => {
|
client.on("message_create", async msg => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
const chatId = getChatId(chat);
|
const chatId = getChatId(chat);
|
||||||
|
|
||||||
@@ -73,74 +68,27 @@ client.on("message_create", async msg => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---------------- Download ----------------
|
|
||||||
|
// ---------------- DOWNLOAD ----------------
|
||||||
|
|
||||||
let downloadQueue = [];
|
let downloadQueue = [];
|
||||||
let processingQueue = false;
|
let processingQueue = false;
|
||||||
|
|
||||||
function run(cmd, args) {
|
function run(cmd) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const proc = spawn(cmd, args);
|
exec(cmd, (err, stdout, stderr) => {
|
||||||
|
if (err) reject(err);
|
||||||
let stdout = "";
|
else resolve({ stdout, stderr });
|
||||||
|
|
||||||
proc.stdout.on("data", data => {
|
|
||||||
const text = data.toString();
|
|
||||||
stdout += text;
|
|
||||||
console.log("[cmd]", text);
|
|
||||||
});
|
|
||||||
|
|
||||||
proc.stderr.on("data", data => {
|
|
||||||
console.error("[cmd ERR]", data.toString());
|
|
||||||
});
|
|
||||||
|
|
||||||
proc.on("close", code => {
|
|
||||||
if (code !== 0) reject(new Error("Processo saiu com código " + code));
|
|
||||||
else resolve(stdout);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const so = os.platform();
|
|
||||||
|
|
||||||
async function get_video(url, id) {
|
async function get_video(url, id) {
|
||||||
let cmd = "";
|
|
||||||
const cmd_lin = "./bin/yt-dlp";
|
|
||||||
const cmd_win = ".\\bin\\yt-dlp.exe";
|
|
||||||
|
|
||||||
switch (so) {
|
const cmd = `./yt-dlp --extractor-args "youtube:player_client=android" -f mp4 --print after_move:filepath -o "downloads/${id}.%(ext)s" "${url}"`;
|
||||||
case "win32":
|
|
||||||
cmd = cmd_win;
|
|
||||||
break;
|
|
||||||
case "linux":
|
|
||||||
cmd = cmd_lin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const args = [
|
const { stdout } = await run(cmd);
|
||||||
'--extractor-args', 'youtube:player_client=android', // mantém seu client preferido
|
|
||||||
'-f', 'bv+ba/best', // tentar vídeo+áudio; fallback para best
|
|
||||||
'--print', 'after_move:filepath',
|
|
||||||
'--output', `downloads/${id}.%(ext)s`,
|
|
||||||
|
|
||||||
// autenticacao (use cookies exportado com yt-dlp ou --cookies-from-browser)
|
|
||||||
'--cookies', 'cookies.txt', // arquivo de cookies (gerar antes)
|
|
||||||
// cabeçalhos para reduzir 403
|
|
||||||
'--add-header', 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
|
||||||
'--add-header', 'Referer:https://www.youtube.com/',
|
|
||||||
// tolerância de rede
|
|
||||||
'--retries', '4',
|
|
||||||
'--fragment-retries', '5',
|
|
||||||
'--socket-timeout', '15',
|
|
||||||
'--sleep-interval', '1', '--max-sleep-interval', '4',
|
|
||||||
// permitir formatos que podem ser marcados como "unplayable" (útil em alguns casos)
|
|
||||||
'--allow-unplayable-formats',
|
|
||||||
// testes / debug: use -v quando precisar diagnosticar
|
|
||||||
// url
|
|
||||||
url
|
|
||||||
];
|
|
||||||
|
|
||||||
const stdout = await run(cmd, args);
|
|
||||||
const filepath = stdout.trim();
|
const filepath = stdout.trim();
|
||||||
if (!filepath) throw new Error("yt-dlp não retornou caminho");
|
if (!filepath) throw new Error("yt-dlp não retornou caminho");
|
||||||
|
|
||||||
@@ -148,65 +96,44 @@ async function get_video(url, id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function get_audio(url, id) {
|
async function get_audio(url, id) {
|
||||||
const video = await get_video(url, id);
|
|
||||||
const output = `downloads/${id}.mp3`;
|
|
||||||
|
|
||||||
let cmd = "";
|
const cmd = `./yt-dlp --extractor-args "youtube:player_client=android" -t mp3 --print after_move:filepath -o "downloads/${id}.%(ext)s" "${url}"`;
|
||||||
const cmd_lin = "./bin/ffmpeg";
|
|
||||||
const cmd_win = ".\\bin\\ffmpeg.exe";
|
|
||||||
|
|
||||||
switch (so) {
|
const { stdout } = await run(cmd);
|
||||||
case "win32":
|
|
||||||
cmd = cmd_win;
|
|
||||||
break;
|
|
||||||
case "linux":
|
|
||||||
cmd = cmd_lin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const args = [
|
const filepath = stdout.trim();
|
||||||
'-i', video,
|
if (!filepath) throw new Error("yt-dlp não retornou caminho");
|
||||||
'-vn', '-acodec',
|
|
||||||
'libmp3lame', '-q:a', '2',
|
|
||||||
output
|
|
||||||
];
|
|
||||||
|
|
||||||
await run(cmd, args);
|
return filepath;
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
function empty_folder(folder) {
|
|
||||||
fs.readdirSync(folder).forEach(file => {
|
|
||||||
const filePath = path.join(folder, file);
|
|
||||||
if (fs.lstatSync(filePath).isFile()) {
|
|
||||||
fs.unlinkSync(filePath);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function enviarVideo(chatId, path) {
|
async function enviarVideo(chatId, path) {
|
||||||
|
|
||||||
const file = fs.readFileSync(path);
|
const file = fs.readFileSync(path);
|
||||||
|
|
||||||
const media = new MessageMedia(
|
const media = new MessageMedia(
|
||||||
"video/mp4",
|
"video/mp4",
|
||||||
file.toString("base64"),
|
file.toString("base64"),
|
||||||
path.split("/").pop()
|
path.split("/").pop()
|
||||||
);
|
);
|
||||||
|
|
||||||
await client.sendMessage(chatId, media);
|
await client.sendMessage(chatId, media);
|
||||||
|
|
||||||
fs.unlinkSync(path);
|
fs.unlinkSync(path);
|
||||||
empty_folder("./downloads");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function enviarAudio(chatId, path) {
|
async function enviarAudio(chatId, path) {
|
||||||
const file = fs.readFileSync(path);
|
const file = fs.readFileSync(path);
|
||||||
|
|
||||||
const media = new MessageMedia(
|
const media = new MessageMedia(
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
file.toString("base64"),
|
file.toString("base64"),
|
||||||
path.split("/").pop()
|
path.split("/").pop()
|
||||||
);
|
);
|
||||||
|
|
||||||
await client.sendMessage(chatId, media);
|
await client.sendMessage(chatId, media);
|
||||||
|
|
||||||
fs.unlinkSync(path);
|
fs.unlinkSync(path);
|
||||||
empty_folder("./downloads");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function enqueueDownload(type, url, msg, chatId) {
|
function enqueueDownload(type, url, msg, chatId) {
|
||||||
@@ -218,8 +145,10 @@ async function processQueue() {
|
|||||||
processingQueue = true;
|
processingQueue = true;
|
||||||
while (downloadQueue.length) {
|
while (downloadQueue.length) {
|
||||||
const job = downloadQueue.shift();
|
const job = downloadQueue.shift();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let path;
|
let path;
|
||||||
|
|
||||||
if (job.type === "video")
|
if (job.type === "video")
|
||||||
path = await get_video(job.url, job.msg.id._serialized);
|
path = await get_video(job.url, job.msg.id._serialized);
|
||||||
else
|
else
|
||||||
@@ -240,7 +169,8 @@ async function processQueue() {
|
|||||||
processingQueue = false;
|
processingQueue = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Figurinha ----------------
|
|
||||||
|
// ---------------- FIGURINHA ----------------
|
||||||
|
|
||||||
async function gerarSticker(msg) {
|
async function gerarSticker(msg) {
|
||||||
if (!msg.hasMedia) {
|
if (!msg.hasMedia) {
|
||||||
@@ -249,6 +179,7 @@ async function gerarSticker(msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const media = await msg.downloadMedia();
|
const media = await msg.downloadMedia();
|
||||||
|
|
||||||
const ext = media.mimetype.split("/")[1];
|
const ext = media.mimetype.split("/")[1];
|
||||||
|
|
||||||
const input = `downloads/${msg.id._serialized}.${ext}`;
|
const input = `downloads/${msg.id._serialized}.${ext}`;
|
||||||
@@ -257,26 +188,41 @@ async function gerarSticker(msg) {
|
|||||||
fs.writeFileSync(input, Buffer.from(media.data, "base64"));
|
fs.writeFileSync(input, Buffer.from(media.data, "base64"));
|
||||||
|
|
||||||
await sharp(input)
|
await sharp(input)
|
||||||
.resize(512, 512, { fit: "contain", background: { r:0,g:0,b:0,alpha:0 } })
|
.resize(512, 512, {
|
||||||
|
fit: "contain",
|
||||||
|
background: { r:0,g:0,b:0,alpha:0 }
|
||||||
|
})
|
||||||
.webp()
|
.webp()
|
||||||
.toFile(output);
|
.toFile(output);
|
||||||
|
|
||||||
const data = fs.readFileSync(output);
|
const data = fs.readFileSync(output);
|
||||||
const sticker = new MessageMedia("image/webp", data.toString("base64"), "sticker.webp");
|
|
||||||
|
const sticker = new MessageMedia(
|
||||||
|
"image/webp",
|
||||||
|
data.toString("base64"),
|
||||||
|
"sticker.webp"
|
||||||
|
);
|
||||||
|
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
|
|
||||||
await client.sendMessage(chat.id._serialized, sticker, { sendMediaAsSticker: true });
|
await client.sendMessage(
|
||||||
|
chat.id._serialized,
|
||||||
|
sticker,
|
||||||
|
{ sendMediaAsSticker: true }
|
||||||
|
);
|
||||||
|
|
||||||
fs.unlinkSync(input);
|
fs.unlinkSync(input);
|
||||||
fs.unlinkSync(output);
|
fs.unlinkSync(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Comandos ----------------
|
|
||||||
|
// ---------------- COMANDOS ----------------
|
||||||
|
|
||||||
async function processarComando(msg, chat, chatId) {
|
async function processarComando(msg, chat, chatId) {
|
||||||
const tokens = msg.body.trim().split(/\s+/);
|
const tokens = msg.body.trim().split(/\s+/);
|
||||||
try {
|
try {
|
||||||
switch(tokens[0]) {
|
switch(tokens[0]) {
|
||||||
|
|
||||||
case "!many":
|
case "!many":
|
||||||
await chat.sendMessage(botMsg(
|
await chat.sendMessage(botMsg(
|
||||||
"Comandos:\n\n"+
|
"Comandos:\n\n"+
|
||||||
@@ -289,18 +235,23 @@ async function processarComando(msg, chat, chatId) {
|
|||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "!ping":
|
case "!ping":
|
||||||
await msg.reply(botMsg("pong 🏓"));
|
await msg.reply(botMsg("pong 🏓"));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "!video":
|
case "!video":
|
||||||
if (!tokens[1]) return;
|
if (!tokens[1]) return;
|
||||||
|
|
||||||
await msg.reply(botMsg("⏳ Baixando vídeo..."));
|
await msg.reply(botMsg("⏳ Baixando vídeo..."));
|
||||||
enqueueDownload("video", tokens[1], msg, chatId);
|
enqueueDownload("video", tokens[1], msg, chatId);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "!audio":
|
case "!audio":
|
||||||
if (!tokens[1]) return;
|
if (!tokens[1]) return;
|
||||||
|
|
||||||
await msg.reply(botMsg("⏳ Baixando áudio..."));
|
await msg.reply(botMsg("⏳ Baixando áudio..."));
|
||||||
enqueueDownload("audio", tokens[1], msg, chatId);
|
enqueueDownload("audio", tokens[1], msg, chatId);
|
||||||
break;
|
break;
|
||||||
@@ -309,71 +260,124 @@ async function processarComando(msg, chat, chatId) {
|
|||||||
await gerarSticker(msg);
|
await gerarSticker(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "!adivinhação":
|
case "!adivinhação":
|
||||||
if (!tokens[1]) {
|
if (!tokens[1]) {
|
||||||
await chat.sendMessage(botMsg("`!adivinhação começar`\n`!adivinhação parar`"));
|
await chat.sendMessage(botMsg(
|
||||||
|
"`!adivinhação começar`\n"+
|
||||||
|
"`!adivinhação parar`"
|
||||||
|
));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tokens[1] === "começar") {
|
if (tokens[1] === "começar") {
|
||||||
jogoAtivo = Math.floor(Math.random()*100)+1;
|
jogoAtivo = Math.floor(Math.random()*100)+1;
|
||||||
await chat.sendMessage(botMsg("Adivinhe o número de 1 a 100!"));
|
|
||||||
|
await chat.sendMessage(botMsg(
|
||||||
|
"Adivinhe o número de 1 a 100!"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tokens[1] === "parar") {
|
if (tokens[1] === "parar") {
|
||||||
jogoAtivo = null;
|
jogoAtivo = null;
|
||||||
|
|
||||||
await chat.sendMessage(botMsg("Jogo encerrado."));
|
await chat.sendMessage(botMsg("Jogo encerrado."));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "A":
|
case "A":
|
||||||
|
if (!tokens[1]) {
|
||||||
|
msg.reply(botMsg("B!"));
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "a":
|
case "a":
|
||||||
if (!tokens[1]) await msg.reply(botMsg("B!"));
|
if (!tokens[1]) {
|
||||||
|
msg.reply(botMsg("B!"));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "!info":
|
case "!info":
|
||||||
|
|
||||||
if (!tokens[1]) {
|
if (!tokens[1]) {
|
||||||
await chat.sendMessage(botMsg("Use:\n`!info <comando>`"));
|
await chat.sendMessage(botMsg(
|
||||||
|
"Use:\n" +
|
||||||
|
"`!info <comando>`"
|
||||||
|
));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(tokens[1]) {
|
switch(tokens[1]) {
|
||||||
|
|
||||||
case "ping":
|
case "ping":
|
||||||
await chat.sendMessage(botMsg("> `!ping`\nResponde pong."));
|
await chat.sendMessage(botMsg(
|
||||||
|
"> `!ping`\nResponde pong."
|
||||||
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "video":
|
case "video":
|
||||||
await chat.sendMessage(botMsg("> `!video <link>`\nBaixa vídeo da internet."));
|
await chat.sendMessage(botMsg(
|
||||||
|
"> `!video <link>`\nBaixa vídeo da internet.\n"
|
||||||
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "audio":
|
case "audio":
|
||||||
await chat.sendMessage(botMsg("> `!audio <link>`\nBaixa áudio da internet."));
|
await chat.sendMessage(botMsg(
|
||||||
|
"> `!audio <link>`\nBaixa áudio da internet.\n"
|
||||||
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "figurinha":
|
case "figurinha":
|
||||||
await chat.sendMessage(botMsg("`!figurinha`\nTransforma imagem/GIF em sticker."));
|
await chat.sendMessage(botMsg(
|
||||||
|
"`!figurinha`\nTransforma imagem/GIF em sticker."
|
||||||
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
await chat.sendMessage(botMsg(`❌ Comando '${tokens[1]}' não encontrado.`));
|
await chat.sendMessage(botMsg(
|
||||||
|
`❌ Comando '${tokens[1]}' não encontrado.`
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
await chat.sendMessage(botMsg("Erro:\n`"+err.message+"`"));
|
await chat.sendMessage(
|
||||||
|
botMsg("Erro:\n`"+err.message+"`")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Jogo ----------------
|
|
||||||
|
// ---------------- JOGO ----------------
|
||||||
|
|
||||||
async function processarJogo(msg, chat) {
|
async function processarJogo(msg, chat) {
|
||||||
|
|
||||||
if (!jogoAtivo) return;
|
if (!jogoAtivo) return;
|
||||||
|
|
||||||
const tentativa = msg.body.trim();
|
const tentativa = msg.body.trim();
|
||||||
|
|
||||||
if (!/^\d+$/.test(tentativa)) return;
|
if (!/^\d+$/.test(tentativa)) return;
|
||||||
|
|
||||||
const num = parseInt(tentativa);
|
const num = parseInt(tentativa);
|
||||||
|
|
||||||
if (num === jogoAtivo) {
|
if (num === jogoAtivo) {
|
||||||
|
|
||||||
await msg.reply(botMsg(`Acertou! Número: ${jogoAtivo}`));
|
await msg.reply(botMsg(`Acertou! Número: ${jogoAtivo}`));
|
||||||
|
|
||||||
jogoAtivo = null;
|
jogoAtivo = null;
|
||||||
} else if (num > jogoAtivo) {
|
|
||||||
|
}
|
||||||
|
else if (num > jogoAtivo) {
|
||||||
|
|
||||||
await chat.sendMessage(botMsg("Menor."));
|
await chat.sendMessage(botMsg("Menor."));
|
||||||
} else {
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
await chat.sendMessage(botMsg("Maior."));
|
await chat.sendMessage(botMsg("Maior."));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "whatsapp-bot",
|
"name": "whatsapp-bot",
|
||||||
"version": "2.0.0",
|
"version": "1.1.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"qrcode-terminal": "^0.12.0",
|
"qrcode-terminal": "^0.12.0",
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
export const CLIENT_ID = "bot_permanente";
|
|
||||||
export const BOT_PREFIX = "🤖 *ManyBot:* ";
|
|
||||||
export const CHATS = [
|
|
||||||
// coloque os chats que quer aqui
|
|
||||||
];
|
|
||||||
Reference in New Issue
Block a user