-
Notifications
You must be signed in to change notification settings - Fork 482
/
index.js
137 lines (126 loc) · 3.65 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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const {
makeWASocket,
fetchLatestBaileysVersion,
DisconnectReason,
useMultiFileAuthState,
makeCacheableSignalKeyStore,
makeInMemoryStore,
Browsers,
} = require("@whiskeysockets/baileys");
const fs = require("fs");
const Pino = require("pino");
const chalk = require("chalk");
const moment = require("moment-timezone");
moment.tz.setDefault("Asia/Jakarta").locale("id");
const { Messages } = require("./lib/messages.js");
const donet = "https://saweria.co/sansekai";
// Baileys
const Logger = {
level: "error",
};
const logger = Pino({
...Logger,
});
const Store = (log = logger) => {
const store = makeInMemoryStore({ logger: log });
return store;
};
const store = Store(logger);
store?.readFromFile("./session.json");
setInterval(() => {
store?.writeToFile("./session.json");
}, 10_000);
const color = (text, color) => {
return !color ? chalk.green(text) : chalk.keyword(color)(text);
};
async function connectToWhatsApp(use_pairing_code = false) {
const { state, saveCreds } = await useMultiFileAuthState("yusril");
const { version } = await fetchLatestBaileysVersion();
const sock = makeWASocket({
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
version: version,
logger: logger,
printQRInTerminal: true,
markOnlineOnConnect: true,
generateHighQualityLinkPreview: true,
browser: Browsers.macOS('Chrome'),
getMessage
});
store?.bind(sock.ev);
sock.ev.process(async (ev) => {
if (ev["creds.update"]) {
await saveCreds();
}
if (ev["connection.update"]) {
console.log("Connection update", ev["connection.update"]);
const update = ev["connection.update"];
const { connection, lastDisconnect } = update;
if (connection === "close") {
const shouldReconnect =
lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut;
console.log(
"connection closed due to ",
lastDisconnect.error,
", reconnecting ",
shouldReconnect
);
// reconnect if not logged out
if (shouldReconnect) {
connectToWhatsApp();
}
} else if (connection === "open") {
const botNumber = sock.user.id
console.log("opened connection");
console.log(color("Bot success conneted to server", "green"));
console.log(color("Donate for creator https://saweria.co/sansekai", "yellow"));
console.log(color("Type /menu to see menu"));
sock.sendMessage(botNumber, { text: `Bot started!\n\njangan lupa support ya bang :)\n${donet}` });
}
}
// sock.ev.on("messages.upsert", async (message) => {
// console.log(message);
// })
const upsert = ev["messages.upsert"];
if (upsert) {
if (upsert.type !== "notify") {
return;
}
const message = Messages(upsert, sock);
if (!message || message.sender === "status@broadcast") {
return;
}
// msgHandler(upsert, sock, store, message);
require("./sansekai.js")(upsert, sock, store, message);
}
// const message = Messages(upsert, sock);
// console.log(message);
// }
});
/**
*
* @param {import("@whiskeysockets/baileys").WAMessageKey} key
* @returns {import("@whiskeysockets/baileys").WAMessageContent | undefined}
*/
async function getMessage(key) {
if (store) {
const msg = await store.loadMessage(key.remoteJid, key.id);
return msg?.message || undefined;
}
// only if store is present
return proto.Message.fromObject({});
}
return sock;
}
connectToWhatsApp()
// Baileys
let file = require.resolve(__filename);
fs.watchFile(file, () => {
fs.unwatchFile(file);
console.log(chalk.redBright(`Update ${__filename}`));
delete require.cache[file];
require(file);
});