Skip to content

Commit 8cffefa

Browse files
committed
feat: bug fixes and improvements
1 parent 3706d68 commit 8cffefa

14 files changed

Lines changed: 87 additions & 91 deletions

File tree

src/commands/play.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default async function play(msg: Message) {
4747
return;
4848
}
4949

50-
await msg.react("");
50+
await msg.react("🔍");
5151

5252
const stream = await yt.download(audio.id, {
5353
type: "video+audio",
@@ -56,10 +56,15 @@ export default async function play(msg: Message) {
5656
});
5757

5858
if (!stream) {
59-
await msg.reply("Failed to download the audio stream.");
59+
await Promise.all([
60+
msg.reply("Failed to download the audio stream."),
61+
msg.react(""),
62+
]);
6063
return;
6164
}
6265

66+
await msg.react("⬇️");
67+
6368
const tempDir = "./.temp";
6469
await fs.promises.mkdir(tempDir, { recursive: true });
6570
const tempPath = path.join(tempDir, `${audio.id}.mp3`);

src/commands/top.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export default async function (msg: Message) {
2424
\`Top Users by Activity:\`
2525
2626
${user
27-
.filter((u, index) => u.commandCount !== 0 && index < 20)
27+
.sort((a, b) => (b.totalActivity ?? 0) - (a.totalActivity ?? 0))
28+
.slice(0, 30)
2829
.map((u, index) => {
2930
const displayName =
30-
u.name.length > 12 ? u.name.slice(0, 12) + "..." : u.name;
31-
return `${index + 1}. ${displayName}: ${u.commandCount + u.quizAnswered} Points`;
31+
u.name.length > 9 ? u.name.slice(0, 9) + ".." : u.name;
32+
return `${index + 1}. ${displayName}: ${u.totalActivity} Points`;
3233
})
3334
.join("\n ")}
3435
`;

src/commands/video.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default async function (msg: Message) {
4949
return;
5050
}
5151

52-
await msg.react("");
52+
await msg.react("🔍");
5353

5454
const stream = await yt.download(video.video_id, {
5555
type: "video+audio",
@@ -62,6 +62,8 @@ export default async function (msg: Message) {
6262
return;
6363
}
6464

65+
await msg.react("⬇️");
66+
6567
const tempDir = "./.temp";
6668
await fs.promises.mkdir(tempDir, { recursive: true });
6769
const tempPath = path.join(tempDir, `${video.video_id}.mp4`);

src/components/events/call.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,22 @@ export default async function (call: Call) {
2424
try {
2525
if (call.fromMe) return;
2626

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-
);
27+
await call.reject();
28+
if (!call.from) return;
29+
30+
let response;
31+
if (call.isVideo) {
32+
response = getRandomResponse(videoResponses);
33+
} else {
34+
response = getRandomResponse(voiceResponses);
4435
}
36+
37+
await client.sendMessage(call.from, response);
38+
39+
log.info(
40+
"CallEvent",
41+
`Rejected a ${call.isVideo ? "video" : "voice"} call from ${call.from}`,
42+
);
4543
} catch (err) {
4644
log.error("Error handling call:", err);
4745
}

src/components/events/edit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ export default async function (
1313
prevBody: string,
1414
) {
1515
if (msg.fromMe) return;
16+
const lid = (msg.author ?? msg.from).split("@")[0];
1617
// const isGroup = !!msg.author;
1718
// const user = await getUserbyLid(msg.from) || "Your";
1819
//
19-
log.info("EditMessage", prevBody);
20+
log.info("EditMessage", lid, prevBody);
2021
await addMessage(msg, prevBody, "edit");
2122
// await msg.reply(
2223
// `${isGroup ? user : "Your"} message was edited from "${prevBody}".`

src/components/events/groups/join.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export default async function (notif: GroupNotification) {
1111
if (notif.timestamp < Date.now() / 1000 - 10) return;
1212
const group = await notif.getChat();
1313
const recipients = await notif.getRecipients();
14+
const text = `
15+
🙋‍♂️ Hello everyone!
16+
17+
I'm ${PROJECT_CANIS_ALIAS}, a scalable, modular and
18+
flexible chatbot for WhatsApp and Telegram.
19+
20+
By continuing you agree to the bot \`terms\` and \`privacy\`.
21+
To list down commands type \`help\`.
22+
`;
1423

1524
const newMembers = [];
1625

@@ -21,16 +30,7 @@ export default async function (notif: GroupNotification) {
2130
log.info("GroupJoin", `${name} joined the group ${group.name}`);
2231

2332
if (isSelf) {
24-
await notif.reply(
25-
`🙋‍♂️ Hello everyone!
26-
27-
I'm ${PROJECT_CANIS_ALIAS}, a scalable, modular and
28-
flexible chatbot for WhatsApp and Telegram.
29-
30-
By continuing you agree to the bot \`terms\` and \`privacy\`.
31-
To list down commands type \`help\`.
32-
`,
33-
);
33+
await notif.reply(text);
3434
} else {
3535
newMembers.push(name);
3636
}

src/components/events/message.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@ export default async function (msg: Message, type: string) {
3939
// ignore message if it is older than 10 seconds
4040
if (msg.timestamp < Date.now() / 1000 - 10 && type === "create") return;
4141

42-
if (
43-
// (msg.hasQuotedMsg && !msg.fromMe) ||
44-
msg.isForwarded ||
45-
msg.isGif ||
46-
msg.isStatus ||
47-
msg.broadcast
48-
)
49-
return; // ignore them all
42+
if (msg.isGif || msg.isStatus || msg.broadcast) return; // ignore them all
5043

5144
// process normalization
5245
msg.body = msg.body
@@ -74,6 +67,8 @@ export default async function (msg: Message, type: string) {
7467
await msg.reply(text);
7568
});
7669

70+
if (msg.isForwarded) return;
71+
7772
/*
7873
*
7974
* Quiz command validation
@@ -109,7 +104,7 @@ export default async function (msg: Message, type: string) {
109104

110105
await sleep(randomMs);
111106
const isEmoji = /.*[A-Za-z0-9].*/.test(reaction);
112-
log.info("AutoReact", reaction);
107+
log.info("AutoReact", lid, reaction);
113108

114109
if (Math.random() < 0.1 && !isEmoji)
115110
if (Math.random() < 0.2)
@@ -251,6 +246,8 @@ export default async function (msg: Message, type: string) {
251246
handler.exec(msg),
252247

253248
(async () => {
249+
if (!msg.author) return;
250+
254251
const user = await findOrCreateUser(msg);
255252

256253
if (user) {

src/components/events/reaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async function (client: Client, react: Reaction) {
1818
if (!message) return;
1919
await sleep(2000);
2020
await message.react(react.reaction);
21-
log.info("Reaction", `Reacted to message with ${react.reaction}`);
21+
log.info("Reaction", senderId, `Reacted to message with ${react.reaction}`);
2222
} catch (error) {
2323
log.error("Failed to react back to message:", error);
2424
}

src/components/events/revoke.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import log from "../utils/log";
99
*/
1010
export default async function (msg: Message, revoked_msg?: Message) {
1111
if (msg.fromMe || !revoked_msg) return;
12-
if (msg.timestamp < Date.now() / 1000 - 10) return;
12+
const lid = (msg.author ?? msg.from).split("@")[0];
1313
// const isGroup = !!msg.author;
1414
// const user = (await getUserbyLid(msg.from)) || "Your";
15-
log.info("RevokeMessage", revoked_msg?.body);
15+
log.info("RevokeMessage", lid, revoked_msg?.body);
1616
await addMessage(msg, revoked_msg?.body, "revoke");
1717
// await msg.reply(
1818
// `${isGroup ? user : "Your"} message "${revoked_msg?.body}" was deleted.`

0 commit comments

Comments
 (0)