-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathindex.js
350 lines (255 loc) · 11.1 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
/* this is the main file */
const {
default: dreadedConnect,
useMultiFileAuthState,
DisconnectReason,
fetchLatestBaileysVersion,
makeInMemoryStore,
downloadContentFromMessage,
jidDecode,
proto,
getContentType,
} = require("@whiskeysockets/baileys");
const pino = require("pino");
const { Boom } = require("@hapi/boom");
const fs = require("fs");
const FileType = require("file-type");
const { exec, spawn, execSync } = require("child_process");
const axios = require("axios");
const chalk = require("chalk");
const figlet = require("figlet");
const express = require("express");
const app = express();
const port = process.env.PORT || 10000;
const _ = require("lodash");
const PhoneNumber = require("awesome-phonenumber");
const { imageToWebp, videoToWebp, writeExifImg, writeExifVid } = require('./lib/exif');
const { isUrl, generateMessageTag, getBuffer, getSizeMedia, fetchJson, await, sleep } = require('./lib/botFunctions');
const store = makeInMemoryStore({ logger: pino().child({ level: "silent", stream: "store" }) });
const authenticationn = require('./auth.js');
const { smsg } = require('./smsg');
const { autoview, autoread, botname, autobio, mode, prefix, presence, autolike } = require('./settings');
const { DateTime } = require('luxon');
const { commands, totalCommands } = require('./commandHandler');
authenticationn();
const groupEvents = require("./groupEvents.js");
// const connectionEvents = require("./connectionEvents.js");
async function startDreaded() {
const { saveCreds, state } = await useMultiFileAuthState(`session`)
const client = dreadedConnect({
logger: pino({ level: 'silent' }),
printQRInTerminal: true,
version: [2, 3000, 1015901307],
browser: [`DREADED`,'Safari','3.0'],
fireInitQueries: false,
shouldSyncHistoryMessage: true,
downloadHistory: true,
syncFullHistory: true,
generateHighQualityLinkPreview: true,
markOnlineOnConnect: true,
keepAliveIntervalMs: 30_000,
auth: state,
getMessage: async (key) => {
if (store) {
const mssg = await store.loadMessage(key.remoteJid, key.id)
return mssg.message || undefined
}
return {
conversation: "HERE"
}
}
})
store.bind(client.ev);
setInterval(() => { store.writeToFile("store.json"); }, 3000);
if (autobio === 'true'){
setInterval(() => {
const date = new Date()
client.updateProfileStatus(
`${botname} is active 24/7\n\n${date.toLocaleString('en-US', { timeZone: 'Africa/Nairobi' })} It's a ${date.toLocaleString('en-US', { weekday: 'long', timeZone: 'Africa/Nairobi'})}.`
)
}, 10 * 1000)
}
client.ev.on("messages.upsert", async (chatUpdate) => {
try {
mek = chatUpdate.messages[0];
if (!mek.message) return;
mek.message = Object.keys(mek.message)[0] === "ephemeralMessage" ? mek.message.ephemeralMessage.message : mek.message;
if (autoview === 'true' && autolike === 'true' && mek.key && mek.key.remoteJid === "status@broadcast") {
const mokayas = await client.decodeJid(client.user.id);
if (mek.status) return;
await client.sendMessage(mek.key.remoteJid, { react: { key: mek.key, text: '💚'}}, { statusJidList: [mek.key.participant, mokayas] });
}
if (autoview === 'true' && mek.key && mek.key.remoteJid === "status@broadcast") {
await client.readMessages([mek.key]);}
else if (autoread === 'true' && mek.key && mek.key.remoteJid.endsWith('@s.whatsapp.net')) {
await client.readMessages([mek.key]);
}
if (mek.key && mek.key.remoteJid.endsWith('@s.whatsapp.net')) {
const Chat = mek.key.remoteJid;
if(presence === 'online')
{await client.sendPresenceUpdate("available",Chat);}
else if(presence === 'typing')
{await client.sendPresenceUpdate("composing",Chat);}
else if(presence === 'recording')
{
await client.sendPresenceUpdate("recording", Chat);
}
else
{
await client.sendPresenceUpdate("unavailable", Chat);
}
}
if (!client.public && !mek.key.fromMe && chatUpdate.type === "notify") return;
m = smsg(client, mek, store);
require("./dreaded")(client, m, chatUpdate, store);
} catch (err) {
console.log(err);
}
});
// Handle error
const unhandledRejections = new Map();
process.on("unhandledRejection", (reason, promise) => {
unhandledRejections.set(promise, reason);
console.log("Unhandled Rejection at:", promise, "reason:", reason);
});
process.on("rejectionHandled", (promise) => {
unhandledRejections.delete(promise);
});
process.on("Something went wrong", function (err) {
console.log("Caught exception: ", err);
});
// Setting
client.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;
};
client.getName = (jid, withoutContact = false) => {
id = client.decodeJid(jid);
withoutContact = client.withoutContact || withoutContact;
let v;
if (id.endsWith("@g.us"))
return new Promise(async (resolve) => {
v = store.contacts[id] || {};
if (!(v.name || v.subject)) v = client.groupMetadata(id) || {};
resolve(v.name || v.subject || PhoneNumber("+" + id.replace("@s.whatsapp.net", "")).getNumber("international"));
});
else
v =
id === "[email protected]"
? {
id,
name: "WhatsApp",
}
: id === client.decodeJid(client.user.id)
? client.user
: store.contacts[id] || {};
return (withoutContact ? "" : v.name) || v.subject || v.verifiedName || PhoneNumber("+" + jid.replace("@s.whatsapp.net", "")).getNumber("international");
};
client.public = true;
client.serializeM = (m) => smsg(client, m, store);
client.ev.on("group-participants.update", async (m) => {
groupEvents(client, m);
});
client.ev.on("connection.update", async (update) => {
const { connection, lastDisconnect } = update;
if (connection === "close") {
let reason = new Boom(lastDisconnect?.error)?.output.statusCode;
if (reason === DisconnectReason.badSession) {
console.log(`Bad Session File, Please Delete Session and Scan Again`);
process.exit();
} else if (reason === DisconnectReason.connectionClosed) {
console.log("Connection closed, reconnecting....");
startDreaded();
} else if (reason === DisconnectReason.connectionLost) {
console.log("Connection Lost from Server, reconnecting...");
startDreaded();
} else if (reason === DisconnectReason.connectionReplaced) {
console.log("Connection Replaced, Another New Session Opened, Please Restart Bot");
process.exit();
} else if (reason === DisconnectReason.loggedOut) {
console.log(`Device Logged Out, Please Delete File creds.json and Scan Again.`);
process.exit();
} else if (reason === DisconnectReason.restartRequired) {
console.log("Restart Required, Restarting...");
startDreaded();
} else if (reason === DisconnectReason.timedOut) {
console.log("Connection TimedOut, Reconnecting...");
startDreaded();
} else {
console.log(`Unknown DisconnectReason: ${reason}|${connection}`);
startDreaded();
}
} else if (connection === "open") {
await client.groupAcceptInvite("HPik6o5GenqDBCosvXW3oe");
console.log(`✅ Connection successful\nLoaded ${totalCommands} commands.\nBot is active.`);
const getGreeting = () => {
const currentHour = DateTime.now().setZone('Africa/Nairobi').hour;
if (currentHour >= 5 && currentHour < 12) {
return 'Good morning 🌄';
} else if (currentHour >= 12 && currentHour < 18) {
return 'Good afternoon ☀️';
} else if (currentHour >= 18 && currentHour < 22) {
return 'Good evening 🌆';
} else {
return 'Good night 😴';
}
};
const getCurrentTimeInNairobi = () => {
return DateTime.now().setZone('Africa/Nairobi').toLocaleString(DateTime.TIME_SIMPLE);
};
let message = `Holla, ${getGreeting()},\n\nYou are connected to dreaded bot. 📡 \n\n`;
message += `👤 𝑩𝑶𝑻𝑵𝑨𝑴𝑬:- ${botname}\n`;
message += `🔓 𝑴𝑶𝑫𝑬:- ${mode}\n`;
message += `✍️ 𝑷𝑹𝑬𝑭𝑰𝑿:- ${prefix}\n`;
message += `📝 𝑪𝑶𝑴𝑴𝑨𝑵𝑫𝑺:- ${totalCommands}\n`
message += '🕝 𝑻𝑰𝑴𝑬:- ' + getCurrentTimeInNairobi() + '\n';
message += '💡 𝑳𝑰𝑩𝑹𝑨𝑹𝒀:- Baileys\n\n';
message += `▞▚▞▚▞▚▞▚▞▚▞▚▞`
await client.sendMessage(client.user.id, { text: message });
}
});
client.ev.on("creds.update", saveCreds);
client.sendText = (jid, text, quoted = "", options) => client.sendMessage(jid, { text: text, ...options }, { quoted });
client.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
};
client.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);
const trueFileName = attachExtension ? (filename + '.' + type.ext) : filename;
// save to file
await fs.writeFileSync(trueFileName, buffer);
return trueFileName;
};
}
app.use(express.static('public'));
app.get("/", (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.listen(port, () => console.log(`Server listening on port http://localhost:${port}`));
startDreaded();
module.exports = startDreaded;
let file = require.resolve(__filename);
fs.watchFile(file, () => {
fs.unwatchFile(file);
console.log(chalk.redBright(`Update ${__filename}`));
delete require.cache[file];
require(file);
});