forked from adisubagja/KryPtoN-WhatsApp-Bot
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
58 lines (50 loc) · 2.28 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
const { create, Client } = require('@open-wa/wa-automate')
const { color } = require('./utils')
const options = require('./utils/options')
const msgHandler = require('./handler/message')
const start = (client = new Client()) => {
console.log('[DEV]', color('Red Emperor', 'yellow'))
console.log('[CLIENT] CLIENT Started!')
// Force it to keep the current session
client.onStateChanged((state) => {
console.log('[Client State]', state)
if (state === 'CONFLICT') client.forceRefocus()
})
// listening on message
client.onMessage((message) => {
client.getAmountOfLoadedMessages() // Cut message Cache if cache more than 3K
.then((msg) => {
if (msg >= 3000) {
console.log('[CLIENT]', color(`Loaded Message Reach ${msg}, cuting message cache...`, 'yellow'))
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 2 then the bot will leave the group
if (ids.length <= 2) {
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*`)
}
}))
client.onRemovedFromGroup((data) => {
console.log(data)
})
// listen paricipant event on group (wellcome message)
client.onGlobalParicipantsChanged((event) => {
if (event.action === 'add') 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)
})
}
create('Imperial', options(true, start))
.then((client) => start(client))
.catch((err) => new Error(err))