forked from bennysolo/whatsapp-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (119 loc) · 6.17 KB
/
Copy pathindex.js
File metadata and controls
128 lines (119 loc) · 6.17 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
const { create, Client } = require('@open-wa/wa-automate')
const color = require('./lib/color')
const { loader } = require('./function')
const figlet = require('figlet')
const fs = require('fs-extra')
const bennymsg = require('./bennymsg')
const canvas = require('discord-canvas')
const ownerNumber = JSON.parse(fs.readFileSync('./settings/owner.json'))
const options = require('./options')
const start = (client = new Client()) => {
console.log(color(figlet.textSync('BennyBot', 'Larry 3D'), 'cyan'))
console.log('[Benny]', color('BennyBot is now online!'))
console.log(color('[DEV]', 'cyan'), color('Welcome back, Owner! Hope you are doing well~', 'magenta'))
// loader.nocache('../message/text/lang/ind.js', m => console.log(color('[WATCH]', 'orange'), color(`=> '${m}'`, 'yellow'), 'file is updated!'))
// loader.nocache('../message/text/lang/eng.js', m => console.log(color('[WATCH]', 'orange'), color(`=> '${m}'`, 'yellow'), 'file is updated!'))
// Force it to keep the current session
client.onStateChanged((state) => {
console.log('[Benny STATE]', state)
if (state === 'UNPAIRED' || state === 'CONFLICT' || state === 'UNLAUNCHED') client.forceRefocus()
})
// Set all received message to seen
client.onAck((x) => {
const { to } = x
if (x !== 3) client.sendSeen(to)
})
// Listening added to group
client.onAddedToGroup(((chat) => {
let totalMem = chat.groupMetadata.participants.length
if (totalMem < 0) {
client.sendText(chat.id, `Cih member nya cuma ${totalMem}, Kalo mau invite bot, minimal jumlah mem ada 0`).then(() => client.leaveGroup(chat.id)).then(() => client.deleteChat(chat.id))
} else {
client.sendText(chat.groupMetadata.id, `Halo warga grup *${chat.contact.name}* terimakasih sudah menginvite bot ini, untuk melihat menu silahkan kirim *!help*`)
}
}))
// Listening to messages
client.onMessage((message) => {
client.getAmountOfLoadedMessages()
.then((msg) => {
if (msg >= 1000) {
console.log('[Benny]', color(`Loaded message reach ${msg}, cuting message cache...`, 'yellow'))
client.cutMsgCache()
console.log('[Benny]', color('Cache deleted!', 'yellow'))
}
})
// Below is an watched version but it will affect the performance
require('./bennymsg')(client, message)
// msgHandler(Benny, message)
})
// Block person who called bot
client.onIncomingCall(async (callData) => {
await client.sendText(callData.peerJid, ind.blocked(ownerNumber))
await client.contactBlock(callData.peerJid)
console.log(color('[BLOCK]', 'red'), color(`${callData.peerJid} has been blocked. Reason:`, 'yellow'), color('CALLING THE BOT', 'cyan'))
})
// Listen to group's event
client.onGlobalParicipantsChanged(async (event) => {
const _welcome = JSON.parse(fs.readFileSync('./lib/welcome.json'))
const isWelcome = _welcome.includes(event.chat)
const gcChat = await client.getChatById(event.chat)
const pcChat = await client.getContact(event.who)
let { pushname, verifiedName, formattedName } = pcChat
pushname = pushname || verifiedName || formattedName
const { name, groupMetadata } = gcChat
const botNumbers = await client.getHostNumber() + '@c.us'
try {
if (event.action === 'add' && event.who !== botNumbers && isWelcome) {
const pic = await client.getProfilePicFromServer(event.who)
if (pic === undefined) {
var picx = 'https://i.ibb.co/Tq7d7TZ/age-hananta-495-photo.png'
} else {
picx = pic
}
const welcomer = await new canvas.Welcome()
.setUsername(pushname)
.setDiscriminator(event.who.substring(6, 10))
.setMemberCount(groupMetadata.participants.length)
.setGuildName(name)
.setAvatar(picx)
.setColor('border', '#00100C')
.setColor('username-box', '#00100C')
.setColor('discriminator-box', '#00100C')
.setColor('message-box', '#00100C')
.setColor('title', '#00FFFF')
.setBackground('https://www.photohdx.com/images/2016/05/red-blurry-background.jpg')
.toAttachment()
const base64 = `data:image/png;base64,${welcomer.toBuffer().toString('base64')}`
await client.sendFile(event.chat, base64, 'welcome.png', `Welcome ${pushname}!`)
} else if (event.action === 'remove' && event.who !== botNumbers && isWelcome) {
const pic = await client.getProfilePicFromServer(event.who)
if (pic === undefined) {
var picxs = 'https://i.ibb.co/Tq7d7TZ/age-hananta-495-photo.png'
} else {
picxs = pic
}
const bye = await new canvas.Goodbye()
.setUsername(pushname)
.setDiscriminator(event.who.substring(6, 10))
.setMemberCount(groupMetadata.participants.length)
.setGuildName(name)
.setAvatar(picxs)
.setColor('border', '#00100C')
.setColor('username-box', '#00100C')
.setColor('discriminator-box', '#00100C')
.setColor('message-box', '#00100C')
.setColor('title', '#00FFFF')
.setBackground('https://www.photohdx.com/images/2016/05/red-blurry-background.jpg')
.toAttachment()
const base64 = `data:image/png;base64,${bye.toBuffer().toString('base64')}`
await client.sendFile(event.chat, base64, 'welcome.png', `Bye ${pushname}, we will miss you~`)
}
} catch (err) {
console.error(err)
}
})
}
// Creating session
create('Benny', options(true, start))
.then(client => start(client))
.catch((error) => console.log(error))