forked from ibnusyawall/aex-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
265 lines (239 loc) · 8.97 KB
/
client.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
/**
* https://github.com/ibnusyawall/aex-bot
* Date: 28/08/20
* client.js
**/
const {
Client,
Location,
GroupChat,
MessageMedia,
Chat,
Message
} = require('whatsapp-web.js')
const {
menu,
about,
quotes,
toxic,
getBase64,
qrMaker,
readQR,
ytDlMP3,
tts,
generateQuotesDefault,
generateQuotes
} = require('./lib/WALIB')
const SESSION_FILE_PATH = process.cwd() + '/session.json'
const needle = require('needle')
const qrcode = require('qrcode-terminal')
const fs = require('fs')
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionCfg = require(SESSION_FILE_PATH)
}
let pathChrome = {
executablePath: '',
}
const os = require('os').platform()
if (os === 'linux') {
pathChrome.executablePath = '/usr/bin/chromium'
} else if (os === 'win32' || os === 'win64') {
pathChrome.executablePath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
} else if (os === 'darwin') {
pathChrome.executablePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
}
const client = new Client({
puppeteer: {
headless: true,
args: [
"--disable-extensions",
"--disable-setuid-sandbox",
"--no-sandbox",
"--disable-accelerated-2d-canvas",
"--no-zygote",
'--aggressive-cache-discard',
'--disable-cache',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--disk-cache-size=0'
],
executablePath: pathChrome.executablePath
},
session: sessionCfg
})
const run = async () => {
client.initialize()
client.on('qr', (qr) => {
qrcode.generate(qr, {small: true})
})
client.on('authenticated', (session) => {
console.log('[:] TERSAMBUNG : ', JSON.stringify(session));
sessionCfg = session;
fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
if (err) throw err;
console.log(`[√] Qr Tersimpan: ${SESSION_FILE_PATH}`)
})
})
client.on('auth_failure', msg => {
console.error('[:] SAMBUNGAN GAGAL : ', msg);
})
client.on('ready', () => {
console.log('[:] READY ');
})
client.on('message', async msg => {
EventHandler(client, msg)
})
client.on('group_join', async notification => {
client.sendMessage(notification.id.remote, `Wellcome to Group!\n\nJangan lupa baca deskripsi group terlebih dahulu, dan semoga betah.\n\n-- wellcome-group by: aex-bot`)
})
}
const EventHandler = async (client_, msg_) => {
try {
const chat = await msg_.getChat()
const users = await msg_.getContact()
const { type, from, to, author, mentionedIds } = msg_
let { body } = msg_
function replace(text) {
return text.replace('@c.us', '')
}
const dari = typeof author === 'undefined' ? from : author
const prefix = '!'
const args = body.slice(prefix.length).trim().split(/ +/);
const argv = body.slice(prefix.length).trim().split(/ +/);
const text = args.splice(1).join(' ')
const number = text.indexOf('62') === -1 ? text.replace('0', '62') + '@c.us' : text
switch (args[0]) {
case 'setName':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.setSubject(text)
}
break;
case 'setDesc':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.setDescription(text)
}
break;
case 'promote':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.promoteParticipants([mentionedIds[0]])
}
break;
case 'demote':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.demoteParticipants([mentionedIds[0]])
}
break;
case 'add':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.addParticipants([number])
}
break;
case 'kick':
if (chat.isGroup) {
if (replace(author) === chat.owner.user) chat.removeParticipants([...mentionedIds])
}
break;
case 'owner':
if (chat.isGroup) {
client_.sendMessage(from, `owner this group: ${chat.owner.user}`)
}
break;
case 'menu':
menu().then(menu => {
console.log(menu)
client_.sendMessage(from, menu)
})
break;
case 'info':
about().then(info => {
client_.sendMessage(from, info)
})
break;
case 'quotes':
quotes().then(quotes => {
chat.sendMessage(`Quotes request by @${users.number},\n\nQuotes :\n\n " *${quotes}* " `, {
mentions: [users]
})
})
break;
case 'toxic':
toxic().then(toxic => {
client_.sendMessage(from, toxic)
})
break;
case 'qr':
qrMaker(text).then(result => {
msg_.reply(new MessageMedia('image/jpeg', result, 'qrcode'))
})
break;
case 'quotesmaker':
const attachmentData = await msg_.downloadMedia()
if (!argv[1] || !argv[2] || !argv[3]) msg_.reply('argv invalid.')
if (type == 'image') {
fs.writeFile(process.cwd() + '/quotesClient.jpg', attachmentData.data, 'base64', (err) => {
try {
generateQuotes(process.cwd() + '/quotesClient.jpg', argv[1], argv[2], argv.splice(3).join(' '))
.then(data => {
msg_.reply(new MessageMedia('image/jpeg', data, 'quotes'))
})
} catch (err) {
client_.sendMessage(from, 'error parse data.')
}
})
} else {
generateQuotesDefault(argv[1], argv[2], argv.splice(3).join(' ')).then(data => {
msg_.reply(new MessageMedia('image/jpeg', data, 'quotes'))
})
}
break;
case 'tts':
switch (argv[1]) {
case 'id':
tts('id', argv.splice(2).join(' ')).then(data => {
msg_.reply(new MessageMedia('audio/mp3', data, 'tts'))
})
break;
case 'en':
tts('en', argv.splice(2).join(' ')).then(data => {
msg_.reply(new MessageMedia('audio/mp3', data, 'tts'))
})
break;
default:
client_.sendMessage(from, 'piilih bahasa: \n\n*id* untuk bahasa indonesia \n*en* untuk bahasa inggris')
break;
}
break;
case 'ytmp3':
let isLink = text.match(/^https:\/\/youtu(?:\.be|be\.com)/g)
if (isLink !== null) {
ytDlMP3(text).then(data => {
msg_.reply(new MessageMedia('audio/mp3', data, 'aexbot'))
})
}
break;
case 'botjoin':
const inviteCode = argv[1].replace('https://chat.whatsapp.com/', '')
if (argv[1].match(/(https:)/gi)) {
try {
await client_.acceptInvite(inviteCode);
msg_.reply('🍻 Terimakasih, bot akan segera masuk!.');
} catch (e) {
msg_.reply('🔐 Sepertinya link group telah dibatalkan sob!.');
}
} else {
msg_.reply('😎👊 ini link ?')
}
break;
}
console.log(msg_, users)
} catch (err) {
console.log(err)
}
}
( async () => {
run()
})()
module.exports = {
client
}