-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
174 lines (160 loc) · 4.92 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
const {
default: makeWASocket,
useMultiFileAuthState,
Browsers,
delay,
DisconnectReason,
} = require("@whiskeysockets/baileys");
const path = require("path");
const { Image, Message, Sticker, Video } = require("./lib/Messages");
let fs = require("fs");
let config = require("./config");
const pino = require("pino");
logger = pino({
level: "silent",
});
const plugins = require("./lib/plugins");
const { serialize, Greetings } = require("./lib");
fs.readdirSync(__dirname + "/assets/database/").forEach((db) => {
if (path.extname(db).toLowerCase() == ".js") {
require(__dirname + "/assets/database/" + db);
}
});
const connect = async () => {
console.log("X-Asena");
console.log("Syncing Database");
config.DATABASE.sync();
console.log("⬇ Installing Plugins...");
fs.readdirSync(__dirname + "/plugins").forEach((plugin) => {
if (path.extname(plugin).toLowerCase() == ".js") {
require(__dirname + "/plugins/" + plugin);
}
});
console.log("✅ Plugins Installed!");
const Xasena = async () => {
const { state, saveCreds } = await useMultiFileAuthState(
__dirname + "/session"
);
let conn = makeWASocket({
auth: state,
printQRInTerminal: true,
logger: pino({
level: "silent",
}),
browser: Browsers.macOS("Desktop"),
downloadHistory: false,
syncFullHistory: false,
});
conn.ev.on("connection.update", async (s) => {
const { connection, lastDisconnect } = s;
if (connection === "connecting") {
console.log("ℹ Connecting to WhatsApp... Please Wait.");
}
if (connection === "open") {
console.log("✅ Login Successful!");
let str = `\`\`\`X-asena connected \nversion : ${
require(__dirname + "/package.json").version
}\nTotal Plugins : ${plugins.commands.length}\nWorktype: ${
config.WORK_TYPE
}\`\`\``;
conn.sendMessage(conn.user.id, {
text: str,
});
}
if (connection === "close") {
const { error, message } = lastDisconnect.error?.output.payload;
if (
lastDisconnect.error?.output?.statusCode !==
DisconnectReason.loggedOut
) {
await delay(300);
Xasena();
console.log("reconnecting...");
} else {
console.log("connection closed\nDevice logged out.");
await delay(3000);
process.exit(0);
}
}
});
conn.ev.on("creds.update", saveCreds);
conn.ev.on("group-participants.update", async (data) => {
Greetings(data, conn);
});
conn.ev.on("messages.upsert", async (m) => {
if (m.type !== "notify") return;
let msg = await serialize(
JSON.parse(JSON.stringify(m.messages[0])),
conn
);
if (!msg) return;
let text_msg = msg.body;
if (text_msg && config.LOGS)
console.log(
`At : ${
msg.from.endsWith("@g.us")
? (await conn.groupMetadata(msg.from)).subject
: msg.from
}\nFrom : ${msg.sender}\nMessage:${text_msg}`
);
plugins.commands.map(async (command) => {
if (
command.fromMe &&
!config.SUDO.split(",").includes(
msg.sender.split("@")[0] || !msg.isSelf
)
) {
return;
}
let comman = text_msg
? text_msg[0].toLowerCase() + text_msg.slice(1).trim()
: "";
msg.prefix = new RegExp(config.HANDLERS).test(text_msg)
? text_msg[0].toLowerCase()
: ",";
let whats;
switch (true) {
case command.pattern && command.pattern.test(comman):
let match;
try {
match = text_msg
.replace(new RegExp(command.pattern, "i"), "")
.trim();
} catch {
match = false;
}
whats = new Message(conn, msg);
command.function(whats, match, msg, conn);
break;
case text_msg && command.on === "text":
whats = new Message(conn, msg);
command.function(whats, text_msg, msg, conn, m);
break;
case command.on === "image" || command.on === "photo":
if (msg.type === "imageMessage") {
whats = new Image(conn, msg);
command.function(whats, text_msg, msg, conn, m);
}
break;
case command.on === "sticker":
if (msg.type === "stickerMessage") {
whats = new Sticker(conn, msg);
command.function(whats, msg, conn, m);
}
break;
case command.on === "video":
if (msg.type === "videoMessage") {
whats = new Video(conn, msg);
command.function(whats, msg, conn, m);
}
break;
default:
break;
}
});
});
return conn;
};
Xasena();
};
connect();