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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
Las funciones ejecutarAsync y loadPlugins me ayudó ChatGPT a hacerlas
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
*/
|
| 10 |
+
import express from 'express';
|
| 11 |
+
import fs from 'fs';
|
| 12 |
+
import wweb from 'whatsapp-web.js';
|
| 13 |
+
import {Client} from 'whatsapp-web.js';
|
| 14 |
+
import {exec} from 'child_process';
|
| 15 |
+
import { MongoStore } from 'wwebjs-mongo';
|
| 16 |
+
import mongoose from 'mongoose';
|
| 17 |
+
import path from 'path';
|
| 18 |
+
const app = express();
|
| 19 |
+
|
| 20 |
+
async function ejecutarAsync(codigo, client, msg, ...args) {
|
| 21 |
+
let result;
|
| 22 |
+
try {
|
| 23 |
+
// Creamos una funcion asíncrona que ejecuta el codigo dentro de un contexto seguro
|
| 24 |
+
const asyncFunction = async (client, msg, ...args) => {
|
| 25 |
+
let capturedOutput = ''; // Variable para capturar la salida
|
| 26 |
+
|
| 27 |
+
// Redirigir la salida estandar temporalmente para capturarla
|
| 28 |
+
const originalWrite = process.stdout.write;
|
| 29 |
+
process.stdout.write = (message, encoding, callback) => {
|
| 30 |
+
capturedOutput += message.toString();
|
| 31 |
+
if (callback) callback();
|
| 32 |
+
return true;
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
// Ejecutar el codigo
|
| 36 |
+
await eval(`(async () => { ${codigo} })()`);
|
| 37 |
+
|
| 38 |
+
// Restaurar la funcion original de process.stdout.write
|
| 39 |
+
process.stdout.write = originalWrite;
|
| 40 |
+
|
| 41 |
+
return capturedOutput.trim(); // Devolver la salida capturada
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
// Ejecutamos la funcion asíncrona con los parametros especificados
|
| 45 |
+
result = await asyncFunction(client, msg, ...args);
|
| 46 |
+
} catch (error) {
|
| 47 |
+
result=error.toString();
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
return result;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// Configuracion
|
| 54 |
+
const PORT = 7860;
|
| 55 |
+
const HOST = '0.0.0.0';
|
| 56 |
+
const PREFIX=process.env.prefix;
|
| 57 |
+
const clientid="Main";
|
| 58 |
+
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
|
| 59 |
+
function between(min, max) {
|
| 60 |
+
return Math.floor(
|
| 61 |
+
Math.random() * (max - min) + min
|
| 62 |
+
);
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const generateAlphanumericString = (length) => {
|
| 66 |
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
| 67 |
+
let result = '';
|
| 68 |
+
|
| 69 |
+
for (let i = 0; i < length; i++) {
|
| 70 |
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return result;
|
| 74 |
+
};
|
| 75 |
+
let special = {
|
| 76 |
+
between:between,
|
| 77 |
+
generateAlphanumericString:generateAlphanumericString,
|
| 78 |
+
delay:delay,
|
| 79 |
+
prefix:PREFIX
|
| 80 |
+
};
|
| 81 |
+
function loadPlugins(client, app, special, callback) {
|
| 82 |
+
console.log("Cargando Plugins");
|
| 83 |
+
|
| 84 |
+
fs.readdir('./plugins', (err, files) => {
|
| 85 |
+
if (err) {
|
| 86 |
+
console.error('Error al leer el directorio de plugins:', err);
|
| 87 |
+
callback([]);
|
| 88 |
+
return;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
let modules = [];
|
| 92 |
+
let count = files.length;
|
| 93 |
+
|
| 94 |
+
if (count === 0) {
|
| 95 |
+
callback(modules);
|
| 96 |
+
return;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
files.forEach(file => {
|
| 100 |
+
console.log(`Cargando ${file}`);
|
| 101 |
+
import('./plugins/' + file).then(m => {
|
| 102 |
+
modules.push(m.default(client, app, special));
|
| 103 |
+
count--;
|
| 104 |
+
|
| 105 |
+
if (count === 0) {
|
| 106 |
+
callback(modules);
|
| 107 |
+
}
|
| 108 |
+
}).catch(err => {
|
| 109 |
+
console.error(`Error al importar ${file}:`, err);
|
| 110 |
+
count--;
|
| 111 |
+
|
| 112 |
+
if (count === 0) {
|
| 113 |
+
callback(modules); // Llamar al callback incluso si hay errores
|
| 114 |
+
}
|
| 115 |
+
});
|
| 116 |
+
});
|
| 117 |
+
});
|
| 118 |
+
}
|
| 119 |
+
function parseComando(comando){
|
| 120 |
+
return `${PREFIX}${comando.name} ${(comando.args!=undefined)?comando.args.join("|"):""} - ${comando.description}\n`;
|
| 121 |
+
|
| 122 |
+
}
|
| 123 |
+
function parseModuleComandos(module){
|
| 124 |
+
let msg=""
|
| 125 |
+
msg+=`●${(module.name==undefined)?"?":module.name}●\n`;
|
| 126 |
+
module.comandos.forEach(comando=>{
|
| 127 |
+
msg+="> "+parseComando(comando)+"\n";
|
| 128 |
+
});
|
| 129 |
+
return msg;
|
| 130 |
+
}
|
| 131 |
+
function main(client){
|
| 132 |
+
loadPlugins(client,app,special,(modules)=>{
|
| 133 |
+
client.on('message_create', async msg => {
|
| 134 |
+
let texto=msg.body.toString();
|
| 135 |
+
if(msg.fromMe){
|
| 136 |
+
//console.log(texto,texto.startsWith('!'),texto.startsWith('!gen '));
|
| 137 |
+
if(texto.startsWith(`${PREFIX}exec `)){
|
| 138 |
+
texto=texto.slice(PREFIX.length+5);
|
| 139 |
+
await delay(3000);
|
| 140 |
+
let te=await ejecutarAsync(texto,client,msg);
|
| 141 |
+
if (te){
|
| 142 |
+
await delay(3000);
|
| 143 |
+
await msg.reply(te);
|
| 144 |
+
}
|
| 145 |
+
}else if(texto==`${PREFIX}help`){
|
| 146 |
+
let mess="Comandos:\n";
|
| 147 |
+
try{
|
| 148 |
+
let modu=modules.map((module)=>parseModuleComandos(module));
|
| 149 |
+
mess+=modu.join("\n");
|
| 150 |
+
mess+=`\n[] parametros obligatorios () parametros opcionales`;
|
| 151 |
+
await msg.reply(mess);
|
| 152 |
+
}catch(e){console.log(e)}
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
}
|
| 156 |
+
});
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
function loadNormal(){
|
| 160 |
+
console.log("Corriendo Normal")
|
| 161 |
+
const client = new Client({
|
| 162 |
+
puppeteer: {
|
| 163 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
| 164 |
+
}
|
| 165 |
+
});
|
| 166 |
+
client.initialize();
|
| 167 |
+
main(client);
|
| 168 |
+
}
|
| 169 |
+
function loadMongo(){
|
| 170 |
+
console.log("Corriendo MongoDB")
|
| 171 |
+
mongoose.connect(process.env.db).then(()=>{
|
| 172 |
+
const store=new MongoStore({mongoose: mongoose });
|
| 173 |
+
const client = new Client({
|
| 174 |
+
puppeteer: {
|
| 175 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
| 176 |
+
},
|
| 177 |
+
authStrategy: new wweb.RemoteAuth({
|
| 178 |
+
store: store,
|
| 179 |
+
clientId: clientid,
|
| 180 |
+
backupSyncIntervalMs: 300000
|
| 181 |
+
})
|
| 182 |
+
});
|
| 183 |
+
client.initialize();
|
| 184 |
+
main(client);
|
| 185 |
+
});
|
| 186 |
+
}
|
| 187 |
+
if(process.env.db!=undefined){ //Chequear si usar mongodb
|
| 188 |
+
loadMongo();
|
| 189 |
+
}else{
|
| 190 |
+
loadNormal();
|
| 191 |
+
}
|
| 192 |
+
app.listen(PORT, HOST, () => {
|
| 193 |
+
if (HOST == '0.0.0.0') {
|
| 194 |
+
console.log(`Ejecutando en http://127.0.0.1:${PORT}`);
|
| 195 |
+
} else {
|
| 196 |
+
console.log(`Ejecutando en http://${HOST}:${PORT}`);
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
});
|