forked from SlavyanDesu/BocchiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (67 loc) · 2.95 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
const { create, Client } = require('@open-wa/wa-automate')
const { color, options } = require('./tools')
const { ind, eng } = require('./message/text/lang/')
const figlet = require('figlet')
const msgHandler = require('./message')
const config = require('./config.json')
const ownerNumber = config.ownerBot
const fs = require('fs-extra')
const start = async (bocchi = new Client()) => {
console.log(color(figlet.textSync('BocchiBot', 'Larry 3D'), 'cyan'))
console.log('[BOCCHI]', color('BocchiBot is now online!'))
// Force it to keep the current session
bocchi.onStateChanged((state) => {
console.log('[BOCCHI STATE]', state)
if (state === 'UNPAIRED') bocchi.forceRefocus()
if (state === 'CONFLICT') bocchi.forceRefocus()
if (state === 'UNLAUNCHED') bocchi.forceRefocus()
})
// Set all received message to seen
bocchi.onAck((x) => {
const { to } = x
if (x !== 3) bocchi.sendSeen(to)
})
// Listening added to group
bocchi.onAddedToGroup(async (chat) => await bocchi.sendText(chat.groupMetadata.id, ind.addedGroup(chat)))
// Listening to messages
bocchi.onMessage((message) => {
bocchi.getAmountOfLoadedMessages()
.then((msg) => {
if (msg >= 1000) {
console.log('[BOCCHI]', color(`Loaded message reach ${msg}, cuting message cache...`, 'yellow'))
bocchi.cutMsgCache()
console.log('[BOCCHI]', color('Cache deleted!', 'yellow'))
}
})
msgHandler(bocchi, message) // Message handler
})
// Block person who called bot
bocchi.onIncomingCall(async (callData) => {
await bocchi.sendText(callData.peerJid, ind.blocked(ownerNumber))
await bocchi.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
bocchi.onGlobalParicipantsChanged(async (event) => {
const _welcome = JSON.parse(fs.readFileSync('./database/group/welcome.json'))
const isWelcome = _welcome.includes(event.chat)
const botNumbers = await bocchi.getHostNumber() + '@c.us'
try {
if (event.action === 'add' && event.who !== botNumbers && isWelcome) {
const pic = await bocchi.getProfilePicFromServer(event.who)
if (pic === undefined) {
await bocchi.sendFileFromUrl(event.chat, 'https://i.imgur.com/UxvMPUz.png', 'profile.png', '')
} else {
await bocchi.sendFileFromUrl(event.chat, pic, 'profile.jpg', '')
}
await bocchi.sendTextWithMentions(event.chat, ind.welcome(event))
}
} catch (err) {
console.error(err)
}
})
}
// Creating session
create(options(start))
.then((bocchi) => start(bocchi))
.catch((err) => console.error(err))