-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.js
More file actions
192 lines (180 loc) · 8.11 KB
/
client.js
File metadata and controls
192 lines (180 loc) · 8.11 KB
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
const { useSingleFileAuthState, DisconnectReason, makeInMemoryStore, msgRetryCounterMap, delay } = require('baileys')
const session = process.argv[2] ? process.argv[2] + '.json' : 'session.json'
const { state, saveState } = useSingleFileAuthState(session)
const pino = require('pino'), path = require('path'), fs = require('fs'), colors = require('@colors/colors/safe'), qrcode = require('qrcode-terminal')
const spinnies = new (require('spinnies'))()
const { Socket, Serialize, Scandir } = require('./system/extra')
global.props = new (require('./system/dataset'))
global.neoxr = new (require('./system/map'))
global.Func = new (require('./system/function'))
global.scrap = new (require('./system/scraper'))
global.store = makeInMemoryStore({
logger: pino().child({
level: 'silent',
stream: 'store'
})
})
const removeAuth = () => {
try {
fs.unlinkSync('./' + session)
} catch {}
}
const connect = async () => {
setInterval(removeAuth, 1000 * 60 * 1)
let content = await props.fetch()
if (!content || Object.keys(content).length === 0) {
global.db = {
users: {},
chats: {},
groups: {},
statistic: {},
sticker: {},
setting: {},
creds: {}
}
await props.save()
} else {
global.db = content
try {
if (global.db.creds) {
credentials = {
creds: content.creds
}
credentials.creds.noiseKey.private = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.noiseKey.private.buffer) : Buffer.from(credentials.creds.noiseKey.private)
credentials.creds.noiseKey.public = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.noiseKey.public.buffer) : Buffer.from(credentials.creds.noiseKey.public)
credentials.creds.signedIdentityKey.private = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signedIdentityKey.private.buffer) : Buffer.from(credentials.creds.signedIdentityKey.private)
credentials.creds.signedIdentityKey.public = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signedIdentityKey.public.buffer) : Buffer.from(credentials.creds.signedIdentityKey.public)
credentials.creds.signedPreKey.keyPair.private = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signedPreKey.keyPair.private.buffer) : Buffer.from(credentials.creds.signedPreKey.keyPair.private)
credentials.creds.signedPreKey.keyPair.public = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signedPreKey.keyPair.public.buffer) : Buffer.from(credentials.creds.signedPreKey.keyPair.public)
credentials.creds.signedPreKey.signature = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signedPreKey.signature.buffer) : Buffer.from(credentials.creds.signedPreKey.signature)
credentials.creds.signalIdentities[0].identifierKey = /mongo/.test(process.env.DATABASE_URL) ? Buffer.from(content.creds.signalIdentities[0].identifierKey.buffer) : Buffer.from(credentials.creds.signalIdentities[0].identifierKey)
//state.creds = credentials.creds
} else {
global.db.creds = state.creds
}
} catch (e) {
console.log(e)
global.db.creds = state.creds
}
}
global.client = Socket({
logger: pino({
level: 'silent'
}),
printQRInTerminal: true,
markOnlineOnConnect: true,
msgRetryCounterMap,
browser: ['@Siesta / Siest-bot', 'Chrome', '1.0.0'],
auth: state,
// To see the latest version : https://web.whatsapp.com/check-update?version=1&platform=web
version: [2, 2236, 10],
getMessage: async (key) => {
return await store.loadMessage(client.decodeJid(key.remoteJid), key.id)
}
})
store.bind(client.ev)
client.ev.on('connection.update', async (update) => {
const {
connection,
lastDisconnect,
qr
} = update
if (lastDisconnect == 'undefined' && qr != 'undefined') {
qrcode.generate(qr, {
small: true
})
}
if (connection === 'connecting') spinnies.add('start', {
text: 'Tunggu sebentar. . .'
})
if (connection === 'open') {
global.db.creds = client.authState.creds
spinnies.succeed('start', {
text: `Terhubung ke ${client.user.name || client.user.verifiedName}`
})
}
if (connection === 'close') {
if (lastDisconnect.error.output.statusCode == DisconnectReason.loggedOut) {
spinnies.fail('start', {
text: `Can't connect to Web Socket`
})
delete global.db.creds
await props.save()
process.exit(0)
} else {
connect().catch(() => connect())
}
}
// if (update.receivedPendingNotifications) await client.reply(global.owner + '@c.us', Func.texted('bold', `🚩 Successfully connected to WhatsApp.`))
})
client.ev.on('creds.update', () => saveState)
client.ev.on('messages.upsert', async chatUpdate => {
try {
m = chatUpdate.messages[0]
if (!m.message) return
Serialize(client, m)
Scandir('./plugins').then(files => {
global.client.plugins = Object.fromEntries(files.filter(v => v.endsWith('.js')).map(file => [path.basename(file).replace('.js', ''), require(file)]))
}).catch(e => console.error(e))
require('./system/config'), require('./handler')(client, m)
} catch (e) {
console.log(e)
}
})
client.ev.on('contacts.update', update => {
for (let contact of update) {
let id = client.decodeJid(contact.id)
if (store && store.contacts) store.contacts[id] = {
id,
name: contact.notify
}
}
})
client.ev.on('group-participants.update', async (room) => {
let meta = await (await client.groupMetadata(room.id))
let member = room.participants[0]
let text_welcome = `*Welcome +tag*`
let text_left = `*+tag Sayonara*`
let groupSet = global.db.groups[room.id]
try {
pic = await Func.fetchBuffer(await client.profilePictureUrl(member, 'image'))
} catch {
pic = await Func.fetchBuffer(await client.profilePictureUrl(member, 'image'))
}
if (room.action == 'add') {
if (groupSet.localonly) {
if (typeof global.db.users[member] != 'undefined' && !global.db.users[member].whitelist && !member.startsWith('62') || !member.startsWith('62')) {
client.reply(room.id, Func.texted('bold', `Sorry @${member.split`@`[0]}, this group is only for indonesian people and you will removed automatically.`))
client.updateBlockStatus(member, 'block')
return await Func.delay(2000).then(() => client.groupParticipantsUpdate(room.id, [member], 'remove'))
}
}
let txt = (groupSet.text_welcome != '' ? groupSet.text_welcome : text_welcome).replace('+tag', `@${member.split`@`[0]}`).replace('+grup', `${meta.subject}`)
if (groupSet.welcome) client.sendMessageModify(room.id, txt, null, {
title: 'Kanna downgrade',
largeThumb: true,
thumbnail: await Func.fetchBuffer('https://telegra.ph/file/dec8b92f3dec03da50c97.jpg'),
})
} else if (room.action == 'remove') {
let txt = (groupSet.text_left != '' ? groupSet.text_left : text_left).replace('+tag', `@${member.split`@`[0]}`).replace('+grup', `${meta.subject}`)
if (groupSet.left) client.sendMessageModify(room.id, txt, null, {
title: 'Kanna downgrade',
largeThumb: true,
thumbnail: pic,
})
}
})
client.ws.on('CB:call', async json => {
if (json.content[0].tag == 'offer') {
let object = json.content[0].attrs['call-creator']
await Func.delay(2000)
await client.updateBlockStatus(object, 'block')
}
})
setInterval(async () => {
global.db.creds = client.authState.creds
if (global.db) await props.save()
}, 10_000)
return client
}
connect().catch(() => connect())