Spaces:
Paused
Paused
edit
Browse files
index.js
CHANGED
|
@@ -1,183 +1,183 @@
|
|
| 1 |
-
import dotenv from 'dotenv';
|
| 2 |
-
dotenv.config();
|
| 3 |
-
|
| 4 |
-
import {
|
| 5 |
-
makeWASocket,
|
| 6 |
-
Browsers,
|
| 7 |
-
fetchLatestBaileysVersion,
|
| 8 |
-
DisconnectReason,
|
| 9 |
-
useMultiFileAuthState,
|
| 10 |
-
} from '@whiskeysockets/baileys';
|
| 11 |
-
import { Handler, Callupdate, GroupUpdate } from './src/event/index.js';
|
| 12 |
-
import express from 'express';
|
| 13 |
-
import pino from 'pino';
|
| 14 |
-
import fs from 'fs';
|
| 15 |
-
import NodeCache from 'node-cache';
|
| 16 |
-
import path from 'path';
|
| 17 |
-
import chalk from 'chalk';
|
| 18 |
-
import moment from 'moment-timezone';
|
| 19 |
-
import axios from 'axios';
|
| 20 |
-
import config from './config.cjs';
|
| 21 |
-
import pkg from './lib/autoreact.cjs';
|
| 22 |
-
const { emojis, doReact } = pkg;
|
| 23 |
-
|
| 24 |
-
const sessionName = "session";
|
| 25 |
-
const app = express();
|
| 26 |
-
const orange = chalk.bold.hex("#FFA500");
|
| 27 |
-
const lime = chalk.bold.hex("#32CD32");
|
| 28 |
-
let useQR = false;
|
| 29 |
-
let initialConnection = true;
|
| 30 |
-
const PORT = process.env.PORT ||
|
| 31 |
-
|
| 32 |
-
const MAIN_LOGGER = pino({
|
| 33 |
-
timestamp: () => `,"time":"${new Date().toJSON()}"`
|
| 34 |
-
});
|
| 35 |
-
const logger = MAIN_LOGGER.child({});
|
| 36 |
-
logger.level = "trace";
|
| 37 |
-
|
| 38 |
-
const msgRetryCounterCache = new NodeCache();
|
| 39 |
-
|
| 40 |
-
const __filename = new URL(import.meta.url).pathname;
|
| 41 |
-
const __dirname = path.dirname(__filename);
|
| 42 |
-
|
| 43 |
-
const sessionDir = path.join('.', 'session');
|
| 44 |
-
const credsPath = path.join(sessionDir, 'creds.json');
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
async function downloadSessionData() {
|
| 48 |
-
if (!config.SESSION_ID) {
|
| 49 |
-
console.error('Please add your session to SESSION_ID env !!');
|
| 50 |
-
return false;
|
| 51 |
-
}
|
| 52 |
-
const sessdata = config.SESSION_ID.split("Ethix-MD&")[1];
|
| 53 |
-
const url = `https://pastebin.com/raw/${sessdata}`;
|
| 54 |
-
try {
|
| 55 |
-
const response = await axios.get(url);
|
| 56 |
-
const data = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
|
| 57 |
-
await fs.promises.writeFile(credsPath, data);
|
| 58 |
-
console.log("🔒 Session Successfully Loaded !!");
|
| 59 |
-
return true;
|
| 60 |
-
} catch (error) {
|
| 61 |
-
// console.error('Failed to download session data:', error);
|
| 62 |
-
return false;
|
| 63 |
-
}
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
async function start() {
|
| 67 |
-
try {
|
| 68 |
-
const { state, saveCreds } = await useMultiFileAuthState(sessionDir);
|
| 69 |
-
const { version, isLatest } = await fetchLatestBaileysVersion();
|
| 70 |
-
console.log(`🤖 Ethix-MD using WA v${version.join('.')}, isLatest: ${isLatest}`);
|
| 71 |
-
|
| 72 |
-
const Matrix = makeWASocket({
|
| 73 |
-
version,
|
| 74 |
-
logger: pino({ level: 'silent' }),
|
| 75 |
-
printQRInTerminal: useQR,
|
| 76 |
-
browser: ["Ethix-MD", "safari", "3.3"],
|
| 77 |
-
auth: state,
|
| 78 |
-
getMessage: async (key) => {
|
| 79 |
-
if (store) {
|
| 80 |
-
const msg = await store.loadMessage(key.remoteJid, key.id);
|
| 81 |
-
return msg.message || undefined;
|
| 82 |
-
}
|
| 83 |
-
return { conversation: "Ethix-MD whatsapp user bot" };
|
| 84 |
-
}
|
| 85 |
-
});
|
| 86 |
-
|
| 87 |
-
Matrix.ev.on('connection.update', (update) => {
|
| 88 |
-
const { connection, lastDisconnect } = update;
|
| 89 |
-
if (connection === 'close') {
|
| 90 |
-
if (lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
| 91 |
-
start();
|
| 92 |
-
}
|
| 93 |
-
} else if (connection === 'open') {
|
| 94 |
-
if (initialConnection) {
|
| 95 |
-
console.log(chalk.green("😃 Integration Successful️ ✅"));
|
| 96 |
-
Matrix.sendMessage(Matrix.user.id, { text: `😃 Integration Successful️ ✅` });
|
| 97 |
-
initialConnection = false;
|
| 98 |
-
} else {
|
| 99 |
-
console.log(chalk.blue("♻️ Connection reestablished after restart."));
|
| 100 |
-
}
|
| 101 |
-
}
|
| 102 |
-
});
|
| 103 |
-
|
| 104 |
-
Matrix.ev.on('creds.update', saveCreds);
|
| 105 |
-
|
| 106 |
-
Matrix.ev.on("messages.upsert", async chatUpdate => await Handler(chatUpdate, Matrix, logger));
|
| 107 |
-
Matrix.ev.on("call", async (json) => await Callupdate(json, Matrix));
|
| 108 |
-
Matrix.ev.on("group-participants.update", async (messag) => await GroupUpdate(Matrix, messag));
|
| 109 |
-
|
| 110 |
-
if (config.MODE === "public") {
|
| 111 |
-
Matrix.public = true;
|
| 112 |
-
} else if (config.MODE === "private") {
|
| 113 |
-
Matrix.public = false;
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
Matrix.ev.on('messages.upsert', async (chatUpdate) => {
|
| 117 |
-
try {
|
| 118 |
-
const mek = chatUpdate.messages[0];
|
| 119 |
-
console.log(mek);
|
| 120 |
-
if (!mek.key.fromMe && config.AUTO_REACT) {
|
| 121 |
-
console.log(mek);
|
| 122 |
-
if (mek.message) {
|
| 123 |
-
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
|
| 124 |
-
await doReact(randomEmoji, mek, Matrix);
|
| 125 |
-
}
|
| 126 |
-
}
|
| 127 |
-
} catch (err) {
|
| 128 |
-
console.error('Error during auto reaction:', err);
|
| 129 |
-
}
|
| 130 |
-
});
|
| 131 |
-
|
| 132 |
-
Matrix.ev.on('messages.upsert', async (chatUpdate) => {
|
| 133 |
-
try {
|
| 134 |
-
const mek = chatUpdate.messages[0];
|
| 135 |
-
const fromJid = mek.key.participant || mek.key.remoteJid;
|
| 136 |
-
if (!mek || !mek.message) return;
|
| 137 |
-
if (mek.key.fromMe) return;
|
| 138 |
-
if (mek.message?.protocolMessage || mek.message?.ephemeralMessage || mek.message?.reactionMessage) return;
|
| 139 |
-
if (mek.key && mek.key.remoteJid === 'status@broadcast' && config.AUTO_STATUS_SEEN) {
|
| 140 |
-
await Matrix.readMessages([mek.key]);
|
| 141 |
-
|
| 142 |
-
if (config.AUTO_STATUS_REPLY) {
|
| 143 |
-
const customMessage = config.STATUS_READ_MSG || '✅ Auto Status Seen Bot By Ethix-MD-V2';
|
| 144 |
-
await Matrix.sendMessage(fromJid, { text: customMessage }, { quoted: mek });
|
| 145 |
-
}
|
| 146 |
-
}
|
| 147 |
-
} catch (err) {
|
| 148 |
-
console.error('Error handling messages.upsert event:', err);
|
| 149 |
-
}
|
| 150 |
-
});
|
| 151 |
-
|
| 152 |
-
} catch (error) {
|
| 153 |
-
console.error('Critical Error:', error);
|
| 154 |
-
process.exit(1);
|
| 155 |
-
}
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
async function init() {
|
| 159 |
-
if (fs.existsSync(credsPath)) {
|
| 160 |
-
console.log("🔒 Session file found, proceeding without QR code.");
|
| 161 |
-
await start();
|
| 162 |
-
} else {
|
| 163 |
-
const sessionDownloaded = await downloadSessionData();
|
| 164 |
-
if (sessionDownloaded) {
|
| 165 |
-
console.log("🔒 Session downloaded, starting bot.");
|
| 166 |
-
await start();
|
| 167 |
-
} else {
|
| 168 |
-
console.log("No session found or downloaded, QR code will be printed for authentication.");
|
| 169 |
-
useQR = true;
|
| 170 |
-
await start();
|
| 171 |
-
}
|
| 172 |
-
}
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
init();
|
| 176 |
-
|
| 177 |
-
app.get('/', (req, res) => {
|
| 178 |
-
res.send('Hello World!');
|
| 179 |
-
});
|
| 180 |
-
|
| 181 |
-
app.listen(PORT, () => {
|
| 182 |
-
console.log(`Server is running on port ${PORT}`);
|
| 183 |
-
});
|
|
|
|
| 1 |
+
import dotenv from 'dotenv';
|
| 2 |
+
dotenv.config();
|
| 3 |
+
|
| 4 |
+
import {
|
| 5 |
+
makeWASocket,
|
| 6 |
+
Browsers,
|
| 7 |
+
fetchLatestBaileysVersion,
|
| 8 |
+
DisconnectReason,
|
| 9 |
+
useMultiFileAuthState,
|
| 10 |
+
} from '@whiskeysockets/baileys';
|
| 11 |
+
import { Handler, Callupdate, GroupUpdate } from './src/event/index.js';
|
| 12 |
+
import express from 'express';
|
| 13 |
+
import pino from 'pino';
|
| 14 |
+
import fs from 'fs';
|
| 15 |
+
import NodeCache from 'node-cache';
|
| 16 |
+
import path from 'path';
|
| 17 |
+
import chalk from 'chalk';
|
| 18 |
+
import moment from 'moment-timezone';
|
| 19 |
+
import axios from 'axios';
|
| 20 |
+
import config from './config.cjs';
|
| 21 |
+
import pkg from './lib/autoreact.cjs';
|
| 22 |
+
const { emojis, doReact } = pkg;
|
| 23 |
+
|
| 24 |
+
const sessionName = "session";
|
| 25 |
+
const app = express();
|
| 26 |
+
const orange = chalk.bold.hex("#FFA500");
|
| 27 |
+
const lime = chalk.bold.hex("#32CD32");
|
| 28 |
+
let useQR = false;
|
| 29 |
+
let initialConnection = true;
|
| 30 |
+
const PORT = process.env.PORT || 7860;
|
| 31 |
+
|
| 32 |
+
const MAIN_LOGGER = pino({
|
| 33 |
+
timestamp: () => `,"time":"${new Date().toJSON()}"`
|
| 34 |
+
});
|
| 35 |
+
const logger = MAIN_LOGGER.child({});
|
| 36 |
+
logger.level = "trace";
|
| 37 |
+
|
| 38 |
+
const msgRetryCounterCache = new NodeCache();
|
| 39 |
+
|
| 40 |
+
const __filename = new URL(import.meta.url).pathname;
|
| 41 |
+
const __dirname = path.dirname(__filename);
|
| 42 |
+
|
| 43 |
+
const sessionDir = path.join('.', 'session');
|
| 44 |
+
const credsPath = path.join(sessionDir, 'creds.json');
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
async function downloadSessionData() {
|
| 48 |
+
if (!config.SESSION_ID) {
|
| 49 |
+
console.error('Please add your session to SESSION_ID env !!');
|
| 50 |
+
return false;
|
| 51 |
+
}
|
| 52 |
+
const sessdata = config.SESSION_ID.split("Ethix-MD&")[1];
|
| 53 |
+
const url = `https://pastebin.com/raw/${sessdata}`;
|
| 54 |
+
try {
|
| 55 |
+
const response = await axios.get(url);
|
| 56 |
+
const data = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
|
| 57 |
+
await fs.promises.writeFile(credsPath, data);
|
| 58 |
+
console.log("🔒 Session Successfully Loaded !!");
|
| 59 |
+
return true;
|
| 60 |
+
} catch (error) {
|
| 61 |
+
// console.error('Failed to download session data:', error);
|
| 62 |
+
return false;
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
async function start() {
|
| 67 |
+
try {
|
| 68 |
+
const { state, saveCreds } = await useMultiFileAuthState(sessionDir);
|
| 69 |
+
const { version, isLatest } = await fetchLatestBaileysVersion();
|
| 70 |
+
console.log(`🤖 Ethix-MD using WA v${version.join('.')}, isLatest: ${isLatest}`);
|
| 71 |
+
|
| 72 |
+
const Matrix = makeWASocket({
|
| 73 |
+
version,
|
| 74 |
+
logger: pino({ level: 'silent' }),
|
| 75 |
+
printQRInTerminal: useQR,
|
| 76 |
+
browser: ["Ethix-MD", "safari", "3.3"],
|
| 77 |
+
auth: state,
|
| 78 |
+
getMessage: async (key) => {
|
| 79 |
+
if (store) {
|
| 80 |
+
const msg = await store.loadMessage(key.remoteJid, key.id);
|
| 81 |
+
return msg.message || undefined;
|
| 82 |
+
}
|
| 83 |
+
return { conversation: "Ethix-MD whatsapp user bot" };
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
|
| 87 |
+
Matrix.ev.on('connection.update', (update) => {
|
| 88 |
+
const { connection, lastDisconnect } = update;
|
| 89 |
+
if (connection === 'close') {
|
| 90 |
+
if (lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
| 91 |
+
start();
|
| 92 |
+
}
|
| 93 |
+
} else if (connection === 'open') {
|
| 94 |
+
if (initialConnection) {
|
| 95 |
+
console.log(chalk.green("😃 Integration Successful️ ✅"));
|
| 96 |
+
Matrix.sendMessage(Matrix.user.id, { text: `😃 Integration Successful️ ✅` });
|
| 97 |
+
initialConnection = false;
|
| 98 |
+
} else {
|
| 99 |
+
console.log(chalk.blue("♻️ Connection reestablished after restart."));
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
Matrix.ev.on('creds.update', saveCreds);
|
| 105 |
+
|
| 106 |
+
Matrix.ev.on("messages.upsert", async chatUpdate => await Handler(chatUpdate, Matrix, logger));
|
| 107 |
+
Matrix.ev.on("call", async (json) => await Callupdate(json, Matrix));
|
| 108 |
+
Matrix.ev.on("group-participants.update", async (messag) => await GroupUpdate(Matrix, messag));
|
| 109 |
+
|
| 110 |
+
if (config.MODE === "public") {
|
| 111 |
+
Matrix.public = true;
|
| 112 |
+
} else if (config.MODE === "private") {
|
| 113 |
+
Matrix.public = false;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
Matrix.ev.on('messages.upsert', async (chatUpdate) => {
|
| 117 |
+
try {
|
| 118 |
+
const mek = chatUpdate.messages[0];
|
| 119 |
+
console.log(mek);
|
| 120 |
+
if (!mek.key.fromMe && config.AUTO_REACT) {
|
| 121 |
+
console.log(mek);
|
| 122 |
+
if (mek.message) {
|
| 123 |
+
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
|
| 124 |
+
await doReact(randomEmoji, mek, Matrix);
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
} catch (err) {
|
| 128 |
+
console.error('Error during auto reaction:', err);
|
| 129 |
+
}
|
| 130 |
+
});
|
| 131 |
+
|
| 132 |
+
Matrix.ev.on('messages.upsert', async (chatUpdate) => {
|
| 133 |
+
try {
|
| 134 |
+
const mek = chatUpdate.messages[0];
|
| 135 |
+
const fromJid = mek.key.participant || mek.key.remoteJid;
|
| 136 |
+
if (!mek || !mek.message) return;
|
| 137 |
+
if (mek.key.fromMe) return;
|
| 138 |
+
if (mek.message?.protocolMessage || mek.message?.ephemeralMessage || mek.message?.reactionMessage) return;
|
| 139 |
+
if (mek.key && mek.key.remoteJid === 'status@broadcast' && config.AUTO_STATUS_SEEN) {
|
| 140 |
+
await Matrix.readMessages([mek.key]);
|
| 141 |
+
|
| 142 |
+
if (config.AUTO_STATUS_REPLY) {
|
| 143 |
+
const customMessage = config.STATUS_READ_MSG || '✅ Auto Status Seen Bot By Ethix-MD-V2';
|
| 144 |
+
await Matrix.sendMessage(fromJid, { text: customMessage }, { quoted: mek });
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
} catch (err) {
|
| 148 |
+
console.error('Error handling messages.upsert event:', err);
|
| 149 |
+
}
|
| 150 |
+
});
|
| 151 |
+
|
| 152 |
+
} catch (error) {
|
| 153 |
+
console.error('Critical Error:', error);
|
| 154 |
+
process.exit(1);
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
async function init() {
|
| 159 |
+
if (fs.existsSync(credsPath)) {
|
| 160 |
+
console.log("🔒 Session file found, proceeding without QR code.");
|
| 161 |
+
await start();
|
| 162 |
+
} else {
|
| 163 |
+
const sessionDownloaded = await downloadSessionData();
|
| 164 |
+
if (sessionDownloaded) {
|
| 165 |
+
console.log("🔒 Session downloaded, starting bot.");
|
| 166 |
+
await start();
|
| 167 |
+
} else {
|
| 168 |
+
console.log("No session found or downloaded, QR code will be printed for authentication.");
|
| 169 |
+
useQR = true;
|
| 170 |
+
await start();
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
init();
|
| 176 |
+
|
| 177 |
+
app.get('/', (req, res) => {
|
| 178 |
+
res.send('Hello World!');
|
| 179 |
+
});
|
| 180 |
+
|
| 181 |
+
app.listen(PORT, () => {
|
| 182 |
+
console.log(`Server is running on port ${PORT}`);
|
| 183 |
+
});
|