-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
193 lines (181 loc) · 5.86 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
const wa = require("@open-wa/wa-automate");
const { create, decryptMedia, ev } = wa;
const { default: PQueue } = require("p-queue");
const fs = require("fs");
const express = require("express");
const axios = require("axios").default;
const helpOnInPM = ["hello", "hi", "hii", "hey", "heyy", "#help", "#menu"];
const helpOnInGroup = ["#help", "#menu"];
const helpText =
process.env.HELP_TEXT ||
`Commands:
#sticker: write in caption of a image/video/gif to turn it into sticker
#spam: tag everyone in a message in a group (only works in a group)
#join https://chat.whatsapp.com/shdkashdh: joing a group with invite link
#leave: i hope you dont use this (only works in a group if sent by an admin)
#help: to recive this same message
#menu: same as help but some people prefer it
#run languages: Returns all languages supported
#run {language}
{code}: Run some code in some language
eg.
'#run node
console.log('hello world');'
Add '#nospam' in group description to stop spam commands
All commands except #spam & #leave work in pm
Made by: pathetic_geek (https://github.com/patheticGeek)
`;
const leaveText =
process.env.LEAVE_TEXT ||
"Ab unko humshe rishta nhi rakhna hai\nto humari taraf se bhi koi zabardasti nhi hai";
const server = express();
const PORT = parseInt(process.env.PORT) || 3000;
const queue = new PQueue({
concurrency: 2,
autoStart: false,
});
/**
* WA Client
* @type {null | import("@open-wa/wa-automate").Client}
*/
let cl = null;
/**
* Process the message
* @param {import("@open-wa/wa-automate").Message} message
*/
async function procMess(message) {
if (message.type === "chat") {
if (
message.isGroupMsg &&
helpOnInGroup.includes(message.body.toLowerCase())
) {
await cl.sendText(message.from, helpText);
} else if (
!message.isGroupMsg &&
helpOnInPM.includes(message.body.toLowerCase())
) {
await cl.sendText(message.from, helpText);
} else if (message.isGroupMsg && message.body.toLowerCase() === "#spam") {
if (
message.chat.groupMetadata.desc &&
message.chat.groupMetadata.desc.includes("#nospam")
) {
await cl.sendText(message.chatId, "Spam protected group");
} else {
const text = `hello ${message.chat.groupMetadata.participants.map(
(participant) =>
`\n🌚 @${
typeof participant.id === "string"
? participant.id.split("@")[0]
: participant.user
}`
)}`;
await cl.sendTextWithMentions(message.chatId, text);
}
} else if (message.body === "#run languages") {
const response = await axios.get(
"https://emkc.org/api/v1/piston/versions"
);
const reply = response.data
.map((item) => `${item.name} - v${item.version}`)
.join("\n");
cl.sendText(message.chatId, reply);
} else if (message.body.startsWith("#run ")) {
const { chatId, body } = message;
try {
let msg = body.replace("#run ", "").split("\n");
const lang = msg.splice(0, 1)[0];
const source = msg.join("\n");
const response = await axios.post(
"https://emkc.org/api/v1/piston/execute",
{
language: lang,
source: source,
}
);
const { ran, language, output, version, code, message } = response.data;
const reply = `${
ran ? "Ran" : "Error running"
} with ${language} v${version}\nOutput:\n${output}`;
cl.sendText(chatId, reply);
} catch (e) {
console.log(e);
cl.sendText(chatId, "Unsupported language");
}
} else if (message.body.startsWith("#join https://chat.whatsapp.com/")) {
await cl.joinGroupViaLink(message.body);
await cl.reply(message.chatId, "Joined group", message.id);
} else if (message.body.toLowerCase() === "#nospam") {
await cl.reply(
message.chatId,
"Add #nospam in group description",
message.id
);
} else if (message.isGroupMsg && message.body.toLowerCase() === "#leave") {
const user = message.chat.groupMetadata.participants.find(
(pat) => pat.id === message.author
);
if (user && user.isAdmin) {
await cl.sendText(message.chatId, leaveText);
await cl.leaveGroup(message.chat.id);
} else {
await cl.reply(message.chatId, "You're not an admin!", message.id);
}
}
} else if (
["image", "video"].includes(message.type) &&
message.caption === "#sticker"
) {
await cl.sendText(message.chatId, "Processing sticker");
const mediaData = await decryptMedia(message);
const dataUrl = `data:${message.mimetype};base64,${mediaData.toString(
"base64"
)}`;
message.type === "image" &&
(await cl.sendImageAsSticker(message.chatId, dataUrl, message.id));
message.type === "video" &&
(await cl.sendMp4AsSticker(message.chatId, dataUrl));
}
}
/**
* Add message to process queue
*/
const processMessage = (message) =>
queue.add(async () => {
try {
await procMess(message);
} catch (e) {
console.log(e);
}
});
/**
* Initialize client
* @param {import("@open-wa/wa-automate").Client} client
*/
async function start(client) {
cl = client;
queue.start();
const unreadMessages = await client.getAllUnreadMessages();
unreadMessages.forEach(processMessage);
client.onMessage(processMessage);
}
ev.on("qr.**", async (qrcode) => {
const imageBuffer = Buffer.from(
qrcode.replace("data:image/png;base64,", ""),
"base64"
);
fs.writeFileSync("./public/qr_code.png", imageBuffer);
});
create({
qrTimeout: 0,
cacheEnabled: false,
}).then((client) => start(client));
server.use(express.static("public"));
server.listen(PORT, () =>
console.log(`> Listining on http://localhost:${PORT}`)
);
process.on("exit", () => {
if (fs.existsSync("./session.data.json")) {
fs.unlinkSync("./session.data.json");
}
});