Skip to content

Commit 2b069a6

Browse files
committed
Implement dynamic command reloading feature
1 parent 634ab5c commit 2b069a6

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/commands/reload.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Message } from "whatsapp-web.js";
2+
import axios from "axios";
3+
import log from "npmlog";
4+
import fs from "fs";
5+
import path from "path";
6+
import { commands } from "../index";
7+
8+
export const command = "reload";
9+
10+
export default async function (msg: Message) {
11+
try {
12+
let count = 0;
13+
const commandsPath = path.join(__dirname, "..", "commands");
14+
fs.readdirSync(commandsPath).forEach((file) => {
15+
if (/\.js$|\.ts$/.test(file)) {
16+
const filePath = path.join(commandsPath, file);
17+
delete require.cache[require.resolve(filePath)];
18+
const commandModule = require(filePath);
19+
if (commandModule.command && typeof commandModule.default === "function") {
20+
commands[commandModule.command] = commandModule.default;
21+
count++;
22+
log.info("Loader", `Reloaded command: ${commandModule.command}`);
23+
}
24+
}
25+
});
26+
27+
await msg.reply(`Reloaded ${count} commands successfully.`);
28+
} catch (error) {
29+
log.error("Command", "Error occured while processing the request:", error);
30+
await msg.reply("An error occurred while processing your request.");
31+
}
32+
}

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,33 @@ const client = new Client({
2222
});
2323
startServer(Number(port));
2424

25-
// Load commands dynamically
2625
const commands: Record<string, (msg: Message) => void> = {};
2726
const commandsPath = path.join(__dirname, "commands");
28-
fs.readdirSync(commandsPath).forEach((file) => {
27+
28+
// Initial load
29+
const loadCommand = (file: string) => {
2930
if (/\.js$|\.ts$/.test(file)) {
30-
const commandModule = require(path.join(commandsPath, file));
31+
const filePath = path.join(commandsPath, file);
32+
delete require.cache[require.resolve(filePath)];
33+
const commandModule = require(filePath);
3134
if (commandModule.command && typeof commandModule.default === "function") {
3235
commands[commandModule.command] = commandModule.default;
3336
log.info("Loader", `Loaded command: ${commandModule.command}`);
3437
}
3538
}
39+
}
40+
41+
fs.readdirSync(commandsPath).forEach(loadCommand);
42+
43+
// Watch for changes
44+
fs.watch(commandsPath, (eventType, filename) => {
45+
if (filename && /\.js$|\.ts$/.test(filename)) {
46+
try {
47+
loadCommand(filename);
48+
} catch (err) {
49+
log.error("Loader", `Failed to reload command: ${filename}`, err);
50+
}
51+
}
3652
});
3753

3854
client.on("qr", (qr) => {
@@ -76,3 +92,5 @@ const messageEvent = (msg: Message) => {
7692
if (debug) log.info("Message", msg.body.slice(0, 255));
7793
handler(msg);
7894
};
95+
96+
export { commands }

0 commit comments

Comments
 (0)