feat: Add /reload command and implement dynamic command reloading functionality in KyokoClient.

This commit is contained in:
syntaxbullet
2025-12-05 12:35:37 +01:00
parent 48995204a5
commit 1a5503a3a6
3 changed files with 44 additions and 6 deletions

View File

@@ -13,12 +13,17 @@ class Client extends DiscordClient {
this.commands = new Collection<string, Command>();
}
async loadCommands() {
async loadCommands(reload: boolean = false) {
if (reload) {
this.commands.clear();
console.log("♻️ Reloading commands...");
}
const commandsPath = join(import.meta.dir, '../commands');
await this.readCommandsRecursively(commandsPath);
await this.readCommandsRecursively(commandsPath, reload);
}
private async readCommandsRecursively(dir: string) {
private async readCommandsRecursively(dir: string, reload: boolean = false) {
try {
const files = await readdir(dir, { withFileTypes: true });
@@ -26,14 +31,15 @@ class Client extends DiscordClient {
const filePath = join(dir, file.name);
if (file.isDirectory()) {
await this.readCommandsRecursively(filePath);
await this.readCommandsRecursively(filePath, reload);
continue;
}
if (!file.name.endsWith('.ts') && !file.name.endsWith('.js')) continue;
try {
const commandModule = await import(filePath);
const importPath = reload ? `${filePath}?t=${Date.now()}` : filePath;
const commandModule = await import(importPath);
const commands = Object.values(commandModule);
if (commands.length === 0) {
console.warn(`⚠️ No commands found in ${file.name}`);