-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
126 lines (115 loc) · 4.31 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
const {
default: makeWASocket,
useMultiFileAuthState,
DisconnectReason,
Browsers,
makeInMemoryStore,
fetchLatestBaileysVersion,
delay,
makeCacheableSignalKeyStore
} = require('@adiwajshing/baileys');
const fs = require('fs');
const path = require('path');
const pino = require('pino');
const config = require('./config');
const { Message, commands, numToJid, sudoIds, PREFIX } = require('./lib/index');
const axios = require('axios');
/* async function saveJsonToFile(folder, id) {
try {
const session = {};
const fixFileName = (file) => file?.replace(/\//g, '__')?.replace(/:/g, '-');
for (const objectName in session) {
if (session.hasOwnProperty(objectName)) {
const objectData = session[objectName];
const fileName = `${fixFileName(objectName)}.json`;
const serializedData = JSON.stringify(objectData);
fs.writeFileSync(`${folder}/${fileName}`, serializedData);
}
}
} catch (error) {}
} */
const connect = async () => {
fs.readdirSync('./plugins').forEach(plugin => {
if (path.extname(plugin).toLowerCase() == '.js') {
require('./plugins/' + plugin);
}
});
const { state, saveCreds } = await useMultiFileAuthState('auth');
const { version, isLatest } = await fetchLatestBaileysVersion();
const logger = pino({ level: 'silent' });
const connectToWhatsApp = async () => {
const client = makeWASocket({
logger,
printQRInTerminal: true,
downloadHistory: false,
syncFullHistory: false,
browser: Browsers.macOS('Desktop'),
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
version,
});
client.ev.on('connection.update', async (node) => {
const { connection, lastDisconnect } = node;
if (connection == 'open') {
console.log("Connecting to Whatsapp...");
console.log('connected');
await delay(5000);
const sudo = numToJid(config.SUDO.split(',')[0]) || client.user.id;
await client.sendMessage(sudo, { text: '*BOT CONNECTED*\n\n```PREFIX : ' + PREFIX + '\nPLUGINS : ' + commands.filter(command => command.pattern).length + '\nVERISON : ' + require('./package.json').version + '```'});
}
if (connection === 'close') {
// const { error, message } = lastDisconnect.error?.output.payload;
if (lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut) {
await delay(300);
connectToWhatsApp();
console.log('reconnecting...');
console.log(node)
} else {
console.log('connection closed');
await delay(3000);
process.exit(0);
}
}
});
client.ev.on('creds.update', saveCreds);
client.ev.on('messages.upsert', async (upsert) => {
if (!upsert.type === 'notify') return;
msg = upsert.messages[0];
if (!msg.message) return;
const message = new Message(client, msg);
if (config.LOG_MSG && !message.data.key.fromMe) console.log(`[MESSAGE] [${message.pushName || message.sender.split('@')[0]}] : ${message.text || message.type || null}`);
if (config.READ_MSG == true && message.data.key.remoteJid !== 'status@broadcast') await client.readMessages([message.data.key]);
commands.map(async (command) => {
const messageType = {
image: 'imageMessage',
sticker: 'stickerMessage',
audio: 'audioMessage',
video: 'videoMessage',
};
const isMatch =
(command.on && messageType[command.on] && message.msg && message.msg[messageType[command.on]] !== null) ||
(!command.pattern || command.pattern.test(message.text)) ||
(command.on === 'text' && message.text) ||
(command.on && !messageType[command.on] && !message.msg[command.on]);
if (isMatch) {
if (command.pattern && config.READ_CMD == true) await client.readMessages([message.data.key]);
const match = message.text?.match(command.pattern) || '';
try {
await command.function(message, match.length === 6 ? (match[3] ?? match[4]) : (match[2] ?? match[3]), client);
} catch (e) {
if (config.ERROR_MSG) {
console.log(e)
const sudo = numToJid(config.SUDO.split(',')[0]) || client.user.id;
await client.sendMessage(sudo, { text: '```─━❲ ERROR REPORT ❳━─\n\nMessage : ' + message.text + '\nError : ' + e.message + '\nJid : ' + message.jid + '```'}, { quoted: message.data });
}
}
}
});
});
return client;
};
connectToWhatsApp()
};
connect()