Skip to content

Commit 10b9c14

Browse files
committed
bug fixes and improvements
1 parent 1ee13a1 commit 10b9c14

7 files changed

Lines changed: 80 additions & 14 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"main": "src/index.ts",
77
"scripts": {
88
"build": "tsc",
9-
"start": "ts-node src/index.ts"
9+
"start": "clear && while [ true ]; do npm run canis ; done",
10+
"canis": "ts-node src/index.ts"
1011
},
1112
"author": {
1213
"name": "Melvin Jones Repol",

src/commands/hotreload.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Message } from "whatsapp-web.js";
2+
import fs from "fs/promises";
3+
import log from "../components/utils/log";
4+
5+
export const command = "hotreload";
6+
export const role = "admin";
7+
8+
export default async function (msg: Message) {
9+
await msg.react("🔄");
10+
11+
const tempDir = "./.temp";
12+
await fs.mkdir(tempDir, { recursive: true });
13+
14+
const tempPath = `${tempDir}/hotreload`;
15+
await fs.writeFile(tempPath, JSON.stringify(msg));
16+
17+
log.info("hotreload", "exiting...");
18+
process.exit(0);
19+
}

src/commands/uptime.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export const role = "user";
99
export default async function (msg: Message) {
1010
const uptimeMinutes = Math.floor(process.uptime() / 60);
1111
const statsMessage = `
12-
*Update*
13-
14-
${uptimeMinutes} minutes #${process.pid}
12+
*${uptimeMinutes} minutes*
13+
#${process.pid}
1514
`;
1615

1716
await msg.reply(statsMessage);

src/components/client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import {
88
} from "whatsapp-web.js";
99
import qrcode from "qrcode-terminal";
1010
import messageEvent from "./events/message";
11-
import groupJoin from "./events/groups/join";
1211
import groupLeave from "./events/groups/leave";
12+
import groupJoin from "./events/groups/join";
1313
import reaction from "./events/reaction";
14+
import ready from "./events/ready";
15+
16+
import fs from "fs";
17+
import path from "path";
1418

1519
const client = new Client({
1620
authStrategy: new LocalAuth(),
@@ -22,7 +26,7 @@ client.on("qr", (qr: string) => {
2226
qrcode.generate(qr, { small: true });
2327
});
2428

25-
client.on("ready", () => log.info("Client", "WhatsApp client is ready!"));
29+
client.on("ready", async () => ready());
2630
client.on("message_reaction", async (react: Reaction) =>
2731
reaction(client, react)
2832
);

src/components/events/groups/join.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export default async function join(notif: GroupNotification) {
99

1010
for (const contact of recipients) {
1111
const name = contact.pushname || contact.name || contact.id.user;
12-
log.info("Group Leave", `${name} left the group ${group.name}`);
13-
await sleep(1500); // prevent flooding
14-
await notif.reply(`👋 *${name}* has left. I’ll miss you!`);
12+
log.info("Group Join", `${name} joined the group ${group.name}`);
13+
await sleep(2000); // prevent flooding
14+
await notif.reply(`👋 Welcome *${name}* 🎉`);
1515
}
1616
} catch (err) {
17-
log.error("Group Leave", "Failed to process group leave event:", err);
17+
log.error("Group Join", "Failed to process group join event:", err);
1818
}
1919
}

src/components/events/groups/leave.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export default async function leave(notif: GroupNotification) {
99

1010
for (const contact of recipients) {
1111
const name = contact.pushname || contact.name || contact.id.user;
12-
log.info("Group Join", `${name} joined the group ${group.name}`);
13-
await sleep(2000); // prevent flooding
14-
await notif.reply(`👋 Welcome *${name}* 🎉`);
12+
log.info("Group Leave", `${name} left the group ${group.name}`);
13+
await sleep(1500); // prevent flooding
14+
await notif.reply(`👋 *${name}* has left. I’ll miss you!`);
1515
}
1616
} catch (err) {
17-
log.error("Group Join", "Failed to process group join event:", err);
17+
log.error("Group Leave", "Failed to process group leave event:", err);
1818
}
1919
}

src/components/events/ready.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import log from "../utils/log";
2+
import fs from "fs";
3+
import path from "path";
4+
import { client } from "../client";
5+
import sleep from "../utils/sleep";
6+
7+
export default async function ready() {
8+
log.info("Client", "WhatsApp client is ready!");
9+
10+
await sleep(5000); // Give some time for the client to stabilize
11+
12+
const hotReloadPath = path.resolve(__dirname, "../../../.temp/hotreload");
13+
if (fs.existsSync(hotReloadPath)) {
14+
try {
15+
const tempData = JSON.parse(fs.readFileSync(hotReloadPath, "utf-8"));
16+
17+
let chatId = null;
18+
if (typeof tempData.id === "string") {
19+
chatId = tempData.id;
20+
} else if (tempData.id?._serialized) {
21+
chatId = tempData.id._serialized;
22+
} else if (tempData.id?.id) {
23+
chatId = tempData.id.id + "@c.us";
24+
}
25+
26+
if (chatId) {
27+
await client.sendMessage(chatId, "Hot reload done.");
28+
return;
29+
}
30+
log.error("Hot Reload", "Could not resolve chat ID for message.");
31+
} catch (err) {
32+
log.error("Hot Reload", err);
33+
} finally {
34+
fs.unlink(hotReloadPath, (err) => {
35+
if (err) {
36+
log.error("Hot Reload", "Failed to delete hot reload file:", err);
37+
} else {
38+
log.info("Hot Reload", "Hot reload file deleted successfully.");
39+
}
40+
});
41+
}
42+
}
43+
};

0 commit comments

Comments
 (0)