-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
243 lines (228 loc) · 7.82 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
const P = require("pino");
const {
default: makeWASocket,
DisconnectReason,
AnyMessageContent,
delay,
proto,
jidDecode,
useSingleFileAuthState,
getContentType,
generateForwardMessageContent,
generateWAMessageFromContent,
downloadContentFromMessage,
generateMessageID,
makeInMemoryStore,
} = require("@adiwajshing/baileys");
const baileys = require("@adiwajshing/baileys");
global.axios = require("axios");
const { Boom } = require("@hapi/boom");
const fs = require("fs");
const conf = require("./config.json");
const { User } = require("./models/db.js");
const { getNo } = require("./models/func.js");
const { state, loadState, saveState } = useSingleFileAuthState("./state.json");
const availableCommands = new Set();
const getVersionWaweb = () => {
let version;
try {
let { data } = axios.get(
"https://web.whatsapp.com/check-update?version=1&platform=web"
);
version = [data.currentVersion.replace(/[.]/g, ", ")];
} catch {
version = [2, 2214, 12];
}
return version;
};
const startSock = () => {
var whats = makeWASocket({
logger: P({ level: "fatal" }),
printQRInTerminal: true,
auth: state,
browser: ["Whatsactyl Bot", "Safari", "1.0.0"],
version: getVersionWaweb() || [2, 2214, 12],
});
let args;
let command;
// command handler
const commandFolders = fs.readdirSync("./commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".js"));
fs.readdir(`./commands/${folder}`, (e, files) => {
if (e) return console.error(e);
files.forEach((commandFile) => {
availableCommands.add(commandFile.replace(".js", ""));
console.log(`Loaded - ${commandFile}`);
});
});
}
whats.ev.on("messages.upsert", async (m) => {
try {
var msg = m.messages[0];
exports.msg = msg;
if (!m || !msg.message) return;
if (msg.key && msg.key.remoteJid === "status@broadcast") return;
global.from = msg.key.remoteJid;
const content = JSON.stringify(msg.message);
const type = getContentType(msg.message);
const prefix = conf.prefix;
const ownerNumber = conf.ownerNumber;
const text =
type === "conversation" && msg.message.conversation
? msg.message.conversation
: type == "imageMessage" && msg.message.imageMessage.caption
? msg.message.imageMessage.caption || ""
: type == "videoMessage" && msg.message.videoMessage.caption
? msg.message.videoMessage.caption
: type == "extendedTextMessage" &&
msg.message.extendedTextMessage.text
? msg.message.extendedTextMessage.text
: "";
const isGroup = from.endsWith("@g.us");
const senderp = isGroup ? msg.key.participant : msg.key.remoteJid;
var dy =
type === "conversation" && msg.message.conversation
? msg.message.conversation
: type == "imageMessage" && msg.message.imageMessage.caption
? msg.message.imageMessage.caption
: type == "documentMessage" && msg.message.documentMessage.caption
? msg.message.documentMessage.caption
: type == "videoMessage" && msg.message.videoMessage.caption
? msg.message.videoMessage.caption
: type == "extendedTextMessage" &&
msg.message.extendedTextMessage.text
? msg.message.extendedTextMessage.text
: type == "buttonsResponseMessage" &&
msg.message.buttonsResponseMessage.selectedButtonId
? msg.message.buttonsResponseMessage.selectedButtonId
: type == "templateButtonReplyMessage" &&
msg.message.templateButtonReplyMessage.selectedId
? msg.message.templateButtonReplyMessage.selectedId
: "";
const argsp = text.trim().split(/ +/g).slice(1);
const q = argsp.join(" ");
global.isOwner = [whats.user.id, ...ownerNumber]
.map((v) => v.replace(/[^0-9]/g, "") + "@s.whatsapp.net")
.includes(senderp.replace(":14", ""));
//Auto Read
if (conf.autoread) {
whats.sendReadReceipt(from, msg.key.participant, [msg.key.id]);
}
//Function
global.reply = async (iy) => {
whats.sendMessage(msg.key.remoteJid, { text: iy }, { quoted: msg });
};
global.replyWithImgAndCap = async (link, cap) => {
whats.sendMessage(
msg.key.remoteJid,
{ image: { url: link }, caption: cap },
{ quoted: msg }
);
};
global.error = async (info) => {
whats.sendMessage(
msg.key.remoteJid,
{ text: "❌ | Something error here, " + info },
{ quoted: msg }
);
};
try {
if (dy.startsWith(conf.prefix)) {
args = dy.slice(conf.prefix.length).trim().split(/ +/g);
command = args.shift().toLowerCase();
sender = msg.pushName;
} else {
return;
}
} catch {}
var afterArgs = args.slice(1).join(" ");
if (availableCommands.has(command)) {
if (fs.existsSync(`${__dirname}/commands/util/${command}.js`)) {
require(`${__dirname}/commands/util/${command}.js`).run(
whats,
msg,
args,
q,
text,
afterArgs
);
} else if (
fs.existsSync(`${__dirname}/commands/application/${command}.js`)
) {
require(`${__dirname}/commands/application/${command}.js`).run(
whats,
msg,
args,
q,
text,
afterArgs
);
} else if (
fs.existsSync(`${__dirname}/commands/client/${command}.js`)
) {
require(`${__dirname}/commands/client/${command}.js`).run(
whats,
msg,
args,
q,
text,
afterArgs
);
} else if (fs.existsSync(`${__dirname}/commands/owner/${command}.js`)) {
require(`${__dirname}/commands/owner/${command}.js`).run(
whats,
msg,
args,
q,
text,
afterArgs
);
}
}
} catch (e) {
console.log(e);
}
});
whats.ev.on("connection.update", async (update) => {
const { connection, lastDisconnect } = update;
if (connection === "close") {
let reason = lastDisconnect.error
? new Boom(lastDisconnect)?.output.statusCode
: 0;
if (reason === DisconnectReason.badSession) {
console.log(`Bad Session File, Please Delete Session and Scan Again`);
startSock();
} else if (reason === DisconnectReason.connectionClosed) {
console.log("[WA SYSTEM] Connection closed, reconnecting....");
startSock();
} else if (reason === DisconnectReason.connectionLost) {
console.log("[WA SYSTEM] Connection Lost from Server, reconnecting...");
startSock();
} else if (reason === DisconnectReason.connectionReplaced) {
console.log(
"[WA SYSTEM] Connection Replaced, Another New Session Opened, Please Close Current Session First"
);
process.exit();
} else if (reason === DisconnectReason.loggedOut) {
console.log(
`[WA SYSTEM] Device Logged Out, Please Delete Session and Scan Again.`
);
process.exit();
} else if (reason === DisconnectReason.restartRequired) {
console.log("[WA SYSTEM] Restart Required, Restarting...");
startSock();
} else if (reason === DisconnectReason.timedOut) {
console.log("[WA SYSTEM] Connection TimedOut, Reconnecting...");
startSock();
} else {
console.log(`Unknown DisconnectReason: ${reason}|${connection}`);
}
}
console.log("Connected!", update);
});
whats.ev.on("creds.update", saveState);
};
startSock();