-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
293 lines (268 loc) · 9.41 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(async () => {
const {
default: makeWASocket,
useMultiFileAuthState,
jidNormalizedUser,
fetchLatestBaileysVersion,
Browsers,
proto,
makeInMemoryStore,
DisconnectReason,
delay,
generateWAMessage,
getAggregateVotesInPollMessage,
areJidsSameUser,
} = require("baileys");
const pino = require("pino");
const { Boom } = require("@hapi/boom");
const chalk = require("chalk");
const readline = require("node:readline");
const simple = require("./lib/simple.js");
const fs = require("node:fs");
const pkg = require("./package.json");
const moment = require("moment-timezone");
const Queque = require("./lib/queque.js");
const messageQueue = new Queque();
const Database = require("./lib/database.js");
const append = require("./lib/append");
const serialize = require("./lib/serialize.js");
const config = require("./settings.js");
const appenTextMessage = async (m, sock, text, chatUpdate) => {
let messages = await generateWAMessage(
m.key.remoteJid,
{
text: text,
},
{
quoted: m.quoted,
},
);
messages.key.fromMe = areJidsSameUser(m.sender, sock.user.id);
messages.key.id = m.key.id;
messages.pushName = m.pushName;
if (m.isGroup) messages.participant = m.sender;
let msg = {
...chatUpdate,
messages: [proto.WebMessageInfo.fromObject(messages)],
type: "append",
};
return sock.ev.emit("messages.upsert", msg);
};
const question = (text) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(text, resolve);
});
};
global.db = new Database(config.database + ".json");
await db.init();
global.pg = new (await require(process.cwd() + "/lib/plugins"))(
process.cwd() + "/system/plugins",
);
await pg.watch();
global.scraper = new (await require(process.cwd() + "/scrapers"))(
process.cwd() + "/scrapers/src",
);
await scraper.watch();
setInterval(async () => {
await db.save();
await pg.load();
await scraper.load();
}, 2000);
const store = makeInMemoryStore({
logger: pino().child({
level: "silent",
stream: "store",
}),
});
console.log(chalk.green.bold(`
--------------------------------------
☘️ Selamat datang di NekoBot
terimakasih telah menggunakan script ini 👍
--------------------------------------
`));
console.log(chalk.yellow.bold("📁 Inisialisasi modul..."));
console.log(chalk.cyan.bold("- API Baileys Telah Dimuat"));
console.log(chalk.cyan.bold("- Sistem File Siap Digunakan"));
console.log(chalk.cyan.bold("- Database Telah Diinisialisasi"));
console.log(chalk.blue.bold("\n🤖 Info Bot:"));
console.log(chalk.white.bold(" | GitHub: ") + chalk.cyan.bold("https://github.com/AxellNetwork"));
console.log(chalk.white.bold(" | Developer: ") + chalk.green.bold("AxellNetwork"));
console.log(chalk.white.bold(" | Status Server: ") + chalk.green.bold("Online"));
console.log(chalk.white.bold(" | Versi: ") + chalk.magenta.bold(pkg.version));
console.log(chalk.white.bold(" | Versi Node.js: ") + chalk.magenta.bold(process.version));
console.log(chalk.blue.bold("\n🔁 Memuat plugin dan scraper..."))
async function system() {
const { state, saveCreds } = await useMultiFileAuthState(config.sessions);
const sock = simple(
{
logger: pino({ level: "silent" }),
printQRInTerminal: false,
auth: state,
version: [2, 3000, 1019441105],
browser: Browsers.ubuntu("Edge"),
},
store,
);
store.bind(sock.ev);
if (!sock.authState.creds.registered) {
console.log(
chalk.white.bold(
"- Silakan masukkan nomor WhatsApp Anda, misalnya +628xxxx",
),
);
const phoneNumber = await question(chalk.green.bold(`– Nomor Anda: `));
const code = await sock.requestPairingCode(phoneNumber);
setTimeout(() => {
console.log(chalk.white.bold("- Kode Pairing Anda: " + code));
}, 3000);
}
//=====[ Pembaruan Koneksi ]======
sock.ev.on("connection.update", async (update) => {
const { connection, lastDisconnect } = update;
if (connection === "close") {
const reason = new Boom(lastDisconnect?.error)?.output.statusCode;
if (lastDisconnect.error == "Error: Stream Errored (unknown)") {
} else if (reason === DisconnectReason.badSession) {
console.log(
chalk.red.bold("File sesi buruk, Harap hapus sesi dan scan ulang"),
);
} else if (reason === DisconnectReason.connectionClosed) {
console.log(
chalk.yellow.bold("Koneksi ditutup, sedang mencoba untuk terhubung kembali..."),
);
} else if (reason === DisconnectReason.connectionLost) {
console.log(
chalk.yellow.bold("Koneksi hilang, mencoba untuk terhubung kembali..."),
);
} else if (reason === DisconnectReason.connectionReplaced) {
console.log(
chalk.green.bold("Koneksi diganti, sesi lain telah dibuka. Harap tutup sesi yang sedang berjalan."),
);
sock.logout();
} else if (reason === DisconnectReason.loggedOut) {
console.log(
chalk.green.bold("Perangkat logout, harap scan ulang."),
);
sock.logout();
} else if (reason === DisconnectReason.restartRequired) {
console.log(chalk.green.bold("Restart diperlukan, sedang memulai ulang..."));
system();
} else if (reason === DisconnectReason.timedOut) {
console.log(
chalk.green.bold("Koneksi waktu habis, sedang mencoba untuk terhubung kembali..."),
);
system();
}
} else if (connection === "connecting") {
console.log(chalk.blue.bold("Menghubungkan ke WhatsApp..."));
} else if (connection === "open") {
console.log(chalk.green.bold("Bot berhasil terhubung."));
}
});
//=====[ Setelah Pembaruan Koneksi ]========//
sock.ev.on("creds.update", saveCreds);
sock.ev.on("contacts.update", (update) => {
for (let contact of update) {
let id = jidNormalizedUser(contact.id);
if (store && store.contacts)
store.contacts[id] = {
...(store.contacts?.[id] || {}),
...(contact || {}),
};
}
});
sock.ev.on("contacts.upsert", (update) => {
for (let contact of update) {
let id = jidNormalizedUser(contact.id);
if (store && store.contacts)
store.contacts[id] = { ...(contact || {}), isContact: true };
}
});
sock.ev.on("groups.update", (updates) => {
for (const update of updates) {
const id = update.id;
if (store.groupMetadata[id]) {
store.groupMetadata[id] = {
...(store.groupMetadata[id] || {}),
...(update || {}),
};
}
}
});
sock.ev.on("group-participants.update", ({ id, participants, action }) => {
const metadata = store.groupMetadata[id];
if (metadata) {
switch (action) {
case "add":
case "revoked_membership_requests":
metadata.participants.push(
...participants.map((id) => ({
id: jidNormalizedUser(id),
admin: null,
})),
);
break;
case "demote":
case "promote":
for (const participant of metadata.participants) {
let id = jidNormalizedUser(participant.id);
if (participants.includes(id)) {
participant.admin = action === "promote" ? "admin" : null;
}
}
break;
case "remove":
metadata.participants = metadata.participants.filter(
(p) => !participants.includes(jidNormalizedUser(p.id)),
);
break;
}
}
});
async function getMessage(key) {
if (store) {
const msg = await store.loadMessage(key.remoteJid, key.id);
return msg;
}
return {
conversation: "NekoBot",
};
}
sock.ev.on("messages.upsert", async (cht) => {
if (cht.messages.length === 0) return;
const chatUpdate = cht.messages[0];
if (!chatUpdate.message) return;
const userId = chatUpdate.key.id;
global.m = await serialize(chatUpdate, sock, store);
if (m.isBot) return;
require("./lib/logger.js")(m);
if (!m.isOwner && db.list().settings.self) return;
await require("./system/handler.js")(m, sock, store);
});
sock.ev.on("messages.update", async (chatUpdate) => {
for (const { key, update } of chatUpdate) {
if (update.pollUpdates && key.fromMe) {
const pollCreation = await getMessage(key);
if (pollCreation) {
let pollUpdate = await getAggregateVotesInPollMessage({
message: pollCreation?.message,
pollUpdates: update.pollUpdates,
});
let toCmd = pollUpdate.filter((v) => v.voters.length !== 0)[0]
?.name;
console.log(toCmd);
await appenTextMessage(m, sock, toCmd, pollCreation);
await sock.sendMessage(m.cht, { delete: key });
} else return false;
return;
}
}
});
return sock;
}
system();
})();