-
Notifications
You must be signed in to change notification settings - Fork 217
/
index.js
73 lines (64 loc) · 2.68 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
const { create, Client } = require('@open-wa/wa-automate')
const { color, messageLog } = require('./utils')
const msgHandler = require('./handler/message')
const start = (client = new Client()) => {
console.log('[DEV]', color('Red Emperor', 'yellow'))
console.log('[CLIENT] CLIENT Started!')
// Message log for analytic
client.onAnyMessage((fn) => messageLog(fn.fromMe, fn.type))
// Force it to keep the current session
client.onStateChanged((state) => {
console.log('[Client State]', state)
if (state === 'CONFLICT' || state === 'DISCONNECTED') client.forceRefocus()
})
// listening on message
client.onMessage((message) => {
// Cut message Cache if cache more than 3K
client.getAmountOfLoadedMessages().then((msg) => (msg >= 3000) && client.cutMsgCache())
// Message Handler
msgHandler(client, message)
})
// listen group invitation
client.onAddedToGroup(({ groupMetadata: { id }, contact: { name } }) =>
client.getGroupMembersId(id)
.then((ids) => {
console.log('[CLIENT]', color(`Invited to Group. [ ${name} => ${ids.length}]`, 'yellow'))
// conditions if the group members are less than 10 then the bot will leave the group
if (ids.length <= 10) {
client.sendText(id, 'Sorry, the minimum group member is 10 user to use this bot. Bye~').then(() => client.leaveGroup(id))
} else {
client.sendText(id, `Hello group members *${name}*, thank you for inviting this bot, to see the bot menu send *#menu*`)
}
}))
// listen paricipant event on group (wellcome message)
client.onGlobalParicipantsChanged(async (event) => {
// const host = await client.getHostNumber() + '@c.us'
// if (event.action === 'add' && event.who !== host) client.sendTextWithMentions(event.chat, `Hello, Welcome to the group @${event.who.replace('@c.us', '')} \n\nHave fun with us✨`)
})
client.onIncomingCall((callData) => {
// client.contactBlock(callData.peerJid)
})
}
const options = {
sessionId: 'Imperial',
headless: true,
qrTimeout: 0,
authTimeout: 0,
restartOnCrash: start,
cacheEnabled: false,
useChrome: true,
killProcessOnBrowserClose: true,
throwErrorOnTosBlock: false,
chromiumArgs: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--aggressive-cache-discard',
'--disable-cache',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--disk-cache-size=0'
]
}
create(options)
.then((client) => start(client))
.catch((err) => new Error(err))