Skip to content

Commit cb98195

Browse files
committed
feat: bug fixes and improvements
- added pair, req & riddle commands - added message content checker for offensive words - update block command for reason - update zsh command
1 parent a4e26ea commit cb98195

18 files changed

Lines changed: 805 additions & 85 deletions

File tree

src/commands/block.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export default async function (msg: Message) {
2727
await prisma.block.upsert({
2828
where: { lid },
2929
update: {},
30-
create: { lid, mode: msg.author ? "group" : "private" },
30+
create: {
31+
lid,
32+
mode: msg.author ? "group" : "private",
33+
reason: "Blocked by admin",
34+
},
3135
});
3236
}
3337

src/commands/chad.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import agentHandler from "../components/ai/agentHandler";
44
import { greetings } from "../components/utils/data";

src/commands/pair.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Contact, GroupChat } from "whatsapp-web.js";
2+
import { Message } from "../../types/message";
3+
import client from "../components/client";
4+
5+
export const info = {
6+
command: "pair",
7+
description: "Pair people and generate a love message",
8+
usage: "pair",
9+
example: "pair",
10+
role: "user",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
if (!/^pair/i.test(msg.body)) return;
16+
17+
const chat = await msg.getChat();
18+
if (!chat.isGroup) {
19+
await msg.reply("This only works on group chats");
20+
return;
21+
}
22+
23+
const groupChat = chat as GroupChat;
24+
const participants = groupChat.participants;
25+
26+
if (participants.length < 2) {
27+
await msg.reply("Not enough people in the group to make a match 💔");
28+
return;
29+
}
30+
31+
const shuffled = participants.sort(() => 0.5 - Math.random());
32+
const [p1, p2] = shuffled.slice(0, 2);
33+
34+
const whatsappClient = await client();
35+
const [c1, c2]: [Contact, Contact] = await Promise.all([
36+
whatsappClient.getContactById(p1.id._serialized),
37+
whatsappClient.getContactById(p2.id._serialized),
38+
]);
39+
40+
const name1 = c1.pushname || c1.number;
41+
const name2 = c2.pushname || c2.number;
42+
43+
const templates = [
44+
`@${name1} and @${name2} are likely compatible! The love between @${name1} and @${name2} is unstoppable 💖`,
45+
`Could @${name1} ❤️ fall for @${name2}? Absolutely, @${name2} will adore @${name1} forever 💘`,
46+
`The stars shine for @${name2} and @${name1} 🌟 Their love is written in destiny 💫`,
47+
`Everyone says @${name1} + @${name2} = pure magic ✨ Love is unstoppable!`,
48+
`Rumor has it that @${name2} secretly loves @${name1} 😍 Compatibility level: 100%`,
49+
`Watch out! @${name1} and @${name2}'s love story is about to go viral 💞`
50+
];
51+
52+
const message = templates[Math.floor(Math.random() * templates.length)];
53+
await msg.reply(
54+
message,
55+
undefined,
56+
{
57+
mentions: [c1, c2] as any,
58+
}
59+
);
60+
}

src/commands/play.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ export default async function play(msg: Message) {
118118
writeStream.on("error", reject);
119119
});
120120

121-
// simulates audio recording
122-
const chat = await msg.getChat();
123-
chat.sendStateRecording();
124-
125121
await execPromise(`ffmpeg -y -i "${tempPath}" -vn -c:a copy "${savePath}"`);
126122
const media = MessageMedia.fromFilePath(savePath);
127123
Promise.all([msg.reply(media), fs.promises.unlink(tempPath)]);

src/commands/req.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Message } from "../../types/message";
2+
import {
3+
findOrCreateUser,
4+
getUserbyLid,
5+
isBlocked,
6+
} from "../components/services/user";
7+
import redis from "../components/redis";
8+
import { client } from "../components/client";
9+
import { MessageMedia } from "whatsapp-web.js";
10+
import { prisma } from "../components/prisma";
11+
import timestamp from "../components/utils/timestamp";
12+
13+
export const info = {
14+
command: "req",
15+
description: "Register a user.",
16+
usage: "req @user",
17+
example: "req @user",
18+
role: "admin",
19+
cooldown: 5000,
20+
};
21+
22+
export default async function (msg: Message) {
23+
const chat = await msg.getChat();
24+
if (!chat.isGroup) {
25+
await msg.reply("This only works on group chats");
26+
return;
27+
}
28+
29+
if (msg.mentionedIds.length === 0) {
30+
await msg.reply("Please mention a user to stalk.");
31+
return;
32+
}
33+
34+
const jid = msg.mentionedIds[0];
35+
const lid = jid.split("@")[0];
36+
37+
const user = await prisma.user
38+
.update({
39+
where: { lid },
40+
data: {},
41+
})
42+
.catch(() => null);
43+
44+
if (user) {
45+
await msg.reply(
46+
`\`${user.name}\` registered since ${timestamp(user.createdAt.getTime())}.`,
47+
);
48+
return;
49+
}
50+
51+
msg.author = jid;
52+
const register = await findOrCreateUser(msg);
53+
if (register) {
54+
await msg.reply("The user has been registered successfully.");
55+
return;
56+
}
57+
58+
await msg.reply("Failed! Please try again later.");
59+
}

src/commands/riddle.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Message } from "../../types/message";
2+
import { riddles } from "../components/utils/data";
3+
import log from "../components/utils/log";
4+
import redis from "../components/redis";
5+
6+
export const info = {
7+
command: "riddle",
8+
description: "Get a random riddle that will shake your head.",
9+
usage: "riddle",
10+
example: "riddle",
11+
role: "user",
12+
cooldown: 5000,
13+
};
14+
15+
export default async function (msg: Message) {
16+
if (!/^riddle/i.test(msg.body)) return;
17+
18+
const id = Math.floor(Math.random() * riddles.length);
19+
const response = riddles[id];
20+
21+
let text = `
22+
\`${response.question}\`
23+
`;
24+
25+
const messageReturn = await msg.reply(text);
26+
redis.set(
27+
`riddle:${messageReturn.id.id}`,
28+
JSON.stringify({ riddle_id: id.toString() }),
29+
);
30+
log.info("riddle", `Riddle sent: ${response.question}`);
31+
}

src/commands/say.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ export default async function (msg: Message) {
2828
host: "https://translate.google.com",
2929
});
3030

31-
// simulates audio recording
32-
const chat = await msg.getChat();
33-
chat.sendStateRecording();
34-
3531
const response = await axios.get(url, { responseType: "arraybuffer" });
3632
const buffer = Buffer.from(response.data);
3733
const tempDir = "./.temp";
@@ -48,7 +44,7 @@ export default async function (msg: Message) {
4844
audioBuffer.toString("base64"),
4945
`${filename}.mp3`,
5046
);
51-
await msg.reply(media, msg.from, {
47+
await msg.reply(media, undefined, {
5248
sendAudioAsVoice: true,
5349
});
5450
await fs.promises.unlink(tempPath);

src/commands/stalk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export const info = {
1414
};
1515

1616
export default async function (msg: Message) {
17+
const chat = await msg.getChat();
18+
if (!chat.isGroup) {
19+
await msg.reply("This only works on group chats");
20+
return;
21+
}
22+
1723
if (msg.mentionedIds.length === 0) {
1824
await msg.reply("Please mention a user to stalk.");
1925
return;

src/commands/zsh.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import { exec } from "child_process";
44
import util from "util";
@@ -23,18 +23,15 @@ export default async function (msg: Message) {
2323
const execPromise = util.promisify(exec);
2424

2525
const { stdout, stderr } = await execPromise(query, {
26-
timeout: 60000,
26+
timeout: 30000,
2727
maxBuffer: 1024 * 1024,
2828
shell: process.env.SHELL || "/bin/zsh",
2929
});
30-
let response = stdout || stderr || "No output.";
30+
let response = `${stdout} \n\n ${stderr}`;
3131
if (response.length > 4000) {
3232
response = response.slice(0, 4000) + "\n\n[Output truncated]";
3333
}
3434

35-
await Promise.all([
36-
msg.reply(response),
37-
logService(msg, query, response),
38-
log.warn("zsh", `Executed command: ${query}`),
39-
]);
35+
await Promise.all([msg.reply(response), logService(msg, query, response)]);
36+
log.warn("zsh", `Executed command: ${query}`);
4037
}

src/components/events/message.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import { phishingSet } from "../../index";
2020
import { getSetting } from "../services/settings";
2121
import { normalize } from "../utils/url";
2222
import { InstantDownloader } from "../utils/instantdl/downloader";
23+
import riddle from "../utils/riddle";
24+
import { checkInappropriate } from "../utils/contentChecker";
25+
import { prisma } from "../prisma";
2326

2427
const regex = emojiRegex();
2528
const commandPrefix = process.env.COMMAND_PREFIX || "!";
@@ -114,7 +117,8 @@ export default async function (msg: Message, type: string) {
114117
Promise.resolve().then(async () => {
115118
const quoted = await msg.getQuotedMessage();
116119
if (!quoted.body || isRateLimit.status) return;
117-
await quiz(msg, quoted);
120+
121+
await Promise.all([quiz(msg, quoted), riddle(msg, quoted)]);
118122
});
119123
}
120124

@@ -226,14 +230,38 @@ export default async function (msg: Message, type: string) {
226230
options?: MessageSendOptions,
227231
): Promise<Message> => {
228232
let messageBody = typeof content === "string" ? Font(content) : content;
229-
230233
log.info("ReplyMessage", lid, content.toString().slice(0, 150));
231234

232235
if (Math.random() < 0.5)
233236
return (await client()).sendMessage(msg.id.remote, messageBody, options);
234237
return await originalReply(messageBody, chatId, options);
235238
};
236239

240+
const isInapproiateResponse = checkInappropriate(msg.body);
241+
if (isInapproiateResponse.isInappropriate) {
242+
const text =
243+
"You have been blocked. For more information \`terms\` & \`privacy\`.";
244+
await Promise.all([
245+
originalReply(text),
246+
prisma.block.upsert({
247+
where: { lid },
248+
update: {},
249+
create: {
250+
lid,
251+
mode: msg.author ? "group" : "private",
252+
reason: `Inapproiate ${isInapproiateResponse.words.join(", ")}`,
253+
},
254+
}),
255+
prisma.user.update({
256+
where: { lid },
257+
data: { points: { decrement: 100 } },
258+
}),
259+
]);
260+
261+
log.info("BlockUser", lid);
262+
return;
263+
}
264+
237265
if (
238266
/^(--?help|\bhelp\b|-h)$/i.test(
239267
msg.body.trim().replace(handler.command, "").trim(),
@@ -256,23 +284,7 @@ export default async function (msg: Message, type: string) {
256284
* Execute the command handler.
257285
*/
258286
try {
259-
await Promise.all([
260-
(async () => {
261-
if (msg.fromMe) return;
262-
263-
const chat = await msg.getChat();
264-
chat.sendStateTyping();
265-
})(),
266-
267-
handler.exec(msg),
268-
269-
(async () => {
270-
if (!msg.author) return;
271-
272-
const user = await findOrCreateUser(msg);
273-
if (user) await msg.react("✅");
274-
})(),
275-
]);
287+
await Promise.all([handler.exec(msg), findOrCreateUser(msg)]);
276288
} catch (error: any) {
277289
if (error.response) {
278290
const { status, headers } = error.response;

0 commit comments

Comments
 (0)