-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.js
More file actions
187 lines (151 loc) · 4.71 KB
/
handler.js
File metadata and controls
187 lines (151 loc) · 4.71 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
186
187
import axios from 'axios';
import { jidNormalizedUser } from 'baileys';
import util from 'util';
import cp from 'child_process';
import Api from '#lib/api.js';
import Func from '#lib/function.js';
const exec = util.promisify(cp.exec).bind(cp);
const ownerCache = new Set();
const initOwnerCache = () => {
if (ownerCache.size === 0) {
ownerNumber.forEach(num => ownerCache.add(num));
}
};
const isOwnerUser = (sender, fromMe) => {
if (fromMe) return true;
initOwnerCache();
return ownerCache.has(sender.split('@')[0]);
};
const getGroupMetadata = (conn, chatId) => {
return conn.chats?.[chatId] || null;
};
const getParticipantRole = (participants, jid) => {
return participants?.find(u => u.id === jid)?.admin === 'admin';
};
export default async function Command(conn, m) {
if (m.isBot) return;
const isOwner = isOwnerUser(m.sender, m.fromMe);
if (!pubelik && !isOwner) return;
const quoted = m.isQuoted ? m.quoted : m;
const body = m.body;
const firstChar = body?.[0];
if (isOwner && body) {
if (firstChar === '>') {
return handleEval(m, conn, body[1] === '=');
}
if (firstChar === '$') {
return handleExec(m);
}
}
const isCommand = m.prefix && body?.startsWith(m.prefix);
if (!isCommand) {
return handlePluginEvents(conn, m, quoted, isOwner);
}
const command = m.command?.toLowerCase();
if (!command) return;
await handlePluginCommand(conn, m, quoted, isOwner, command);
}
async function handlePluginEvents(conn, m, quoted, isOwner) {
const ctx = createContext(conn, m, quoted, isOwner);
const pluginValues = Object.values(plugins);
for (const plugin of pluginValues) {
if (typeof plugin.on === 'function') {
try {
if (await plugin.on.call(conn, m, ctx)) return;
} catch (e) {
console.error(`[PLUGIN EVENT ERROR] ${plugin.name}:`, e.message);
}
}
}
}
async function handlePluginCommand(conn, m, quoted, isOwner, command) {
let metadata, isAdmin, isBotAdmin;
const pluginValues = Object.values(plugins);
for (const plugin of pluginValues) {
const isCmd = plugin.command?.includes(command) || plugin.alias?.includes(command);
if (!isCmd) continue;
try {
const settings = plugin.settings || {};
if (settings.owner && !isOwner) {
return m.reply(mess.owner);
}
if (settings.private && m.isGroup) {
return m.reply(mess.private);
}
if (settings.group && !m.isGroup) {
return m.reply(mess.group);
}
if (settings.admin || settings.botAdmin) {
if (!metadata) {
metadata = getGroupMetadata(conn, m.chat);
if (!metadata && m.isGroup) {
try {
metadata = await conn.groupMetadata(m.chat);
if (metadata) conn.chats[m.chat] = metadata;
} catch (e) {
console.error(`[METADATA ERROR] ${m.chat}:`, e.message);
}
}
if (metadata) {
const botJid = jidNormalizedUser(conn.user.id);
isAdmin = getParticipantRole(metadata.participants, m.sender);
isBotAdmin = getParticipantRole(metadata.participants, botJid);
}
}
if (settings.admin && !isAdmin) {
return m.reply(mess.admin);
}
if (settings.botAdmin && !isBotAdmin) {
return m.reply(mess.botAdmin);
}
}
if (settings.loading) m.reply(mess.wait);
const ctx = createContext(conn, m, quoted, isOwner, metadata, isAdmin, isBotAdmin);
await plugin.run(conn, m, ctx);
return;
} catch (e) {
console.error(`[PLUGIN ERROR] ${plugin.name}:`, e.message);
return m.reply('Terjadi error saat menjalankan command.');
}
}
}
function createContext(conn, m, quoted, isOwner, metadata = null, isAdmin = false, isBotAdmin = false) {
return {
Api,
Func,
downloadM: (filename) => conn.downloadMediaMessage(quoted, filename),
quoted,
metadata,
text: m.text,
isAdmin,
isBotAdmin,
isOwner
};
}
async function handleEval(m, conn, isAsync) {
const code = m.text;
let evalCmd;
try {
evalCmd = isAsync || /await/i.test(code)
? eval(`(async() => { ${code} })()`)
: eval(code);
} catch (e) {
return m.reply(util.format(e));
}
try {
const res = await Promise.resolve(evalCmd);
m.reply(util.format(res));
} catch (err) {
m.reply(util.format(err));
}
}
async function handleExec(m) {
const cmd = m.text.slice(1).trim();
try {
const { stdout = '', stderr = '' } = await exec(cmd, { timeout: 60000 });
const output = stdout.trim() || stderr.trim() || 'No output';
m.reply(output);
} catch (e) {
m.reply(util.format(e));
}
}