-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
398 lines (360 loc) · 18.8 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
const {
default: makeWASocket,
fetchLatestBaileysVersion,
// useSingleFileLegacyAuthState,
DisconnectReason,
// makeInMemoryStore,
// generateWAMessageFromContent,
// proto,
MessageType,
MessageOptions,
Mimetype,
downloadContentFromMessage,
downloadMediaMessage,
useMultiFileAuthState
// Browsers
} = require('@adiwajshing/baileys')
const { exec } = require('node:child_process');
const { createSticker, StickerTypes, default: Sticker } = require('wa-sticker-formatter')
const str_replace = require('str_replace')
const writeFile = require('fs/promises');
const util = require('util')
const pino = require('pino')
const owner = '6281382519681@s.whatsapp.net'
const fs = require('fs');
const { writeFileSync } = require('node:fs');
const path = require('node:path');
const prefix = '!' // Prefix to be use can be '!' or '.' etc
function containsWhitespace(str) {
return /\s/.test(str);
};
function removeFirstWord(str) {
const indexOfSpace = str.indexOf(' ');
return str.substring(indexOfSpace + 1);
}
function jsonReader(filePath, cb) {
fs.readFile(filePath, (err, fileData) => {
if (err) {
return cb && cb(err);
}
try {
const object = JSON.parse(fileData);
return cb && cb(null, object);
} catch (err) {
return cb && cb(err);
}
});
}
async function start() {
const { version, isLatest } = await fetchLatestBaileysVersion();
const { state, saveCreds } = await useMultiFileAuthState('auth_info');
const desiredversion = [ 2, 22, 19, 78 ]
let ws = makeWASocket({
version,
printQRInTerminal: true,
auth: state,
//msgRetryCounterMap,
// browser: Browsers.macOS('Desktop'),
logger: pino({ level: 'silent'})})
const baileysversion = version.join('.');
console.log('------------------------------------------------');
console.log('Baileys BOT');
console.log('------------------------------------------------');
console.log("[AVAILABLE]");
console.log("[VERSION] " + baileysversion);
console.log("[LATEST] : " + isLatest);
console.log('------------------------------------------------');
console.log('[DEV] SkyUnik');
console.log('[SERVER] Server Started! \n');
ws.ev.on('creds.update', saveCreds)
ws.ev.on('connection.update', async (update) => {
const { connection, lastDisconnect } = update;
if(connection == 'close') {
const shouldReconnect = lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut
if(lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut) {
console.log('- connection closed, reconnecting...')
if(shouldReconnect) {
start()
}
} else {
console.log('+ connection closed', lastDisconnect.error, ', reconnecting ', shouldReconnect)
}
} else if(connection === 'open') {
console.log('opened connection')
}
})
ws.ev.on('messages.upsert', async (chatUpdate) => {
console.log(`[RECEIVED] : ${Object.keys(chatUpdate.messages).length}`);
if (chatUpdate.messages[0]) {
//result message income
const messageRaw = chatUpdate.messages[0]
let message = messageRaw
// Return if statement
if (!message) return
if (message.key && message.key.remoteJid == 'status@broadcast') return
if (message.key.fromMe == true) return
//Formatting
message = require('./Message_format.js')(ws, messageRaw)
if (message.body === undefined) return
// console.log(message)
// Ternary Operators to check if the sender is a group and is admin
const groupMetadata = message.isGroupMsg ? await ws.groupMetadata(message.from) : ''
const groupMembers = message.isGroupMsg ? groupMetadata.participants : ''
const groupAdmins0 = message.isGroupMsg ? groupMembers.filter(admn => admn.admin != null) : ''
const groupAdmins = message.isGroupMsg ? groupAdmins0.map(admn2 => admn2.id) : ''
const isGroupAdmins = message.isGroupMsg ? groupAdmins.includes(message.sender) | message.sender.includes(owner) : false
const msgbody = message.body
if (msgbody.startsWith(prefix)) {
await ws.readMessages([message.key])
// Declare some important variable
let command = containsWhitespace(msgbody) ? msgbody.replace(/^\s+|\s+$/g, '').split(/\s+/)[0] : msgbody
let argument = containsWhitespace(msgbody) ? str_replace(command,'', msgbody).replace(/^\s+|\s+$/g, '') : false
const from = message.from
const sender = message.sender
const metadata = await ws.groupMetadata(message.chatId)
// const store = makeInMemoryStore({ })
await ws.sendPresenceUpdate('composing', from)
let notesingroup = []
const jsonnotesingroup = fs.readFileSync("./datan.json", 'utf-8')
for (let result of JSON.parse(jsonnotesingroup)) {
if (result.chatId == message.chatId) {
// console.log(result);
notesingroup.push(prefix + result.command)
}}
if (notesingroup.includes(command)) {
let argumentnotes
for (let result of JSON.parse(jsonnotesingroup)) {
if (result.chatId == message.chatId) {
// console.log(result);
// console.log(command);
if (prefix + result.command == command) {
// console.log('CODE PASSED BOI')
// console.log(result.argument);
argumentnotes = result.argument
}
}}
await ws.sendMessage(from, {text: util.format(argumentnotes)}, {quoted: message})
return;
}
switch(command.toLowerCase()){
case prefix + 'run':
if (sender == owner) {
try {
await eval(argument);
} catch (err) {
console.error(`[CODE_ERROR] : ${err}`);
await ws.sendMessage(from, { text: `Error!\n${err}\n${argument}`})
}
} else {
await ws.sendMessage(from, { text: `IM SORRY BUT YOU CANNOT ${prefix}run\nTHIS COMMAND IS ONLY FOR OWNER`})
}
break
case prefix + 'reply':
await ws.sendMessage(from, { text: `I've Been told to: ${command}` }, { quoted: message })
break
case prefix + 'sme': // sme a.ka send to me
if (sender == owner) {
await ws.sendMessage(owner, { text: util.format(message) })
await ws.sendMessage(from, { text: 'Done'}, { quoted: message })
} else {
await ws.sendMessage(from, { text: 'No'}, { quoted: message })
}
break
case prefix + 'notes':
let notesgrupj = []
let somenotestehre = []
for (let result of JSON.parse(jsonnotesingroup)) {
somenotestehre.push(result.chatId == message.chatId)
if (result.chatId == message.chatId) {
// console.log(result);
notesgrupj.push({title: prefix + result.command})
}}
// console.log(notesgrupj);
if (!somenotestehre.includes(true)) {
ws.sendMessage(from, {text: `There isn't any notes in here.\nGroup Name: *${metadata.subject}*\nNotes: NONE\n${prefix}addnotes to add notes..`}, {quoted: message})
return;
}
const sections = [
{
title: "Notes",
rows: notesgrupj
},
]
const listMessage = {
text: `Group Name: *${metadata.subject}*\n${notesingroup.join('\r\n')}`,
// footer: notesingroup.join('\r\n'),
title: "Daftar Saved Notes",
buttonText: "NOTES",
sections
}
ws.sendMessage(from, listMessage, {quoted: message})
break
case prefix + 'addnotes':
let news = []
let hasinit = []
const commandnotes = argument != false ? argument.split(/\s+/)[0] : null
if (commandnotes === null) {
ws.sendMessage(from, {text: "Please Give an Argument."}, {quoted: message})
return;
}
const notesargument = commandnotes != null ? removeFirstWord(argument) : null
for (let result of JSON.parse(jsonnotesingroup)) {
if (result.chatId == message.chatId) {
// console.log(result);
hasinit.push(result.command == commandnotes)
}}
if (hasinit.includes(true)) {
ws.sendMessage(from, {text: `There is Already Notes By the name of ${prefix+commandnotes}.`}, {quoted: message})
return;
}
jsonReader("./datan.json", (err, datan) => {
if (err) {
console.log("Error reading file:", err);
return;
}
if(typeof datan !== 'undefined'){
const jsondata =
{"chatId": message.chatId, "id": message.id, "command" : commandnotes, "argument": notesargument}
// {"chatid": "something@ilham.sus", "id": "69", "argument": "im over ehre"}
datan.push(jsondata)
fs.writeFile("./datan.json", JSON.stringify(datan, null, 2), err => {
if (err) console.log("Error writing file:", err);
try {
jsonReader("./datan.json", (err, datan) => {
ws.sendMessage(from, {text: util.format(jsondata)})
})
} catch (err) {
ws.sendMessage(from, {text: 'failed reading file'})
return;
}});
}});
// for(let Ndatas of datan) {
// listo.push(participant);
// mentionse.push(contact);
// const sume = contact.split('@s.whatsapp.net')
// const memey = sume[0]
// texte += `@${memey} `;
// }
break
case prefix + 'delnotes' :
let delhasinit = []
const comandemen = argument != false ? argument.split(/\s+/)[0] : null
if (comandemen === null) {
ws.sendMessage(from, {text: "Please Give an Argument."}, {quoted: message})
return;
}
for (let result of JSON.parse(jsonnotesingroup)) {
if (result.chatId == message.chatId) {
// console.log(result);
delhasinit.push(result.command == comandemen)
}}
// console.log(delhasinit);
if (!delhasinit.includes(true)) {
ws.sendMessage(from, {text: `There isn't any Notes in this group By the name of ${prefix+comandemen}.`}, {quoted: message})
return;
}
// console.log(comandemen);
let filtered = JSON.parse(jsonnotesingroup).filter(function(el) { return el.command != str_replace(prefix,'', comandemen); });
fs.writeFile("./datan.json", JSON.stringify(filtered, null, 2), err => {
if (err) console.log("Error writing file:", err);
try {
jsonReader("./datan.json", (err, datan) => {
ws.sendMessage(from, {text: "Done."})
})
} catch (err) {
ws.sendMessage(from, {text: 'failed reading file'})
return;
}});
break
case prefix + 'everyone':
const group_all = await ws.groupMetadata(from)
// const participante = group_all.participants
// console.log(group_all);
const splet = message.sender.split('@s.whatsapp.net')
let texte = `@${splet} Tagged you :\n`;
let mentionse = [];
let listo = [];
for(let participant of group_all.participants) {
const contact = participant.id;
listo.push(participant);
mentionse.push(contact);
const sume = contact.split('@s.whatsapp.net')
const memey = sume[0]
texte += `@${memey} `;
}
await ws.sendMessage(from, { text: texte, mentions: mentionse })
break
case prefix + 'sticker':
if (message.quotedMsg ? message.quotedMsg.isMedia : message.isMedia == true) {
const name = 'sticker-img';
const pathget = message.message.imageMessage ? message.message.imageMessage.mimetype : message.message.videoMessage ? message.message.videoMessage.mimetype : message.quotedMsg ? message.quotedMsg.mimetype : null
const size = message.message.videoMessage ? message.message.videoMessage.fileLength : message.quotedMsg ? message.quotedMsg.fileLength : null
// console.log(size > 5000000);
// console.log(pathget);
if ( size > 5000000) {
await ws.sendMessage(from, {text: `kegedean mas file nya\nmax nya 5mb doang`}, {quoted: message})
return
}
const mediaData = message.quotedMsg.isMedia ? await message.quotedMsg.getMedia() : message.isMedia ? await downloadMediaMessage(
message,
'buffer',
{ },
{
//logger,
// pass this so that baileys can request a reupload of media
// that has been deleted
reuploadRequest: ws.updateMediaMessage
}
) : null
// console.log(mediaData);
if (pathget.includes('video')) {
// const path = str_replace('image/', '', pathget);
const pathclean = str_replace('video/', '', pathget);
// save to file
writeFileSync(`./${name}.${pathclean}`, mediaData)
const folder = ('./')
exec('ffmpeg -i '+folder+name+'.'+pathclean+' -t 3s -v quiet -vcodec libwebp -filter:v fps=fps=20 -lossless 0 -compression_level 3 -q:v 70 -loop 1 -preset picture -an -vsync 0 '+folder+name+'.webp', async(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
//return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
//return;
}
const filesticker = fs.readFileSync(`./${name}.webp`)
const stickerOptions = new Sticker(filesticker, {
pack: 'STICKERS', // pack name
author: 'Bot WA Fatih V2', // author name
type: StickerTypes.CROPPED, // sticker type
quality: 100, // quality of the output file
})
// const buffer = await stickerOptions.toBuffer()
await ws.sendMessage(from, await stickerOptions.toMessage() , {quoted: message})
// fs.unlinkSync(`./${name}.webp`, `./${name}.${pathclean}`)
})
//await delay(6000)
}
const stickerimg = new Sticker(mediaData, {
pack: 'STICKERS', // pack name
author: 'Bot WA Fatih V2', // author name
type: StickerTypes.CROPPED, // sticker type
quality: 100, // quality of the output file
})
// const buffer = await stickerOptions.toBuffer()
await ws.sendMessage(from, await stickerimg.toMessage() , {quoted: message})
} else {
ws.sendMessage(from, { text: `Itu bukan gambar... kirimin gambar mazzeh` }, { quoted: message })
}
break
default:
// console.log(command);
// console.log(notesingroup);
// console.log(notesingroup.includes(command));
await ws.sendMessage(from, { text: `${command} COMMAND is not in a condition block...` }, { quoted: message })
// code
}}
}
})
}
start()