Skip to content

Commit 7b056f8

Browse files
committed
Enhance reload command to support reloading specific commands and improve loader function to accept custom paths
1 parent 0c4a052 commit 7b056f8

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/commands/reload.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@ import log from "npmlog";
44
import fs from "fs";
55
import path from "path";
66
import { commands } from "../index";
7+
import Loader from "../components/loader";
78

89
export const command = "reload";
910
export const role = "admin";
1011

1112
export default async function (msg: Message) {
13+
const query = msg.body.replace(/^reload\b\s*/i, "").trim();
14+
if (query.length !== 0) {
15+
if (!commands[query.toLocaleLowerCase()]) {
16+
await msg.reply(`Command "${query}" not found.`);
17+
return;
18+
}
19+
20+
const commandsPath = path.join(__dirname, "..", "commands");
21+
const possibleExtensions = [".ts", ".js"];
22+
let found = false;
23+
24+
for (const ext of possibleExtensions) {
25+
const filePath = path.join(commandsPath, `${query}${ext}`);
26+
if (fs.existsSync(filePath)) {
27+
Loader(`${query}${ext}`);
28+
found = true;
29+
}
30+
}
31+
32+
if (!found) await msg.reply(`Failed to reload command "${query}".`);
33+
if (found) await msg.reply(`Reloaded command "${query}".`);
34+
return;
35+
}
36+
37+
// Reload all commands
1238
let count = 0;
1339
const commandsPath = path.join(__dirname, "..", "commands");
1440
fs.readdirSync(commandsPath).forEach((file) => {

src/components/client.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ client.initialize();
3434

3535
const messageEvent = (msg: Message) => {
3636
// ignore message if it is older than 10 seconds
37-
if (msg.timestamp < Date.now() / 1000 - 10)
38-
return;
37+
if (msg.timestamp < Date.now() / 1000 - 10) return;
3938

4039
const prefix = !msg.body.startsWith(commandPrefix);
4140
const senderId = msg.from.split("@")[0];
@@ -49,10 +48,11 @@ const messageEvent = (msg: Message) => {
4948
/*
5049
* Check if the message starts with the command prefix.
5150
*/
52-
const keyWithPrefix = msg.body.split(" ")[0];
53-
const key = keyWithPrefix.startsWith(commandPrefix)
54-
? keyWithPrefix.slice(commandPrefix.length)
55-
: keyWithPrefix;
51+
const messageBody = msg.body.split(" ")[0];
52+
const bodyHasPrefix = messageBody.startsWith(commandPrefix);
53+
const key = bodyHasPrefix
54+
? messageBody.slice(commandPrefix.length)
55+
: messageBody;
5656
const handler = commands[key.toLocaleLowerCase()];
5757
if (!handler) return;
5858

@@ -76,9 +76,7 @@ const messageEvent = (msg: Message) => {
7676
}
7777

7878
if (debug) log.info("Message", msg.body.slice(0, 255));
79-
msg.body = commandPrefixLess
80-
? msg.body
81-
: msg.body.slice(commandPrefix.length).trim();
79+
msg.body = !bodyHasPrefix ? msg.body : msg.body.slice(commandPrefix.length);
8280

8381
/*
8482
* Execute the command handler.

src/components/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export const role = "admin";
88

99
const commandsPath = path.join(__dirname, "..", "commands");
1010

11-
export default function Loader(file: string) {
11+
export default function Loader(file: string, customPath?: string) {
1212
if (/\.js$|\.ts$/.test(file)) {
13-
const filePath = path.join(commandsPath, file);
13+
const filePath = path.join(customPath || commandsPath, file);
1414
delete require.cache[require.resolve(filePath)];
1515
const commandModule = require(filePath);
1616

0 commit comments

Comments
 (0)