forked from neoxr/neoxr-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
164 lines (146 loc) · 7.13 KB
/
client.js
File metadata and controls
164 lines (146 loc) · 7.13 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'
require('events').EventEmitter.defaultMaxListeners = 500
const { Baileys, MongoDB, PostgreSQL, Scandir, Function: Func } = new(require('@neoxr/wb'))
const spinnies = new(require('spinnies'))(),
fs = require('fs'),
path = require('path'),
colors = require('@colors/colors'),
stable = require('json-stable-stringify'),
env = require('./config.json')
const cache = new(require('node-cache'))({
stdTTL: env.cooldown
})
if (process.env.DATABASE_URL && /mongo/.test(process.env.DATABASE_URL)) MongoDB.db = env.database
const machine = (process.env.DATABASE_URL && /mongo/.test(process.env.DATABASE_URL)) ? MongoDB : (process.env.DATABASE_URL && /postgres/.test(process.env.DATABASE_URL)) ? PostgreSQL : new(require('./lib/system/localdb'))(env.database)
const client = new Baileys({
type: '--neoxr-v1',
plugsdir: 'plugins',
sf: 'session',
online: true,
version: [2, 2318, 11]
})
/* starting to connect */
client.on('connect', async res => {
/* load database */
global.db = {users:[], chats:[], groups:[], statistic:{}, sticker:{}, setting:{}, ...(await machine.fetch() ||{})}
/* save database */
await machine.save(global.db)
/* write connection log */
if (res && typeof res === 'object' && res.message) Func.logFile(res.message)
})
/* print error */
client.on('error', async error => {
console.log(colors.red(error.message))
if (error && typeof error === 'object' && error.message) Func.logFile(error.message)
})
/* bot is connected */
client.on('ready', async () => {
/* auto restart if ram usage is over */
const ramCheck = setInterval(() => {
var ramUsage = process.memoryUsage().rss
if (ramUsage >= require('bytes')(env.ram_limit)) {
clearInterval(ramCheck)
process.send('reset')
}
}, 60 * 1000)
/* create temp directory if doesn't exists */
if (!fs.existsSync('./temp')) fs.mkdirSync('./temp')
/* require all additional functions */
require('./lib/system/config'), require('./lib/system/baileys')(client.sock), require('./lib/system/functions'), require('./lib/system/scraper')
/* clear temp folder every 10 minutes */
setInterval(() => {
try {
const tmpFiles = fs.readdirSync('./temp')
if (tmpFiles.length > 0) {
tmpFiles.filter(v => !v.endsWith('.file')).map(v => fs.unlinkSync('./temp/' + v))
}
} catch {}
}, 60 * 1000 * 10)
/* save database send http-request every 30 seconds */
setInterval(async () => {
if (global.db) await machine.save(global.db)
if (process.env.CLOVYR_APPNAME && process.env.CLOVYR_URL && process.env.CLOVYR_COOKIE) {
const response = await axios.get(process.env.CLOVYR_URL, {
headers: {
referer: 'https://clovyr.app/view/' + process.env.CLOVYR_APPNAME,
cookie: process.env.CLOVYR_COOKIE
}
})
Func.logFile(`${await response.status} - Application wake-up!`)
}
}, 30_000)
})
/* print all message object */
client.on('message', ctx => require('./handler')(client.sock, ctx))
/* print deleted message object */
client.on('message.delete', ctx => {
const sock = client.sock
if (!ctx || ctx.origin.fromMe || ctx.origin.isBot || !ctx.origin.sender) return
if (cache.has(ctx.origin.sender) && cache.get(ctx.origin.sender) === 1) return
cache.set(ctx.origin.sender, 1)
if (ctx.origin.isGroup && global.db.groups.some(v => v.jid == ctx.origin.chat) && global.db.groups.find(v => v.jid == ctx.origin.chat).antidelete) return sock.copyNForward(ctx.origin.chat, ctx.delete)
})
/* AFK detector */
client.on('presence.update', update => {
if (!update) return
const sock = client.sock
const { id, presences } = update
if (id.endsWith('g.us')) {
for (let jid in presences) {
if (!presences[jid] || jid == sock.decodeJid(sock.user.id)) continue
if ((presences[jid].lastKnownPresence === 'composing' || presences[jid].lastKnownPresence === 'recording') && global.db && global.db.users && global.db.users.find(v => v.jid == jid) && global.db.users.find(v => v.jid == jid).afk > -1) {
sock.reply(id, `System detects activity from @${jid.replace(/@.+/, '')} after being offline for : ${Func.texted('bold', Func.toTime(new Date - global.db.users.find(v => v.jid == jid).afk))}\n\n➠ ${Func.texted('bold', 'Reason')} : ${global.db.users.find(v => v.jid == jid).afkReason ? global.db.users.find(v => v.jid == jid).afkReason : '-'}`, global.db.users.find(v => v.jid == jid).afkObj)
global.db.users.find(v => v.jid == jid).afk = -1
global.db.users.find(v => v.jid == jid).afkReason = ''
global.db.users.find(v => v.jid == jid).afkObj = {}
}
}
} else {}
})
client.on('group.add', async ctx => {
const sock = client.sock
const text = `Thanks +tag for joining into +grup group.`
const groupSet = global.db.groups.find(v => v.jid == ctx.jid)
try {
var pic = await Func.fetchBuffer(await sock.profilePictureUrl(ctx.member, 'image'))
} catch {
var pic = await Func.fetchBuffer(await sock.profilePictureUrl(ctx.jid, 'image'))
}
/* localonly to remove new member when the number not from indonesia */
if (groupSet && groupSet.localonly) {
if (global.db.users.some(v => v.jid == ctx.member) && !global.db.users.find(v => v.jid == ctx.member).whitelist && !ctx.member.startsWith('62') || !ctx.member.startsWith('62')) {
sock.reply(ctx.jid, Func.texted('bold', `Sorry @${ctx.member.split`@`[0]}, this group is only for indonesian people and you will removed automatically.`))
sock.updateBlockStatus(member, 'block')
return await Func.delay(2000).then(() => sock.groupParticipantsUpdate(ctx.jid, [ctx.member], 'remove'))
}
}
const txt = (groupSet && groupSet.text_welcome ? groupSet.text_welcome : text).replace('+tag', `@${ctx.member.split`@`[0]}`).replace('+grup', `${ctx.subject}`)
if (groupSet && groupSet.welcome) sock.sendMessageModify(ctx.jid, txt, null, {
largeThumb: true,
thumbnail: pic,
url: global.db.setting.link
})
})
client.on('group.remove', async ctx => {
const sock = client.sock
const text = `Good bye +tag :)`
const groupSet = global.db.groups.find(v => v.jid == ctx.jid)
try {
var pic = await Func.fetchBuffer(await sock.profilePictureUrl(ctx.member, 'image'))
} catch {
var pic = await Func.fetchBuffer(await sock.profilePictureUrl(ctx.jid, 'image'))
}
const txt = (groupSet && groupSet.text_left ? groupSet.text_left : text).replace('+tag', `@${ctx.member.split`@`[0]}`).replace('+grup', `${ctx.subject}`)
if (groupSet && groupSet.left) sock.sendMessageModify(ctx.jid, txt, null, {
largeThumb: true,
thumbnail: pic,
url: global.db.setting.link
})
})
client.on('caller', ctx => {
if (typeof ctx === 'boolean') return
client.sock.updateBlockStatus(ctx.jid, 'block')
})
// client.on('group.promote', ctx => console.log(ctx))
// client.on('group.demote', ctx => console.log(ctx))