-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
main.js
164 lines (146 loc) · 4.63 KB
/
main.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
//jshint esversion:11
const express = require("express");
const app = express();
const { Client, LocalAuth } = require("whatsapp-web.js");
const pmpermit = require("./helpers/pmpermit");
const config = require("./config");
const fs = require("fs");
const logger = require("./logger");
const { afkHandler } = require("./helpers/afkWrapper");
const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LocalAuth({ clientId: "whatsbot" }),
});
client.commands = new Map();
fs.readdir("./commands", (err, files) => {
if (err) return console.error(e);
files.forEach((commandFile) => {
if (commandFile.endsWith(".js")) {
let commandName = commandFile.replace(".js", "");
const command = require(`./commands/${commandName}`);
client.commands.set(commandName, command);
}
});
});
client.initialize();
client.on("auth_failure", () => {
console.error(
"There is a problem in authentication, Kindly set the env var again and restart the app"
);
});
client.on("ready", async () => {
console.log("Bot has been started");
try {
await logger(client, "Bot has been started");
} catch (err) {
console.log(err);
}
});
client.on("message", async (msg) => {
if (!msg.author && config.pmpermit_enabled === "true") {
// Pm check for pmpermit module
var checkIfAllowed = await pmpermit.handler(msg.from.split("@")[0]); // get status
if (!checkIfAllowed.permit) {
// if not permitted
if (checkIfAllowed.block) {
await msg.reply(checkIfAllowed.msg);
setTimeout(async () => {
await (await msg.getContact()).block();
}, 3000);
} else if (!checkIfAllowed.block) {
msg.reply(checkIfAllowed.msg);
}
} else {
await checkAndApplyAfkMode();
}
}
if (!msg.author && config.pmpermit_enabled !== "true") {
await checkAndApplyAfkMode();
}
async function checkAndApplyAfkMode() {
const contact = await msg.getContact();
const afkData = await afkHandler(contact?.name || contact?.pushname);
if (afkData?.notify) {
//if user is afk
const { reason, timediff } = afkData;
let lastseen = "";
lastseen += timediff[0] ? `${timediff[0]} days ` : "";
lastseen += timediff[1] ? `${timediff[1]} hrs ` : "";
lastseen += timediff[2] ? `${timediff[2]} min ` : "";
lastseen += `${timediff[3]} sec ago`;
await msg.reply(
`${afkData.msg}\n\n😊😊😊\n\nI am currently offline...\n\n*Reason*: ${reason}\n*Last Seen*:${lastseen}`
);
}
}
});
client.on("message_create", async (msg) => {
// auto pmpermit
try {
if (config.pmpermit_enabled == "true") {
var otherChat = await (await msg.getChat()).getContact();
if (
msg.fromMe &&
msg.type !== "notification_template" &&
otherChat.isUser &&
!(await pmpermit.isPermitted(otherChat.number)) &&
!otherChat.isMe &&
!msg.body.startsWith("!") &&
!msg.body.endsWith("_Powered by WhatsBot_")
) {
await pmpermit.permit(otherChat.number);
await msg.reply(
`You are automatically permitted for message !\n\n_Powered by WhatsBot_`
);
}
}
} catch (ignore) {}
if (msg.fromMe && msg.body.startsWith("!")) {
let args = msg.body.slice(1).trim().split(/ +/g);
let command = args.shift().toLowerCase();
console.log({ command, args });
if (client.commands.has(command)) {
try {
await client.commands.get(command).execute(client, msg, args);
} catch (error) {
console.log(error);
}
} else {
await client.sendMessage(
msg.to,
"No such command found. Type !help to get the list of available commands"
);
}
}
});
client.on("message_revoke_everyone", async (after, before) => {
if (before) {
if (
before.fromMe !== true &&
before.hasMedia !== true &&
before.author == undefined &&
config.enable_delete_alert == "true"
) {
client.sendMessage(
before.from,
"_You deleted this message_ 👇👇\n\n" + before.body
);
}
}
});
client.on("disconnected", (reason) => {
console.log("Client was logged out", reason);
});
app.get("/", (req, res) => {
res.send(
'<h1>This server is powered by Whatsbot<br><a href="https://github.com/tuhinpal/WhatsBot">https://github.com/tuhinpal/WhatsBot</a></h1>'
);
});
app.use(
"/public",
express.static("public"),
require("serve-index")("public", { icons: true })
); // public directory will be publicly available
app.listen(process.env.PORT || 8080, () => {
console.log(`Server listening at Port: ${process.env.PORT || 8080}`);
});