forked from syntaxbullet/AuroraBot-discord
feat: Add /reload command and implement dynamic command reloading functionality in KyokoClient.
This commit is contained in:
31
app/src/commands/system/reload.ts
Normal file
31
app/src/commands/system/reload.ts
Normal file
@@ -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] });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -13,12 +13,17 @@ class Client extends DiscordClient {
|
||||
this.commands = new Collection<string, Command>();
|
||||
}
|
||||
|
||||
async loadCommands() {
|
||||
const commandsPath = join(import.meta.dir, '../commands');
|
||||
await this.readCommandsRecursively(commandsPath);
|
||||
async loadCommands(reload: boolean = false) {
|
||||
if (reload) {
|
||||
this.commands.clear();
|
||||
console.log("♻️ Reloading commands...");
|
||||
}
|
||||
|
||||
private async readCommandsRecursively(dir: string) {
|
||||
const commandsPath = join(import.meta.dir, '../commands');
|
||||
await this.readCommandsRecursively(commandsPath, reload);
|
||||
}
|
||||
|
||||
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}`);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user