-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
import { | ||
makeWASocket, | ||
Browsers, | ||
jidDecode, | ||
makeInMemoryStore, | ||
makeCacheableSignalKeyStore, | ||
fetchLatestBaileysVersion, | ||
DisconnectReason, | ||
useMultiFileAuthState, | ||
getAggregateVotesInPollMessage | ||
} from '@whiskeysockets/baileys'; | ||
import { Handler, Callupdate, GroupUpdate } from './src/event/index.js'; | ||
import { Boom } from '@hapi/boom'; | ||
import express from 'express'; | ||
import pino from 'pino'; | ||
import fs from 'fs'; | ||
import NodeCache from 'node-cache'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import { writeFile } from 'fs/promises'; | ||
import moment from 'moment-timezone'; | ||
import axios from 'axios'; | ||
import fetch from 'node-fetch'; | ||
import * as os from 'os'; | ||
import config from './config.cjs'; | ||
import pkg from './lib/autoreact.cjs'; | ||
const { emojis, doReact } = pkg; | ||
|
||
const sessionName = "session"; | ||
const app = express(); | ||
const orange = chalk.bold.hex("#FFA500"); | ||
const lime = chalk.bold.hex("#32CD32"); | ||
let useQR; | ||
let isSessionPutted; | ||
let initialConnection = true; | ||
const PORT = process.env.PORT || 3000; | ||
|
||
const MAIN_LOGGER = pino({ | ||
timestamp: () => `,"time":"${new Date().toJSON()}"` | ||
}); | ||
const logger = MAIN_LOGGER.child({}); | ||
logger.level = "trace"; | ||
|
||
const msgRetryCounterCache = new NodeCache(); | ||
|
||
const store = makeInMemoryStore({ | ||
logger: pino().child({ | ||
level: 'silent', | ||
stream: 'store' | ||
}) | ||
}); | ||
|
||
const __filename = new URL(import.meta.url).pathname; | ||
const __dirname = path.dirname(__filename); | ||
|
||
const sessionDir = path.join(__dirname, 'session'); | ||
const credsPath = path.join(sessionDir, 'creds.json'); | ||
|
||
if (!fs.existsSync(sessionDir)) { | ||
fs.mkdirSync(sessionDir, { recursive: true }); | ||
} | ||
|
||
async function downloadSessionData() { | ||
if (!config.SESSION_ID) { | ||
console.error('Please add your session to SESSION_ID env !!'); | ||
process.exit(1); | ||
} | ||
const sessdata = config.SESSION_ID.split("Imalka-MD&")[1]; | ||
const url = `https://pastebin.com/raw/${sessdata}`; | ||
try { | ||
const response = await axios.get(url); | ||
const data = typeof response.data === 'string' ? response.data : JSON.stringify(response.data); | ||
await fs.promises.writeFile(credsPath, data); | ||
console.log("🔒 Session Successfully Loaded !!"); | ||
} catch (error) { | ||
console.error('Failed to download session data:', error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (!fs.existsSync(credsPath)) { | ||
downloadSessionData(); | ||
} | ||
|
||
async function start() { | ||
try { | ||
const { state, saveCreds } = await useMultiFileAuthState(sessionDir); | ||
const { version, isLatest } = await fetchLatestBaileysVersion(); | ||
console.log(`🧬HANSAMAL-MD using WA v${version.join('.')}, isLatest: ${isLatest}`); | ||
|
||
const Matrix = makeWASocket({ | ||
version, | ||
logger: pino({ level: 'silent' }), | ||
printQRInTerminal: false, | ||
browser: ["HANSAMAL-MD", "safari", "3.3"], | ||
auth: state, | ||
getMessage: async (key) => { | ||
if (store) { | ||
const msg = await store.loadMessage(key.remoteJid, key.id); | ||
return msg.message || undefined; | ||
} | ||
return { conversation: "HANSAMAL-MD Nonstop Testing" }; | ||
} | ||
}); | ||
|
||
Matrix.ev.on('connection.update', (update) => { | ||
const { connection, lastDisconnect } = update; | ||
if (connection === 'close') { | ||
if (lastDisconnect.error.output.statusCode !== DisconnectReason.loggedOut) { | ||
start(); | ||
} | ||
} else if (connection === 'open') { | ||
if (initialConnection) { | ||
console.log(chalk.green("HANSAMAL-MD CONNECTED Successful️ ✅")); | ||
Matrix.sendMessage(Matrix.user.id, { text: `HANSAMAL-MD CONNECTED Successful️ ✅` }); | ||
initialConnection = false; | ||
} else { | ||
console.log(chalk.blue("♻️ Connection reestablished after restart.")); | ||
} | ||
} | ||
}); | ||
|
||
Matrix.ev.on('creds.update', saveCreds); | ||
|
||
Matrix.ev.on("messages.upsert", async chatUpdate => await Handler(chatUpdate, Matrix, logger)); | ||
Matrix.ev.on("call", async (json) => await Callupdate(json, Matrix)); | ||
Matrix.ev.on("group-participants.update", async (messag) => await GroupUpdate(Matrix, messag)); | ||
|
||
if (config.MODE === "public") { | ||
Matrix.public = true; | ||
} else if (config.MODE === "private") { | ||
Matrix.public = false; | ||
} | ||
|
||
Matrix.ev.on('messages.upsert', async (chatUpdate) => { | ||
try { | ||
const mek = chatUpdate.messages[0]; | ||
if (!mek.key.fromMe && config.AUTO_REACT) { | ||
console.log(mek); | ||
if (mek.message) { | ||
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)]; | ||
await doReact(randomEmoji, mek, Matrix); | ||
} | ||
} | ||
} catch (err) { | ||
console.error('Error during auto reaction:', err); | ||
} | ||
}); | ||
} catch (error) { | ||
console.error('Critical Error:', error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
start(); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello World!'); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |