-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhandler.js
More file actions
185 lines (162 loc) · 7.74 KB
/
handler.js
File metadata and controls
185 lines (162 loc) · 7.74 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import yourApi from 'your-api'
const { func } = yourApi
import { plugins } from './lib/plugins.js'
import { owner, defaultPrefix } from './setting.js'
import { decodeJid } from './lib/src/func.js'
import { printLog } from './lib/src/print.js'
import store from './lib/src/store.js'
import db from './lib/database.js'
const handler = async (m, conn) => {
try {
const setting = db.settings.get(conn.user.jid)
const prefixList = (setting && setting.prefix.length) ? setting.prefix : defaultPrefix
let prefix = ""
for (const p of prefixList) {
const trimmedPrefix = p.trim()
if (trimmedPrefix === '' || m.text.startsWith(trimmedPrefix)) {
prefix = trimmedPrefix
break
}
}
const isUsingPrefix = prefix !== false
if (!isUsingPrefix && !setting.usePrefix) {
} else if (!prefix) {
return
}
const trimText = m.text.slice(prefix.length).trim()
const [rawCommand, ...args] = trimText.split(/\s+/)
const command = rawCommand ? rawCommand.toLowerCase() : rawCommand
const text = command ? trimText.slice(rawCommand.length).trim() : trimText
const isGroup = m.from.endsWith('@g.us')
const isPrivate = m.from.endsWith('@s.whatsapp.net')
const isBroadcast = m.from === 'status@broadcast'
const isOwner = [conn.user.jid, ...owner.map(([number]) => number.replace(/[^0-9]/g, '') + '@s.whatsapp.net')].includes(m.sender)
const isRegistered = db.users.exist(m.sender)
const isNsfw = isGroup ? db.groups.get(m.from).setting?.nsfw : true
const isBaileys = m.id.startsWith('3EB0')
const groupMetadata = isGroup ? await conn.groupMetadata(m.from) : {}
const groupName = groupMetadata.subject || ''
const participants = groupMetadata.participants || []
const user = isGroup ? participants.find(u => decodeJid(u.id) === m.sender) : {}
const bot = isGroup ? participants.find(b => decodeJid(b.id) === conn.user.jid) : {}
const isSuperAdmin = user?.admin === 'superadmin' || false
const isAdmin = isSuperAdmin || user?.admin === 'admin' || false
const isBotAdmin = bot?.admin === 'admin' || false
let isCommand = false
if (!db.groups.exist(m.from) && isGroup) {
await db.groups.add(m.from)
await db.save()
}
if (db.groups.exist(m.from) && isRegistered) {
const group = db.groups.get(m.from)
await group.users.add(m.sender)
await db.save()
}
if (setting.mode === 'public' || (setting.mode === 'self' && isOwner)) {
if (Array.isArray(plugins.befores)) {
for (const before of plugins.befores) {
const name = Object.keys(before)[0]
try {
await before[name].start({
m, conn, text, args, status,
isGroup, store, isPrivate, isBroadcast, isOwner, isRegistered, isSuperAdmin, isAdmin, isBotAdmin, isBaileys,
groupMetadata, groupName, participants, db, plugins
})
} catch (e) {
console.log(func.colors(e, 'bgRed'))
if (e.name) {
if (before[name].setting?.error_react) await m.react('❌')
await m.reply(`*${e.name}* : ${e.message}`)
}
}
}
}
if (!isBaileys && !isBroadcast) {
const stickerCommand = (m.type === 'stickerMessage'
? db.stickers.get(Buffer.from(m.message[m.type].fileSha256).toString('base64'))?.command
: ''
)
const commands = plugins.commands
.map(plugin => Object.values(plugin)[0])
.filter(commandObj => commandObj.command.some(cmd =>
cmd.toLowerCase() === stickerCommand || cmd.toLowerCase() === command
))
if (commands.length > 0) {
isCommand = true
for (const cmd of commands) {
const setting = {
isRegister: false,
isNsfw: false,
isGroup: false,
isPrivate: false,
isOwner: false,
isSuperAdmin: false,
isAdmin: false,
isBotAdmin: false,
usePrefix: cmd.setting?.usePrefix !== undefined ? cmd.setting.usePrefix : true,
...cmd.setting
}
if (setting.isRegister && !isRegistered) {
await status({ type: 'isRegister', m, prefix })
continue
}
if (setting.isNsfw && !isNsfw) {
await status({ type: 'isNsfw', m })
continue
}
if (setting.isGroup && !isGroup) {
await status({ type: 'isGroup', m })
continue
}
if (setting.isPrivate && !isPrivate) {
await status({ type: 'isPrivate', m })
continue
}
if (setting.isOwner && !isOwner) {
await status({ type: 'isOwner', m })
continue
}
if (setting.isAdmin && !isAdmin) {
await status({ type: 'isAdmin', m })
continue
}
if (setting.isBotAdmin && !isBotAdmin) {
await status({ type: 'isBotAdmin', m })
continue
}
try {
await cmd.start({
m, conn, text, store, args, prefix, command, status,
isGroup, isPrivate, isOwner, isRegistered, isSuperAdmin, isAdmin, isBotAdmin,
groupMetadata, groupName, participants, db, plugins
})
} catch (e) {
console.log(func.colors(e, 'bgRed'))
if (e.name) {
if (cmd.setting?.error_react) await m.react('❌')
await m.reply(`*${e.name}* : ${e.message}`)
}
}
}
}
}
}
await printLog({ m, conn, args, command, groupName, isGroup, isCommand })
} catch (e) {
console.log(func.colors(e, 'bgRed'))
}
}
const status = ({ type, m, prefix = '' }) => {
const texts = {
isRegister: `Untuk menggunakan Perintah ini, Anda harus terdaftar di database.\n\n*Contohnya* \n\n1. ${prefix}reg <nama pengguna>\n2. ${prefix}reg asep`,
isNsfw: 'Konten NSFW tidak tersedia untuk grup ini.',
isOwner: 'Perintah ini hanya Owner Bot.',
isGroup: 'Perintah ini hanya berlaku di dalam group.',
isPrivate: 'Perintah ini hanya berlaku di private chat.',
isAdmin: 'Perintah ini hanya berlaku untuk admin group.',
isBotAdmin: 'Perintah ini hanya berlaku saat Bot menjadi admin.'
}
const text = texts[type]
if (text) return m.reply(text)
}
export default handler