Skip to content

Commit 4c4e7e1

Browse files
committed
feat: call event handler & mention responses
1 parent 9e9d969 commit 4c4e7e1

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/components/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import log from "./utils/log";
22
import {
3+
Call,
34
Client,
45
GroupNotification,
56
LocalAuth,
@@ -15,6 +16,7 @@ import groupJoin from "./events/groups/join";
1516
import reaction from "./events/reaction";
1617
import ready from "./events/ready";
1718
import revoke from "./events/revoke";
19+
import callEvent from "./events/call";
1820

1921
const loadingBar = LoadingBar("Loading Client | {bar} | {value}%");
2022
const client = new Client({
@@ -63,6 +65,8 @@ client.on("message_revoke_everyone", (msg: Message, revoked_msg?: Message) =>
6365
revoke(msg, revoked_msg),
6466
);
6567

68+
client.on("call", (call: Call) => callEvent(call));
69+
6670
client.on("group_join", (notif: GroupNotification) => groupJoin(notif));
6771
client.on("group_leave", (notif: GroupNotification) => groupLeave(notif));
6872
client.on("auth_failure", (msg: string) => {

src/components/events/call.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Call } from "whatsapp-web.js";
2+
import log from "../utils/log";
3+
import { client } from "../client";
4+
5+
const voiceResponses = [
6+
"Sorry, I can't take voice calls right now.",
7+
"I'm unavailable for a voice call at the moment.",
8+
"Not able to answer your call right now, please text me instead.",
9+
"I'm currently busy, please try later for a voice call.",
10+
];
11+
12+
const videoResponses = [
13+
"Sorry, I can’t do video calls right now.",
14+
"I’m not available for a video call at the moment.",
15+
"Please send me a message instead of a video call.",
16+
"I can’t pick up your video call right now, let’s chat instead.",
17+
];
18+
19+
function getRandomResponse(arr: string[]) {
20+
return arr[Math.floor(Math.random() * arr.length)];
21+
}
22+
23+
export default async function (call: Call) {
24+
try {
25+
if (call.fromMe) return;
26+
27+
if (call.timestamp < Date.now() / 1000 - 60) {
28+
await call.reject();
29+
if (!call.from) return;
30+
31+
let response;
32+
if (call.isVideo) {
33+
response = getRandomResponse(videoResponses);
34+
} else {
35+
response = getRandomResponse(voiceResponses);
36+
}
37+
38+
await client.sendMessage(call.from, response);
39+
40+
log.info(
41+
"CallEvent",
42+
`Rejected a ${call.isVideo ? "video" : "voice"} call from ${call.from}`,
43+
);
44+
}
45+
} catch (err) {
46+
log.error("Error handling call:", err);
47+
}
48+
}

src/components/events/edit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default async function (
1212
_newBody: string,
1313
prevBody: string,
1414
) {
15+
if (msg.fromMe) return;
1516
// const isGroup = !!msg.author;
1617
// const user = await getUserbyLid(msg.from) || "Your";
1718
//

src/components/events/message.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ const regex = emojiRegex();
2121
const commandPrefix = process.env.COMMAND_PREFIX || "!";
2222
const commandPrefixLess = process.env.COMMAND_PREFIX_LESS === "true";
2323
const debug = process.env.DEBUG === "true";
24+
const mentionResponses = [
25+
"👀 Did someone just say my name?",
26+
"Bruh, why me again? 😂",
27+
"Oh no… not me 😭",
28+
"You called? Or just summoning me like Voldemort?",
29+
"Here I am, what’s the emergency? 🚨",
30+
"Why always me tho 🤔",
31+
"Plot twist: I was just about to mention YOU.",
32+
"Careful… mention me three times and I appear 👻",
33+
"My ears were burning 🔥",
34+
"Did you just @ me for vibes, or do I owe you money? 💸",
35+
];
2436

2537
export default async function (msg: Message, type: string) {
2638
// ignore message if it is older than 10 seconds
@@ -113,7 +125,21 @@ export default async function (msg: Message, type: string) {
113125
? messageBody.slice(commandPrefix.length).trim()
114126
: messageBody;
115127
const handler = commands[key.toLowerCase()];
116-
if (!handler) return;
128+
129+
// no match command
130+
// so check if the message mentioned the bot name
131+
// and return funny messages HAHAHAHA
132+
if (!handler) {
133+
if (
134+
msg.mentionedIds &&
135+
msg.mentionedIds.length > 0 &&
136+
msg.mentionedIds.includes(client.info.wid._serialized)
137+
)
138+
await msg.reply(
139+
mentionResponses[Math.random() * mentionResponses.length],
140+
);
141+
return;
142+
}
117143

118144
/*
119145
* Block users from running commands.

0 commit comments

Comments
 (0)