import { createCommand } from "@lib/utils"; import { KyokoClient } from "@/lib/BotClient"; import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, MessageFlags } from "discord.js"; import { createErrorEmbed, createSuccessEmbed, createWarningEmbed } from "@lib/embeds"; export const reload = createCommand({ data: new SlashCommandBuilder() .setName("reload") .setDescription("Reloads all commands") .addBooleanOption(option => option .setName("redeploy") .setDescription("Pull latest changes from git and restart the bot") .setRequired(false) ) .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), execute: async (interaction) => { await interaction.deferReply({ flags: MessageFlags.Ephemeral }); const redeploy = interaction.options.getBoolean("redeploy") ?? false; if (redeploy) { const { exec } = await import("child_process"); const { promisify } = await import("util"); const { writeFile, utimes } = await import("fs/promises"); const execAsync = promisify(exec); try { await interaction.editReply({ embeds: [createWarningEmbed("Pulling latest changes and restarting...", "Redeploy Initiated")] }); const { stdout, stderr } = await execAsync("git pull"); if (stderr && !stdout) { throw new Error(stderr); } await interaction.editReply({ embeds: [createSuccessEmbed(`Git Pull Output:\n\`\`\`\n${stdout}\n\`\`\`\nRestarting process...`, "Update Successful")] }); // Write context for post-restart notification await writeFile(".restart_context.json", JSON.stringify({ channelId: interaction.channelId, userId: interaction.user.id, timestamp: Date.now() })); // Trigger restart by touching entry point const { appendFile } = await import("fs/promises"); await appendFile("src/index.ts", " "); } catch (error) { console.error(error); await interaction.editReply({ embeds: [createErrorEmbed(`Failed to redeploy:\n\`\`\`\n${error instanceof Error ? error.message : String(error)}\n\`\`\``, "Redeploy Failed")] }); } return; } try { const start = Date.now(); await KyokoClient.loadCommands(true); const duration = Date.now() - start; // Deploy commands await KyokoClient.deployCommands(); const embed = createSuccessEmbed( `Successfully reloaded ${KyokoClient.commands.size} commands in ${duration}ms.`, "System Reloaded" ); await interaction.editReply({ embeds: [embed] }); } catch (error) { console.error(error); await interaction.editReply({ embeds: [createErrorEmbed("An error occurred while reloading commands. Check console for details.", "Reload Failed")] }); } } });