From a6dcfeedb609809024834841620f336de5d00de5 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Sat, 25 Jan 2025 09:15:14 -0700 Subject: [PATCH] Do callbacks on admin commands --- interactionHandlers/commands/admin.js | 35 ++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/interactionHandlers/commands/admin.js b/interactionHandlers/commands/admin.js index ca00737..0310f72 100644 --- a/interactionHandlers/commands/admin.js +++ b/interactionHandlers/commands/admin.js @@ -1,4 +1,5 @@ const { exec } = require('child_process'); +const process = require('child_process').exec(command); const pool = global.pool const fpbx = global.fpbx @@ -6,18 +7,29 @@ const client = global.client const log = global.log -const runCommand = (command) => { +const runCommand = (command, onData) => { return new Promise((resolve, reject) => { - require('child_process').exec(command, (error, stdout, stderr) => { - if (error) { - reject(`error: ${error.message}`); + + process.stdout.on('data', (data) => { + if (onData) { + onData(data); + } + }); + + process.stderr.on('data', (data) => { + reject(`stderr: ${data}`); + }); + + process.on('close', (code) => { + if (code !== 0) { + reject(`process exited with code ${code}`); return; } - if (stderr) { - reject(`stderr: ${stderr}`); - return; - } - resolve(stdout); + resolve('Command executed successfully'); + }); + + process.on('error', (error) => { + reject(`error: ${error.message}`); }); }); } @@ -34,8 +46,9 @@ module.exports.execute = async (interaction) => { }); break; case 'reload': // Run `fwconsole reload` - runCommand('fwconsole reload').then((res) => { - interaction.reply({ content: res, ephemeral: true }); + await interaction.deferReply({ ephemeral: true }); + runCommand('fwconsole reload', (data) => { + interaction.editReply({ content: data, ephemeral: true }); }); break; case 'reboot': // Run `reboot 0`