From eb92edb19a4fad1153a2e2433b089e87327c0a69 Mon Sep 17 00:00:00 2001 From: shnimlz Date: Tue, 23 Sep 2025 23:37:50 -0500 Subject: [PATCH] feat: add memory refresh button and update admin panel with memory usage details --- src/commands/messages/net/commandsAdmin.ts | 91 ++++++++++++++-------- src/components/buttons/cmdMemRefresh.ts | 24 ++++++ 2 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 src/components/buttons/cmdMemRefresh.ts diff --git a/src/commands/messages/net/commandsAdmin.ts b/src/commands/messages/net/commandsAdmin.ts index 0558509..db3fac5 100644 --- a/src/commands/messages/net/commandsAdmin.ts +++ b/src/commands/messages/net/commandsAdmin.ts @@ -4,6 +4,59 @@ import { CommandMessage } from "../../../core/types/commands"; const OWNER_ID = '327207082203938818'; +function formatBytesMB(bytes: number) { + return (bytes / 1024 / 1024).toFixed(1) + 'MB'; +} + +function buildAdminPanel() { + const m = process.memoryUsage(); + const rss = formatBytesMB(m.rss); + const heapUsed = formatBytesMB(m.heapUsed); + const heapTotal = formatBytesMB(m.heapTotal); + const ext = formatBytesMB(m.external); + const now = new Date(); + const ts = now.toISOString().replace('T', ' ').split('.')[0]; + + return { + type: 17, + accent_color: 0x2b2d31, + components: [ + { + type: 10, + content: '### 🛠️ Panel de Administración de Comandos\nGestiona el registro y limpieza de comandos **Slash**.' + }, + { type: 14, divider: true, spacing: 1 }, + { + type: 10, + content: 'Acciones disponibles:\n• Registrar comandos de GUILD (testing)\n• Registrar comandos GLOBAL (propagación lenta)\n• Limpiar comandos de GUILD\n• Limpiar comandos GLOBAL\n\nUsa los botones de abajo. Se evita ejecución simultánea.' + }, + { type: 14, divider: true, spacing: 1 }, + { + type: 10, + content: `**Memoria (actual)**\n• RSS: ${rss}\n• Heap Used: ${heapUsed}\n• Heap Total: ${heapTotal}\n• External: ${ext}\n\nÚltima actualización: ${ts} UTC` + }, + { type: 14, divider: false, spacing: 1 }, + // Fila 1 (acciones de registro) + { + type: 1, + components: [ + { type: 2, style: 1, label: 'Registrar GUILD', custom_id: 'cmd_reg_guild' }, + { type: 2, style: 1, label: 'Registrar GLOBAL', custom_id: 'cmd_reg_global' }, + { type: 2, style: 2, label: '🔄 Refrescar Memoria', custom_id: 'cmd_mem_refresh' } + ] + }, + // Fila 2 (acciones de limpieza) + { + type: 1, + components: [ + { type: 2, style: 4, label: 'Limpiar GUILD', custom_id: 'cmd_clear_guild' }, + { type: 2, style: 4, label: 'Limpiar GLOBAL', custom_id: 'cmd_clear_global' } + ] + } + ] + }; +} + export const command: CommandMessage = { name: 'admin-comandos', type: 'message', @@ -15,42 +68,14 @@ export const command: CommandMessage = { return; } - const panel = { - type: 17, - accent_color: 0x2b2d31, - components: [ - { - type: 10, - content: '### 🛠️ Panel de Administración de Comandos\nGestiona el registro y limpieza de comandos **Slash**.' - }, - { type: 14, divider: true, spacing: 1 }, - { - type: 10, - content: 'Acciones disponibles:\n• Registrar comandos de GUILD (testing)\n• Registrar comandos GLOBAL (propagación lenta)\n• Limpiar comandos de GUILD\n• Limpiar comandos GLOBAL\n\nUsa los botones de abajo. Se evita ejecución simultánea.' - } - ] - }; - - const rows = [ - { - type: 1, - components: [ - { type: 2, style: 1, label: 'Registrar GUILD', custom_id: 'cmd_reg_guild' }, - { type: 2, style: 1, label: 'Registrar GLOBAL', custom_id: 'cmd_reg_global' } - ] - }, - { - type: 1, - components: [ - { type: 2, style: 4, label: 'Limpiar GUILD', custom_id: 'cmd_clear_guild' }, - { type: 2, style: 4, label: 'Limpiar GLOBAL', custom_id: 'cmd_clear_global' } - ] - } - ]; + const panel = buildAdminPanel(); await message.reply({ flags: 32768, - components: [panel, ...rows] + components: [panel] }); } }; + +// Exportamos builder para reutilizar en el botón de refresco +export { buildAdminPanel }; diff --git a/src/components/buttons/cmdMemRefresh.ts b/src/components/buttons/cmdMemRefresh.ts new file mode 100644 index 0000000..b927bff --- /dev/null +++ b/src/components/buttons/cmdMemRefresh.ts @@ -0,0 +1,24 @@ +import type { ButtonInteraction } from 'discord.js'; +import { buildAdminPanel } from '../../commands/messages/net/commandsAdmin'; + +const OWNER_ID = '327207082203938818'; + +export default { + customId: 'cmd_mem_refresh', + run: async (interaction: ButtonInteraction) => { + if (interaction.user.id !== OWNER_ID) { + return interaction.reply({ content: '❌ No autorizado.', ephemeral: true }); + } + try { + await interaction.deferUpdate(); + const panel = buildAdminPanel(); + // Edita el mensaje original reemplazando componentes (solo el contenedor con filas internas) + await interaction.message.edit({ components: [panel] }); + } catch (e) { + console.error('Error refrescando panel de memoria:', e); + if (!interaction.deferred && !interaction.replied) + await interaction.reply({ content: '❌ Error refrescando panel.', ephemeral: true }); + } + } +}; +