Skip to content

Commit 4136d61

Browse files
committed
feat: streamline message handling by removing random response logic and add message edit event handling
1 parent 7bf6cf6 commit 4136d61

6 files changed

Lines changed: 49 additions & 25 deletions

File tree

src/commands/ai.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ export default async function (msg: Message) {
3333
await msg.reply("Sorry, I couldn't generate a response. Please try again.");
3434
return;
3535
}
36-
37-
if (Math.random() < 0.5) {
38-
const chat = await msg.getChat();
39-
await client.sendMessage(chat.id._serialized, text);
40-
return;
41-
}
36+
4237
await msg.reply(text);
4338
}

src/commands/mj.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ export default async function (msg: Message) {
4040
);
4141
return;
4242
}
43-
const font = Font(text);
4443

45-
if (Math.random() < 0.5) {
46-
const chat = await msg.getChat();
47-
await client.sendMessage(chat.id._serialized, font);
48-
return;
49-
}
50-
await msg.reply(font);
44+
await msg.reply(text);
5145
}

src/commands/sim.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,5 @@ export default async function (msg: Message) {
3838
return;
3939
}
4040

41-
if (Math.random() < 0.5) {
42-
const chat = await msg.getChat();
43-
await client.sendMessage(chat.id._serialized, text);
44-
return;
45-
}
4641
await msg.reply(text);
4742
}

src/components/client.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "whatsapp-web.js";
99
import qrcode from "qrcode-terminal";
1010
import messageEvent from "./events/message";
11+
import messageEdit from "./events/edit";
1112
import groupLeave from "./events/groups/leave";
1213
import groupJoin from "./events/groups/join";
1314
import reaction from "./events/reaction";
@@ -17,22 +18,32 @@ const client = new Client({
1718
authStrategy: new LocalAuth(),
1819
});
1920

21+
client.on("loading_screen", (percent: number, message: string) =>
22+
log.info("loading", `..................... ${percent}%`)
23+
);
24+
client.on("authenticated", () =>
25+
log.info("auth", "Client authenticated successfully.")
26+
);
2027
client.on("qr", (qr: string) => {
2128
// Generate and scan this code with your phone
22-
log.info("QR Code", "Scan this QR code with your WhatsApp app:");
29+
log.info("qrcode", "Scan this QR code with your WhatsApp app:");
2330
qrcode.generate(qr, { small: true });
2431
});
25-
2632
client.on("ready", async () => ready());
2733
client.on("message_reaction", async (react: Reaction) =>
2834
reaction(client, react)
2935
);
30-
client.on("group_join", async (notif: GroupNotification) => groupJoin(notif));
31-
client.on("group_leave", async (notif: GroupNotification) => groupLeave(notif));
3236
// client.on("message", (msg) => messageEvent(msg));
3337
client.on("message_create", async (msg: Message) => messageEvent(msg));
38+
client.on(
39+
"message_edit",
40+
async (msg: Message, newBody: String, prevBody: String) =>
41+
messageEdit(msg, newBody, prevBody)
42+
);
43+
client.on("group_join", async (notif: GroupNotification) => groupJoin(notif));
44+
client.on("group_leave", async (notif: GroupNotification) => groupLeave(notif));
3445
client.on("auth_failure", (msg: String) =>
35-
log.error("Auth", "Authentication failed. Please try again.")
46+
log.error("auth", "Authentication failed. Please try again.")
3647
);
3748

3849
client.initialize();

src/components/events/edit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Message } from "whatsapp-web.js";
2+
3+
export default async function (msg: Message, newBody: String, prevBody: String) {
4+
await msg.reply(`Your message was edited from "${prevBody}."`);
5+
}

src/components/events/message.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { Message } from "whatsapp-web.js";
1+
import {
2+
Client,
3+
Message,
4+
MessageContent,
5+
MessageSendOptions,
6+
} from "whatsapp-web.js";
27
import log from "../utils/log";
38
import { commands } from "../../index";
49
import rateLimiter from "../utils/rateLimiter";
510
import { isRateLimitError, getRateLimitInfo } from "../utils/rateLimit";
611
import sleep from "../utils/sleep";
712
import { findOrCreateUser, isBlocked } from "../services/user";
13+
import { client } from "../client";
14+
import Font from "../utils/font";
815

916
const commandPrefix = process.env.COMMAND_PREFIX || "!";
1017
const commandPrefixLess = process.env.COMMAND_PREFIX_LESS === "true";
@@ -44,7 +51,9 @@ export default async function message(msg: Message) {
4451
/*
4552
* Block users from running commands.
4653
*/
47-
const isBlockedUser = await isBlocked(msg.author ? msg.author.split("@")[0] : senderId);
54+
const isBlockedUser = await isBlocked(
55+
msg.author ? msg.author.split("@")[0] : senderId
56+
);
4857
if (isBlockedUser) {
4958
return;
5059
}
@@ -73,6 +82,21 @@ export default async function message(msg: Message) {
7382
}
7483
msg.body = !bodyHasPrefix ? msg.body : msg.body.slice(commandPrefix.length);
7584

85+
const originalReply = msg.reply.bind(msg);
86+
msg.reply = async (
87+
content: MessageContent,
88+
chatId?: string,
89+
options?: MessageSendOptions
90+
): Promise<Message> => {
91+
let text =
92+
Font(typeof content === "string" ? content : (content as any).body) || "";
93+
94+
if (Math.random() < 0.5) {
95+
return await client.sendMessage(msg.id.remote, text);
96+
}
97+
return await originalReply(text, chatId, options);
98+
};
99+
76100
/*
77101
* Execute the command handler.
78102
*/

0 commit comments

Comments
 (0)