-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
101 lines (86 loc) · 3.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as baileys from "@whiskeysockets/baileys";
import chalk from "chalk";
import readlineSync from "readline-sync";
import fs from "fs";
import pino from "pino";
import { db } from "./lib/postgres.ts";
const sessionFolder = "./session";
const credsPath = `${sessionFolder}/creds.json`;
if (!fs.existsSync(sessionFolder)) fs.mkdirSync(sessionFolder);
let usarCodigo = false;
let numero = "";
async function main() {
console.clear();
console.log(chalk.cyanBright.bold("══════════════════════════════"));
console.log(chalk.magentaBright.bold(" InfinityBot v1.0"));
console.log(chalk.cyanBright.bold("══════════════════════════════"));
console.log(chalk.green("✅ PostgreSQL conectado"));
// Si no hay credenciales guardadas, preguntar método de conexión (QR o código)
if (!fs.existsSync(credsPath)) {
console.log(chalk.green("1.") + " Conectar con código QR");
console.log(chalk.green("2.") + " Conectar con código de 8 dígitos");
const opcion = readlineSync.question(chalk.yellow("Elige una opción (1 o 2): "));
usarCodigo = opcion === "2";
if (usarCodigo) {
numero = readlineSync.question(chalk.yellow("Ingresa tu número (ej: 5218144380378): "));
}
}
startBot();
}
async function startBot() {
const { state, saveCreds } = await baileys.useMultiFileAuthState("session");
const { version } = await baileys.fetchLatestBaileysVersion();
const sock = baileys.makeWASocket({
version,
printQRInTerminal: !usarCodigo && !fs.existsSync(credsPath),
logger: pino({ level: "silent" }),
auth: {
creds: state.creds,
keys: baileys.makeCacheableSignalKeyStore(state.keys, pino({ level: "silent" }))
},
browser: ["Ubuntu", "Chrome", "108.0.5359.125"]
});
sock.ev.on("creds.update", saveCreds);
sock.ev.on("connection.update", async ({ connection, lastDisconnect }) => {
const code = (lastDisconnect?.error as any)?.output?.statusCode;
if (connection === "open") {
console.log(chalk.greenBright("¡Conectado correctamente!"));
}
if (connection === "close") {
const reconectar = code !== baileys.DisconnectReason.loggedOut;
console.log(chalk.red("Conexión cerrada. Código:"), code);
if (reconectar) {
console.log(chalk.blue("Reconectando..."));
startBot();
} else {
console.log(chalk.redBright("Sesión finalizada. Borra la carpeta 'session' y vuelve a vincular."));
}
}
});
sock.ev.on("messages.upsert", async ({ messages, type }) => {
try {
if (type !== "notify") return; // solo manejar mensajes nuevos (no historial)
// Procesar cada mensaje nuevo recibido
for (const msg of messages) {
if (!msg.message || msg.key.fromMe) continue; // ignorar mensajes sin contenido o enviados por el propio bot
const { handleMessage } = await import("./handler.ts");
await handleMessage(sock, msg); // delegar procesamiento del mensaje al handler general
}
} catch (err) {
console.error(chalk.red("❌ Error procesando mensaje:"), err);
}
});
// Si se eligió vinculación por código y aún no se ha registrado, generar el código de emparejamiento
if (usarCodigo && !state.creds.registered && !fs.existsSync(credsPath)) {
setTimeout(async () => {
try {
const code = await sock.requestPairingCode(numero);
console.log(chalk.yellow("Código de emparejamiento (8 dígitos):"), chalk.greenBright.bold(code));
console.log(chalk.gray("WhatsApp > Dispositivos vinculados > Vincular > Usar código"));
} catch (e) {
console.log(chalk.red("Error al generar el código:"), e);
}
}, 2500);
}
}
main();