diff --git a/app/src/commands/system/reload.ts b/app/src/commands/system/reload.ts new file mode 100644 index 0000000..3ec3f3f --- /dev/null +++ b/app/src/commands/system/reload.ts @@ -0,0 +1,31 @@ +import { createCommand } from "@lib/utils"; +import { KyokoClient } from "@lib/KyokoClient"; +import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js"; + +export const reload = createCommand({ + data: new SlashCommandBuilder() + .setName("reload") + .setDescription("Reloads all commands") + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), + execute: async (interaction) => { + await interaction.deferReply({ ephemeral: true }); + + try { + await KyokoClient.loadCommands(true); + const embed = new EmbedBuilder() + .setTitle("✅ System Reloaded") + .setDescription(`Successfully reloaded ${KyokoClient.commands.size} commands.`) + .setColor("Green"); + + await interaction.editReply({ embeds: [embed] }); + } catch (error) { + console.error(error); + const embed = new EmbedBuilder() + .setTitle("❌ Reload Failed") + .setDescription("An error occurred while reloading commands. Check console for details.") + .setColor("Red"); + + await interaction.editReply({ embeds: [embed] }); + } + } +}); \ No newline at end of file diff --git a/app/src/lib/KyokoClient.ts b/app/src/lib/KyokoClient.ts index 7813b89..5e25388 100644 --- a/app/src/lib/KyokoClient.ts +++ b/app/src/lib/KyokoClient.ts @@ -13,12 +13,17 @@ class Client extends DiscordClient { this.commands = new Collection(); } - 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}`); diff --git a/app/src/scripts/deploy.ts b/app/src/scripts/deploy.ts index 2976fe2..c5a712d 100644 --- a/app/src/scripts/deploy.ts +++ b/app/src/scripts/deploy.ts @@ -3,12 +3,13 @@ import { KyokoClient } from "@lib/KyokoClient"; console.log("🚀 Starting deployment script..."); // Load all commands first -await KyokoClient.loadCommands(); +await KyokoClient.loadCommands(false); console.log(`📦 Loaded ${KyokoClient.commands.size} commands.`); // Deploy await KyokoClient.deployCommands(); + console.log("👋 Deployment script finished."); process.exit(0);