-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
126 lines (110 loc) · 3.51 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 { version } = require('./package.json')
const { create, Client } = require('@open-wa/wa-automate');
const { msgHandler, restartHandler, autoRemoveHandler, forwardHandler, welcomeMsgHandler, setupBot } = require('./handler');
const mutexify = require('mutexify/promise')
/**
* Get all unread messages and go over them.
*
* @param {Client} client
*/
async function handleUnread(client) {
const unreadMessages = await client.getAllUnreadMessages();
const promises = [];
unreadMessages.forEach(message => {
promises.push(msgHandler(client, message))
promises.push(forwardHandler(client, message))
});
await Promise.all(promises)
.catch(err => {
console.error(err);
setTimeout(() => {
return client.kill()
}, 4000);
})
}
const start = async (client = new Client()) => {
console.log(`The Multitasker [Version ${version}]`)
// Mutex lock for handleUnread.
const lock = mutexify()
// refresh the client every 2 hours.
setInterval(() => {
client.kill();
}, 3600000 * 2)
// Set the host number globally.
await setupBot(client);
// Activate all commands that run in background if they were active before restart.
restartHandler(client, ['redalerts'])
// If diconnected, go over missed messages.
client.onStateChanged(async (state) => {
console.log('[Client State]', state)
const release = await lock()
if (state === 'CONNECTED') await handleUnread(client);
release();
// Force it to keep the current session
if (state === 'CONFLICT' || state === 'DISCONNECTED') client.forceRefocus();
}).catch(err => {
console.error(err)
setTimeout(() => {
return client.kill()
}, 4000);
})
await handleUnread(client);
client.onMessage(message => {
// Message handler.
msgHandler(client, message);
// Forwarding handler.
forwardHandler(client, message);
}).catch(err => {
console.error(err);
setTimeout(() => {
return client.kill()
}, 4000);
})
client.onAnyMessage(message => {
// Auto remove users.
autoRemoveHandler(client, message);
// Welcome message.
welcomeMsgHandler(client, message);
}).catch(err => {
console.error(err);
setTimeout(() => {
return client.kill()
}, 4000);
})
}
const options = {
sessionId: 'TheMultitasker',
multiDevice: true,
qrTimeout: 0,
authTimeout: 0,
headless: true,
cacheEnabled: false,
restartOnCrash: start,
killProcessOnBrowserClose: true,
// log browser errors
logConsoleErrors: true,
// try to auto detect chrome location.
// run instead of chromium
// useChrome: true
// custom path windows
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
// for linux
// executablePath: '/usr/bin/google-chrome-stable',
// for heroku
// executablePath: '/app/.apt/usr/bin/google-chrome',
throwErrorOnTosBlock: false,
disableSpins: true,
chromiumArgs: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--aggressive-cache-discard',
'--disable-cache',
'--disable-gl-drawing-for-tests',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--disk-cache-size=0'
]
};
create(options)
.then(client => start(client))
.catch((err) => console.error(err));