forked from tuhinpal/WhatsBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
140 lines (124 loc) · 3.88 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
//jshint esversion:8
const express = require("express");
const app = express();
const { Client, LegacySessionAuth } = require("whatsapp-web.js");
const pmpermit = require("./helpers/pmpermit");
const config = require("./config");
const fs = require("fs");
const logger = require("./logger");
const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LegacySessionAuth({
session: config.session,
}),
});
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", () => {
console.log("Bot has been started");
});
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);
}
}
}
});
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 logger(
client,
`User ${
otherChat.name || otherChat.number
} is automatically permitted for message !`
);
}
}
} 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}`);
});