-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (112 loc) · 3.13 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
const wa = require("@open-wa/wa-automate");
const config = require("./config.json");
const fs = require("fs");
const path = require("path");
wa.create({
sessionId: config.name,
multiDevice: true,
authTimeout: 0,
headless: "new",
hostNotificationLang: "ID_ID",
qrTimeout: 0,
restartOnCrash: start,
useChrome: true,
killProcessOnBrowserClose: true,
throwErrorOnTosBlock: false,
chromiumArgs: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--aggressive-cache-discard",
"--disable-cache",
"--disable-application-cache",
"--disable-offline-load-stale-cache",
"--disk-cache-size=0",
],
})
.then((client) => start(client))
.catch((error) => {
console.error("Error creating client:", error);
process.exit(1);
});
function start(client) {
client.onStateChanged((state) => {
console.log("[Client State]", state);
if (state === "CONFLICT" || state === "UNLAUNCHED")
client.forceRefocus();
});
client.onAck((x) => {
const { from, to, ack } = x;
if (x !== 3) client.sendSeen(to);
});
client.onAddedToGroup(async (chat) => {
try {
await client.sendText(
chat.groupMetadata.id,
`🔰 *${config.name}* 🔰\n==================\nHalo, saya adalah bot yang dibuat oleh \`${config.owner}\`\n\nKetik \`${config.prefix}menu\` untuk melihat menu`
);
} catch (error) {
console.error("Error sending welcome message to group:", error);
}
});
client.onMessage(async (message) => {
try {
client.getAmountOfLoadedMessages().then((msg) => {
if (msg >= 3000) {
client.cutMsgCache();
}
});
} catch (error) {
console.error("Error delete message cache:", error);
}
try {
const commandFiles = getCommandFiles(message.type);
for (const file of commandFiles) {
const command = require(`./commands/${message.type}/${file}`);
const commandName = file.split(".")[0];
if (isValidCommand(message, commandName)) {
client.simulateTyping(message.from, true);
if (config.log)
console.log(`${commandName} - ${message.from}`);
await handleCommand(client, message, command);
client.simulateTyping(message.from, false);
break;
}
}
} catch (error) {
console.error("Error processing message:", error);
}
});
}
function getCommandFiles(type) {
let commandFiles = [];
try {
const dir = path.join(__dirname, `commands/${type}`);
if (fs.existsSync(dir)) {
commandFiles = fs
.readdirSync(dir)
.filter((file) => file.endsWith(".js"));
}
} catch (error) {
console.error(`Error reading command files for type ${type}:`, error);
}
return commandFiles;
}
function isValidCommand(message, commandName) {
const prefixCommand = `${config.prefix}${commandName}`;
const regex = new RegExp(`^${prefixCommand}\\b`);
return (
(message.type === "image" &&
message.caption &&
message.caption.match(regex)) ||
(message.type === "chat" && message.body && message.body.match(regex))
);
}
async function handleCommand(client, message, command) {
await client.simulateTyping(message.from, true);
try {
await command(client, message);
} catch (error) {
console.error("Error executing command:", error);
}
await client.simulateTyping(message.from, false);
}