Skip to content

Commit 3dbc81f

Browse files
committed
feat: bug fixes and improvements
1 parent 541b722 commit 3dbc81f

5 files changed

Lines changed: 68 additions & 34 deletions

File tree

src/commands/help.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import { commands } from "../components/utils/cmd/loader";
44

55
export const info = {
66
command: "help",
77
description: "List available commands and their usage.",
8-
usage: "help [page] | [role]",
9-
example: "help admin",
8+
usage: "help [page] | [role] | [command]",
9+
example: "help",
1010
role: "user",
1111
cooldown: 5000,
1212
};
@@ -26,10 +26,10 @@ function paginate(items: string[], page: number, pageSize: number): string[] {
2626
function buildUserPage(
2727
userCommands: string[],
2828
page: number,
29-
totalPages: number
29+
totalPages: number,
3030
): string {
3131
let response = `
32-
Use \`help <command>\` for more details on a specific command.\n
32+
Use \`help [page] | [role] | [command]\` for more details on a specific command.\n
3333
< ─────────── >\n • ${userCommands.join("\n • ") || "_None_"}\n\n`;
3434
response += `< ─────────── >`;
3535
response += `\n\`Page ${page} of ${totalPages}\``;
@@ -38,7 +38,7 @@ function buildUserPage(
3838

3939
function buildAdminPage(adminCommands: string[]): string {
4040
return `
41-
Use \`help <command>\` for more details on a specific command.\n
41+
Use \`help [page] | [role] | [command]\` for more details on a specific command.\n
4242
< ─────────── >\n • ${
4343
adminCommands.join("\n • ") || "_None_"
4444
}\n< ─────────── >
@@ -79,6 +79,11 @@ export default async function (msg: Message) {
7979
return;
8080
}
8181

82+
if (!/^[1-9]\d*$/.test(query)) {
83+
await msg.reply("Please type a valid page number.");
84+
return;
85+
}
86+
8287
// help [page]
8388
const match = query.match(/(\d+)?/i);
8489
const page = Math.max(1, match && match[1] ? parseInt(match[1], 10) : 1);

src/commands/stalk.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
2+
import { getQuizCount } from "../components/services/quiz";
23
import { getUserbyLid } from "../components/services/user";
34

45
export const info = {
@@ -11,32 +12,15 @@ export const info = {
1112
};
1213

1314
export default async function (msg: Message) {
14-
if (msg.body.includes(" ") && msg.mentionedIds.length === 0) {
15-
await msg.reply("Please mention a user to stalk.");
16-
return;
17-
}
18-
1915
if (msg.mentionedIds.length === 0) {
20-
const contact = await msg.getContact();
21-
const countryCode = await contact.getCountryCode();
22-
const about = await contact.getAbout();
23-
const name = contact.pushname || contact.name || "Unknown";
24-
25-
const text = `
26-
\`${name}\`
27-
${about || "No about information available."}
28-
29-
ID: ${contact.id.user}
30-
Number: ${contact.number}
31-
Country Code: ${countryCode}
32-
Type: ${contact.isBusiness ? "Business" : "Personal"}
33-
Mode: ${msg.author ? "group" : "private"}
34-
`;
35-
await msg.reply(text);
16+
await msg.reply("Please mention a user to stalk.");
3617
return;
3718
}
3819

39-
const user = await getUserbyLid(msg.mentionedIds[0].split("@")[0]);
20+
const lid = msg.mentionedIds[0].split("@")[0];
21+
const [user] = await Promise.all([
22+
getUserbyLid(lid)
23+
]);
4024
if (!user) return await msg.reply("User not found.");
4125

4226
const text = `
@@ -49,6 +33,7 @@ export default async function (msg: Message) {
4933
Type: ${user.type}
5034
Mode: ${user.mode}
5135
Command Count: ${user.commandCount}
36+
Last Seen: ${new Date(user.updatedAt).toLocaleString()}
5237
`;
5338
await msg.reply(text);
5439
}

src/components/events/groups/join.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,35 @@ export default async function (notif: GroupNotification) {
1111
const group = await notif.getChat();
1212
const recipients = await notif.getRecipients();
1313

14+
const newMembers = [];
15+
1416
for (const contact of recipients) {
1517
const name = contact.pushname || contact.name || contact.id.user;
1618
const isSelf = contact.id._serialized === client.info.wid._serialized;
1719

1820
await sleep(2000);
1921

2022
log.info("Group Join", `${name} joined the group ${group.name}`);
23+
2124
if (isSelf) {
2225
await notif.reply(
23-
`🙋‍♂️ Hello everyone! I'm ${PROJECT_CANIS_ALIAS} your WhatsApp Bot,
24-
for more information please send \`help\` or \`legal\.`,
26+
`🙋‍♂️ Hello everyone!
27+
28+
I'm ${PROJECT_CANIS_ALIAS}, a scalable, modular and
29+
flexible chatbot for WhatsApp and Telegram.
30+
31+
By continuing you agree to the bot \`terms\` and \`privacy\`.
32+
To list down commands type \`help\`.
33+
`,
2534
);
2635
} else {
27-
await notif.reply(`👋 Welcome *${name}* 🎉`);
36+
newMembers.push(name);
2837
}
2938
}
39+
40+
if (newMembers.length > 0) {
41+
await notif.reply(`👋 Welcome *${newMembers.join(", ")}* 🎉`);
42+
}
3043
} catch (err) {
3144
log.error("Group Join", "Failed to process group join event:", err);
3245
}

src/components/events/groups/leave.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ export default async function (notif: GroupNotification) {
88
const group = await notif.getChat();
99
const recipients = await notif.getRecipients();
1010

11+
const leavers = [];
12+
1113
for (const contact of recipients) {
1214
const name = contact.pushname || contact.name || contact.id.user;
1315
log.info("Group Leave", `${name} left the group ${group.name}`);
16+
1417
await sleep(1500); // prevent flooding
15-
await notif.reply(`👋 *${name}* has left. I’ll miss you!`);
18+
leavers.push(name);
19+
}
20+
21+
if (leavers.length > 0) {
22+
await notif.reply(
23+
leavers.length === 1
24+
? `👋 *${leavers[0]}* has left. I’ll miss you!`
25+
: `👋 *${leavers.join(", ")}* have left. We’ll miss you all!`,
26+
);
1627
}
1728
} catch (err) {
1829
log.error("Group Leave", "Failed to process group leave event:", err);

src/components/services/quiz.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function getQuizAttempts(msg: Message): Promise<any | null> {
3434

3535
export async function setQuizAttemptAnswered(
3636
msg: Message,
37-
quoted: Message
37+
quoted: Message,
3838
): Promise<void> {
3939
try {
4040
if (!quoted.id || !quoted.id.remote) return;
@@ -59,3 +59,23 @@ export async function setQuizAttemptAnswered(
5959
log.error("Database", `Failed to set quiz attempt as answered.`, error);
6060
}
6161
}
62+
63+
export async function getQuizCount(
64+
lid: string,
65+
): Promise<{ total: number; answered: number }> {
66+
try {
67+
const [total, answered] = await Promise.all([
68+
prisma.quiz.count({
69+
where: { lid },
70+
}),
71+
prisma.quiz.count({
72+
where: { lid, answeredAt: { not: null } },
73+
}),
74+
]);
75+
76+
return { total, answered };
77+
} catch (error) {
78+
log.error("Database", `Failed to get quiz counts.`, error);
79+
return { total: 0, answered: 0 };
80+
}
81+
}

0 commit comments

Comments
 (0)