forked from tichnas/WhatsApp-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.js
130 lines (107 loc) · 3.27 KB
/
Bot.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
const makeWASocket = require("@whiskeysockets/baileys").default;
const {
DisconnectReason,
useMultiFileAuthState,
} = require("@whiskeysockets/baileys");
const P = require("pino");
class Bot {
#socket;
#messageStore = {};
#emptyChar = " ";
#authFolder;
#selfReply;
#saveCredentials;
#logMessages;
#plugins;
constructor(plugins = [], config = {}) {
this.#plugins = plugins;
this.#authFolder = config.authFolder || "auth";
this.#selfReply = config.selfReply || false;
this.#logMessages = config.logMessages || true;
}
async connect() {
const { state, saveCreds } = await useMultiFileAuthState(this.#authFolder);
this.#saveCredentials = saveCreds;
this.#socket = makeWASocket({
printQRInTerminal: true,
auth: state,
getMessage: this.#getMessageFromStore,
logger: P({ level: "error" }),
downloadHistory: false,
});
this.#plugins.forEach((plugin) =>
plugin.init(this.#socket, this.#getText, this.#sendMessage)
);
}
async run() {
this.#socket.ev.process(async (events) => {
if (events["connection.update"]) {
const update = events["connection.update"];
const { connection, lastDisconnect } = update;
if (connection === "close") {
// reconnect if not logged out
if (
lastDisconnect?.error?.output?.statusCode ===
DisconnectReason.loggedOut
) {
console.log("Connection closed. You are logged out.");
} else if (
lastDisconnect?.error?.output?.statusCode ===
DisconnectReason.timedOut
) {
console.log(
new Date().toLocaleTimeString(),
"Timed out. Will retry in 1 minute."
);
setTimeout(this.#restart.bind(this), 60 * 1000);
} else {
this.#restart();
}
}
}
if (events["creds.update"]) {
await this.#saveCredentials();
}
if (events["messages.upsert"]) {
const { messages } = events["messages.upsert"];
if (this.#logMessages) console.log("msg upsert", messages);
messages.forEach(async (msg) => {
const { key, message } = msg;
if (!message || this.#getText(key, message).includes(this.#emptyChar))
return;
this.#plugins.forEach((plugin) => plugin.process(key, message));
});
}
});
}
async #restart() {
await this.connect();
await this.run();
}
#getMessageFromStore = (key) => {
const { id } = key;
if (this.#messageStore[id]) return this.#messageStore[id].message;
};
#getText(key, message) {
try {
let text = message.conversation || message.extendedTextMessage.text;
if (key.participant) {
const me = key.participant.slice(0, 12);
text = text.replace(/\@me\b/g, `@${me}`);
}
return text;
} catch (err) {
return "";
}
}
#sendMessage = async (jid, content, ...args) => {
try {
if (!this.#selfReply) content.text = content.text + this.#emptyChar;
const sent = await this.#socket.sendMessage(jid, content, ...args);
this.#messageStore[sent.key.id] = sent;
} catch (err) {
console.log("Error sending message", err);
}
};
}
module.exports = Bot;