forked from A-S-W-I-N-S-P-A-R-K-Y/X-BOT-MD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
471 lines (431 loc) · 14.5 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
require("./config");
const X = require("./config")
const {
default: xbotConnect,
DisconnectReason,
fetchLatestBaileysVersion,
downloadContentFromMessage,
makeInMemoryStore,
jidDecode,
useMultiFileAuthState,
} = require("@whiskeysockets/baileys");
const fs = require("fs");
const fx = require("fs-extra");
const figlet = require("figlet");
const { join } = require("path");
const got = require("got");
const pino = require("pino");
const path = require("path");
const FileType = require("file-type");
const { AUTO_STATUS_VIEW, SUDO } = require("./config")
const { Boom } = require("@hapi/boom");
const { serialize, WAConnection } = require("./lib/whatsapp.js");
const { smsg, getBuffer, getSizeMedia } = require("./lib/Function2");
const express = require("express");
const app = express();
const PORT = X.PORT
const owner = global.owner
const welcomeLeft = require("./lib/Welcome.js");
const { readcommands, commands } = require("./lib/Plugins.js");
commands.prefix = X.PREFIX;
const mongoose = require("mongoose");
const qrcode = require("qrcode");
var CryptoJS = require("crypto-js");
const axios = require('axios');
const web = require ('./lib/server.js')
const more = String.fromCharCode(8206)
const readMore = more.repeat(4001)
const {
getPluginURLs, // -------------------- GET ALL PLUGIN DATA FROM DATABASE
} = require("./lib/MongoDB/MONGO_VEDI.js");
const chalk = require("chalk");
const store = makeInMemoryStore({
logger: pino().child({
level: "silent",
stream: "store",
}),
});
// Xbot Server configuration
let QR_GENERATE = "invalid";
let status;
//creds fresher-----------------------------------------
async function freshsession() {
if (fx.existsSync('./lib/auth_info_baileys')) {
fx.emptyDirSync(__dirname + '/lib/auth_info_baileys');
};
console.log("OLD SESSION CLOSED")
}
freshsession();
//
//session id connect----------------------------------------------------------------------------
(function(_0xea80ff,_0x3ad506){const _0x1c15a8=_0x16b3,_0x5991bb=_0xea80ff();while(!![]){try{const _0x4ac573=parseInt(_0x1c15a8(0x8e))/0x1*(-parseInt(_0x1c15a8(0x92))/0x2)+parseInt(_0x1c15a8(0x88))/0x3+parseInt(_0x1c15a8(0x8d))/0x4*(parseInt(_0x1c15a8(0x8b))/0x5)+-parseInt(_0x1c15a8(0x87))/0x6+parseInt(_0x1c15a8(0x93))/0x7+parseInt(_0x1c15a8(0x91))/0x8+-parseInt(_0x1c15a8(0x8f))/0x9;if(_0x4ac573===_0x3ad506)break;else _0x5991bb['push'](_0x5991bb['shift']());}catch(_0x44dec2){_0x5991bb['push'](_0x5991bb['shift']());}}}(_0x54a8,0xbaa2f));async function MakeSession(){const _0x556af6=_0x16b3;try{console['log']('WRITING\x20SESSION...');const {data:_0x2455ec}=await axios(_0x556af6(0x8c)+X['SESSION_ID'][_0x556af6(0x89)](':')[0x1]);await fs['writeFileSync']('./lib/auth_info_baileys/creds.json',JSON[_0x556af6(0x8a)](_0x2455ec)),console[_0x556af6(0x90)]('SESSION\x20CREATED\x20SUCCESSFULLY✅'),console['log']('BASE\x20X-BOT-MD🍒🫠');}catch(_0x283c28){console[_0x556af6(0x90)](_0x283c28);}}function _0x54a8(){const _0x587230=['log','5833968GwvmrZ','6DEVPLb','6706280bsMglB','862524OqHbUa','1270056yIfQfK','split','stringify','10TstQWP','https://paste.c-net.org/','952460pajGyt','70382RPggBc','13207545hyLMkc'];_0x54a8=function(){return _0x587230;};return _0x54a8();}function _0x16b3(_0xbb1e0e,_0x1cf9f0){const _0x54a8b1=_0x54a8();return _0x16b3=function(_0x16b31b,_0xcf256d){_0x16b31b=_0x16b31b-0x87;let _0x8137a1=_0x54a8b1[_0x16b31b];return _0x8137a1;},_0x16b3(_0xbb1e0e,_0x1cf9f0);}MakeSession();
//----------------------------------------------------------------------------------
const startXbot = async () => {
//make session----------------------------------------------------------------------------
const getVersionWaweb = () => {
let version
try {
let a = fetchJson('https://web.whatsapp.com/check-update?version=1&platform=web')
version = [a.currentVersion.replace(/[.]/g, ', ')]
} catch {
version = [2, 2204, 13]
}
return version
}
//------------------------------------------------------------------------------------------------
try {
await mongoose.connect(X.mongodb).then(() => {
console.log(
chalk.greenBright("Connecting to MongoDB📊\n")
);
});
} catch (err) {
console.log(
chalk.redBright(
"Error connecting to MongoDB📊\nPlease check MongoDB URI\n"
)
);
console.log(err);
}
//
const { state, saveCreds, saveState, clearState } =await useMultiFileAuthState(`./lib/auth_info_baileys`)
//
//
console.log(`\n`);
await installPlugin();
const { version, isLatest } = await fetchLatestBaileysVersion();
const Xbot = xbotConnect({
logger: pino({ level: "silent" }),
printQRInTerminal: false,
browser: ["X-BOT-MD V2"],
auth: state,
version,
});
store.bind(Xbot.ev);
Xbot.public = true;
async function installPlugin() {
console.log(chalk.yellow("CHECKING FOR EXTERNAL PLUGINS🧩\n"));
let plugins = [];
try {
plugins = await getPluginURLs();
} catch (err) {
console.log(
chalk.redBright(
"Error connecting to MongoDB📊\nPlease check MongoDB URI\n"
)
);
console.log(err);
}
if (!plugins.length || plugins.length == 0) {
console.log(
chalk.redBright("NO EXTERNAL PLUGINS INSTALLED🧩\n")
);
} else {
console.log(
chalk.greenBright(plugins.length + "INSTALLING EXTERNAL PLUGINS🧩\n")
);
for (let i = 0; i < plugins.length; i++) {
pluginUrl = plugins[i];
var { body, statusCode } = await got(pluginUrl);
if (statusCode == 200) {
try {
var folderName = "Plugins";
var fileName = path.basename(pluginUrl);
var filePath = path.join(folderName, fileName);
fs.writeFileSync(filePath, body);
} catch (error) {
console.log("Error:", error);
}
}
}
console.log(
chalk.greenBright(
"PLUGIN'S INSTALLED🧩\n"
)
);
}
}
await readcommands();
//Xbot.ev.on("creds.update", saveState);
//Xbot.serializeM = (m) => smsg(Xbot, m, store);
Xbot.ev.on("connection.update", async (update) => {
const { lastDisconnect, connection, qr } = update;
if (connection) {
console.info(`BOT IS ${connection}`)
}
if (connection==="open") {
let text = `_*x-ʙᴏᴛ ᴍᴅ ᴄᴏɴɴᴇᴄᴛᴇᴅ*_ ${readMore} \n\n
_*Auto status read :*_ *_${X.AUTO_STATUS_VIEW}_*\n
_*Always online : false*_\n
_*Prefix : ${X.PREFIX}*_\n
_*Version : 2.00*_`
for (let num of global.owner) {
Xbot.sendMessage(num + "@s.whatsapp.net", {text : text})
}
}
if (connection === "close") {
let reason = new Boom(lastDisconnect?.error)?.output.statusCode;
if (reason === DisconnectReason.badSession) {
console.log(
`[ X-BOT-MD ] Bad Session File, Please Delete Session and Scan Again.\n`
);
process.exit();
} else if (reason === DisconnectReason.connectionClosed) {
console.log("[ X-BOT-MD ] Connection closed, reconnecting....\n");
startXbot();
} else if (reason === DisconnectReason.connectionLost) {
console.log("[ X-BOT-MD ] Connection Lost from Server, reconnecting...\n");
startXbot();
} else if (reason === DisconnectReason.connectionReplaced) {
console.log(
"[ X-BOT-MD ] Connection Replaced, Another New Session Opened, Please Close Current Session First!\n"
);
process.exit();
} else if (reason === DisconnectReason.loggedOut) {
clearState();
console.log(
`[ X-BOT-MD ] Device Logged Out, Please Delete Session and Scan Again.\n`
);
process.exit();
} else if (reason === DisconnectReason.restartRequired) {
console.log("[ X-BOT-MD ] Server Restarting...\n");
startXbot();
} else if (reason === DisconnectReason.timedOut) {
console.log("[ X-BOT-MD ] Connection Timed Out, Trying to Reconnect...\n");
startXbot();
} else {
console.log(
`[ X-BOT-MD ] Server Disconnected: "It's either safe disconnect or WhatsApp Account got banned !\n"`
);
}
}
if (qr) {
QR_GENERATE = qr;
}
});
Xbot.ev.on("group-participants.update", async (m) => {
welcomeLeft(Xbot, m);
});
Xbot.ev.on("messages.upsert", async (chatUpdate) => {
//////////////////////////////////////
/* try {
const mek = chatUpdate.messages[0]
if (!mek.message) return
} catch (e){
console.log(e)
}*/
//////////////////////////////////////
m = serialize(Xbot, chatUpdate.messages[0]);
if (!m.message) return;
m.message = (Object.keys(m.message)[0] === 'ephemeralMessage') ? m.message.ephemeralMessage.message : m.message
if (m.key && m.key.remoteJid === 'status@broadcast'){
if (X.AUTO_STATUS_VIEW) {
Xbot.readMessages([m.key])
}
}
if (m.key.id.startsWith("BAE5") && m.key.id.length == 16) return;
require("./X-BOT-MD.js")(Xbot, m, commands, chatUpdate);
});
Xbot.getName = (jid, withoutContact = false) => {
id = Xbot.decodeJid(jid);
withoutContact = Xbot.withoutContact || withoutContact;
let v;
if (id.endsWith("@g.us"))
return new Promise(async (resolve) => {
v = store.contacts[id] || {};
if (!(v.name || v.subject)) v = Xbot.groupMetadata(id) || {};
resolve(
v.name ||
v.subject ||
PhoneNumber("+" + id.replace("@s.whatsapp.net", "")).getNumber(
"international"
)
);
});
else
v =
id === "0@s.whatsapp.net"
? {
id,
name: "WhatsApp",
}
: id === Xbot.decodeJid(Xbot.user.id)
? Xbot.user
: store.contacts[id] || {};
return (
(withoutContact ? "" : v.name) ||
v.subject ||
v.verifiedName ||
PhoneNumber("+" + jid.replace("@s.whatsapp.net", "")).getNumber(
"international"
)
);
};
Xbot.decodeJid = (jid) => {
if (!jid) return jid;
if (/:\d+@/gi.test(jid)) {
let decode = jidDecode(jid) || {};
return (
(decode.user && decode.server && decode.user + "@" + decode.server) ||
jid
);
} else return jid;
};
Xbot.ev.on("contacts.update", (update) => {
for (let contact of update) {
let id = Xbot.decodeJid(contact.id);
if (store && store.contacts)
store.contacts[id] = {
id,
name: contact.notify,
};
}
});
Xbot.downloadAndSaveMediaMessage = async (
message,
filename,
attachExtension = true
) => {
let quoted = message.msg ? message.msg : message;
let mime = (message.msg || message).mimetype || "";
let messageType = message.mtype
? message.mtype.replace(/Message/gi, "")
: mime.split("/")[0];
const stream = await downloadContentFromMessage(quoted, messageType);
let buffer = Buffer.from([]);
for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]);
}
let type = await FileType.fromBuffer(buffer);
trueFileName = attachExtension ? filename + "." + type.ext : filename;
// save to file
await fs.writeFileSync(trueFileName, buffer);
return trueFileName;
};
//POLL
Xbot.sendPoll = (jid, name = '', values = [], selectableCount = 1) => { return Xbot.sendMessage(jid, { poll: { name, values, selectableCount }}) }
Xbot.downloadMediaMessage = async (message) => {
let mime = (message.msg || message).mimetype || "";
let messageType = message.mtype
? message.mtype.replace(/Message/gi, "")
: mime.split("/")[0];
const stream = await downloadContentFromMessage(message, messageType);
let buffer = Buffer.from([]);
for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]);
}
return buffer;
};
Xbot.parseMention = async (text) => {
return [...text.matchAll(/@([0-9]{5,16}|0)/g)].map(
(v) => v[1] + "@s.whatsapp.net"
);
};
Xbot.sendText = (jid, text, quoted = "", options) =>
Xbot.sendMessage(
jid,
{
text: text,
...options,
},
{
quoted,
}
);
Xbot.getFile = async (PATH, save) => {
let res;
let data = Buffer.isBuffer(PATH)
? PATH
: /^data:.*?\/.*?;base64,/i.test(PATH)
? Buffer.from(PATH.split`,`[1], "base64")
: /^https?:\/\//.test(PATH)
? await (res = await getBuffer(PATH))
: fs.existsSync(PATH)
? ((filename = PATH), fs.readFileSync(PATH))
: typeof PATH === "string"
? PATH
: Buffer.alloc(0);
let type = (await FileType.fromBuffer(data)) || {
mime: "application/octet-stream",
ext: ".bin",
};
filename = path.join(
__filename,
"../src/" + new Date() * 1 + "." + type.ext
);
if (data && save) fs.promises.writeFile(filename, data);
return {
res,
filename,
size: await getSizeMedia(data),
...type,
data,
};
};
Xbot.setStatus = (status) => {
Xbot.query({
tag: "iq",
attrs: {
to: "@s.whatsapp.net",
type: "set",
xmlns: "status",
},
content: [
{
tag: "status",
attrs: {},
content: Buffer.from(status, "utf-8"),
},
],
});
return status;
};
//
//
Xbot.sendFile = async (jid, PATH, fileName, quoted = {}, options = {}) => {
let types = await Xbot.getFile(PATH, true);
let { filename, size, ext, mime, data } = types;
let type = "",
mimetype = mime,
pathFile = filename;
if (options.asDocument) type = "document";
if (options.asSticker || /webp/.test(mime)) {
let { writeExif } = require("./lib/sticker.js");
let media = {
mimetype: mime,
data,
};
pathFile = await writeExif(media, {
packname: global.packname,
author: global.packname,
categories: options.categories ? options.categories : [],
});
await fs.promises.unlink(filename);
type = "sticker";
mimetype = "image/webp";
} else if (/image/.test(mime)) type = "image";
else if (/video/.test(mime)) type = "video";
else if (/audio/.test(mime)) type = "audio";
else type = "document";
await Xbot.sendMessage(
jid,
{
[type]: {
url: pathFile,
},
mimetype,
fileName,
...options,
},
{
quoted,
...options,
}
);
return fs.promises.unlink(pathFile);
};
};
web ()
setTimeout( () => {
startXbot ()
}, 5000)