forked from TobyG74/ChisatoBOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (134 loc) · 5.45 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const { create, Client } = require('@open-wa/wa-automate')
const welcome = require('./lib/welcome')
const left = require('./lib/left')
const cron = require('node-cron')
const color = require('./lib/color')
const fs = require('fs')
// const msgHndlr = require ('./tobz')
const figlet = require('figlet')
const lolcatjs = require('lolcatjs')
const options = require('./options')
// AUTO UPDATE BY NURUTOMO
// THX FOR NURUTOMO
// Cache handler and check for file change
require('./tobz.js')
nocache('./tobz.js', module => console.log(`'${module}' Updated!`))
require('./lib/help.js')
nocache('./lib/help.js', module => console.log(`'${module}' Updated!`))
require('./lib/database/setting.json')
nocache('./lib/database/setting.json', module => console.log(`'${module}' Updated!`))
const adminNumber = JSON.parse(fs.readFileSync('./lib/database/admin.json'))
const setting = JSON.parse(fs.readFileSync('./lib/database/setting.json'))
const isWhite = (chatId) => adminNumber.includes(chatId) ? true : false
let {
limitCount,
memberLimit,
groupLimit,
mtc: mtcState,
banChats,
restartState: isRestart
} = setting
function restartAwal(tobz){
setting.restartState = false
isRestart = false
tobz.sendText(setting.restartId, 'Restart Succesfull!')
setting.restartId = 'undefined'
//fs.writeFileSync('./lib/setting.json', JSON.stringify(setting, null,2));
}
lolcatjs.options.seed = Math.round(Math.random() * 1000);
lolcatjs.options.colors = true;
const start = async (tobz = new Client()) => {
console.log('------------------------------------------------')
lolcatjs.fromString(color(figlet.textSync('ELAINA BOT', { horizontalLayout: 'full' })))
console.log('------------------------------------------------')
lolcatjs.fromString('[DEV] TOBZ')
lolcatjs.fromString('[SERVER] Server Started!')
tobz.onAnyMessage((fn) => messageLog(fn.fromMe, fn.type))
// Force it to keep the current session
tobz.onStateChanged((state) => {
console.log('[Client State]', state)
if (state === 'CONFLICT' || state === 'UNLAUNCHED') tobz.forceRefocus()
})
// listening on message
tobz.onMessage((async (message) => {
tobz.getAmountOfLoadedMessages() // Cut message Cache if cache more than 3K
.then((msg) => {
if (msg >= 1000) {
console.log('[CLIENT]', color(`Loaded Message Reach ${msg}, cuting message cache...`, 'yellow'))
tobz.cutMsgCache()
}
})
// msgHndlr(tobz, message)
// Message Handler (Loaded from recent cache)
require('./tobz.js')(tobz, message)
}))
tobz.onGlobalParicipantsChanged((async (heuh) => {
await welcome(tobz, heuh)
left(tobz, heuh)
}))
tobz.onAddedToGroup(async (chat) => {
if(isWhite(chat.id)) return tobz.sendText(chat.id, 'Halo aku Elaina, Ketik #help Untuk Melihat List Command Ku...')
if(mtcState === false){
const groups = await tobz.getAllGroups()
// BOT group count less than
if(groups.length > groupLimit){
await tobz.sendText(chat.id, 'Maaf, Batas group yang dapat Elaina tampung sudah penuh').then(async () =>{
tobz.deleteChat(chat.id)
tobz.leaveGroup(chat.id)
})
}else{
if(chat.groupMetadata.participants.length < memberLimit){
await tobz.sendText(chat.id, `Maaf, BOT keluar jika member group tidak melebihi ${memberLimit} orang`).then(async () =>{
tobz.deleteChat(chat.id)
tobz.leaveGroup(chat.id)
})
}else{
if(!chat.isReadOnly) tobz.sendText(chat.id, 'Halo aku Elaina, Ketik #help Untuk Melihat List Command Ku...')
}
}
}else{
await tobz.sendText(chat.id, 'Elaina sedang maintenance, coba lain hari').then(async () => {
tobz.deleteChat(chat.id)
tobz.leaveGroup(chat.id)
})
}
})
/*tobz.onAck((x => {
const { from, to, ack } = x
if (x !== 3) tobz.sendSeen(to)
}))*/
// listening on Incoming Call
tobz.onIncomingCall(( async (call) => {
await tobz.sendText(call.peerJid, 'Maaf, saya tidak bisa menerima panggilan. nelfon = block!.\nJika ingin membuka block harap chat Owner!')
.then(() => tobz.contactBlock(call.peerJid))
}))
}
/**
* Uncache if there is file change
* @param {string} module Module name or path
* @param {function} cb <optional>
*/
function nocache(module, cb = () => { }) {
console.log('Module', `'${module}'`, 'is now being watched for changes')
fs.watchFile(require.resolve(module), async () => {
await uncache(require.resolve(module))
cb(module)
})
}
/**
* Uncache a module
* @param {string} module Module name or path
*/
function uncache(module = '.') {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(module)]
resolve()
} catch (e) {
reject(e)
}
})
}
create(options(true, start))
.then(tobz => start(tobz))
.catch((error) => console.log(error))