forked from BrunoSobrino/TheMystic-Bot-MD
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
140 lines (119 loc) · 4.32 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { join, dirname } from 'path';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import { setupMaster, fork } from 'cluster';
import cfonts from 'cfonts';
import readline from 'readline';
import yargs from 'yargs';
import chalk from 'chalk';
import fs from 'fs';
import './config.js';
const { PHONENUMBER_MCC } = await import('baileys');
const __dirname = dirname(fileURLToPath(import.meta.url));
const require = createRequire(__dirname);
const { say } = cfonts;
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
let isRunning = false;
const question = (texto) => new Promise((resolver) => rl.question(texto, resolver));
console.log(chalk.yellow.bold('—◉ㅤIniciando sistema...'));
function verificarOCrearCarpetaAuth() {
const authPath = join(__dirname, global.authFile);
if (!fs.existsSync(authPath)) {
fs.mkdirSync(authPath, { recursive: true });
}
}
function verificarCredsJson() {
const credsPath = join(__dirname, global.authFile, 'creds.json');
return fs.existsSync(credsPath);
}
function formatearNumeroTelefono(numero) {
let formattedNumber = numero.replace(/[^\d+]/g, '');
if (formattedNumber.startsWith('+52') && !formattedNumber.startsWith('+521')) {
formattedNumber = formattedNumber.replace('+52', '+521');
} else if (formattedNumber.startsWith('52') && !formattedNumber.startsWith('521')) {
formattedNumber = `+521${formattedNumber.slice(2)}`;
} else if (formattedNumber.startsWith('52') && formattedNumber.length >= 12) {
formattedNumber = `+${formattedNumber}`;
} else if (!formattedNumber.startsWith('+')) {
formattedNumber = `+${formattedNumber}`;
}
return formattedNumber;
}
function esNumeroValido(numeroTelefono) {
const numeroSinSigno = numeroTelefono.replace('+', '');
return Object.keys(PHONENUMBER_MCC).some(codigo => numeroSinSigno.startsWith(codigo));
}
async function start(file) {
if (isRunning) return;
isRunning = true;
say('The Mystic\nBot', {
font: 'chrome',
align: 'center',
gradient: ['red', 'magenta'],
});
say(`Bot creado por Bruno Sobrino`, {
font: 'console',
align: 'center',
gradient: ['red', 'magenta'],
});
verificarOCrearCarpetaAuth();
if (verificarCredsJson()) {
const args = [join(__dirname, file), ...process.argv.slice(2)];
setupMaster({ exec: args[0], args: args.slice(1) });
const p = fork();
return;
}
const opcion = await question(chalk.yellowBright.bold('—◉ㅤSeleccione una opción (solo el numero):\n') + chalk.white.bold('1. Con código QR\n2. Con código de texto de 8 dígitos\n—> '));
let numeroTelefono = '';
if (opcion === '2') {
const phoneNumber = await question(chalk.yellowBright.bold('\n—◉ㅤEscriba su número de WhatsApp:\n') + chalk.white.bold('◉ㅤEjemplo: +5219992095479\n—> '));
numeroTelefono = formatearNumeroTelefono(phoneNumber);
if (!esNumeroValido(numeroTelefono)) {
console.log(chalk.bgRed(chalk.white.bold('[ ERROR ] Número inválido. Asegúrese de haber escrito su numero en formato internacional y haber comenzado con el código de país.\n—◉ㅤEjemplo:\n◉ +5219992095479\n')));
process.exit(0);
}
process.argv.push(numeroTelefono);
}
if (opcion === '1') {
process.argv.push('qr');
} else if (opcion === '2') {
process.argv.push('code');
}
const args = [join(__dirname, file), ...process.argv.slice(2)];
setupMaster({ exec: args[0], args: args.slice(1) });
const p = fork();
p.on('message', (data) => {
console.log(chalk.green.bold('—◉ㅤRECIBIDO:'), data);
switch (data) {
case 'reset':
p.process.kill();
isRunning = false;
start.apply(this, arguments);
break;
case 'uptime':
p.send(process.uptime());
break;
}
});
p.on('exit', (_, code) => {
isRunning = false;
console.error(chalk.red.bold('[ ERROR ] Ocurrió un error inesperado:'), code);
p.process.kill();
isRunning = false;
start.apply(this, arguments);
if (process.env.pm_id) {
process.exit(1);
} else {
process.exit();
}
});
const opts = new Object(yargs(process.argv.slice(2)).exitProcess(false).parse());
if (!opts['test']) {
if (!rl.listenerCount()) {
rl.on('line', (line) => {
p.emit('message', line.trim());
});
}
}
}
start('main.js');