Do callbacks on admin commands

This commit is contained in:
Christopher Cookman 2025-01-25 09:15:14 -07:00
parent f26b8b22de
commit a6dcfeedb6

View file

@ -1,4 +1,5 @@
const { exec } = require('child_process'); const { exec } = require('child_process');
const process = require('child_process').exec(command);
const pool = global.pool const pool = global.pool
const fpbx = global.fpbx const fpbx = global.fpbx
@ -6,18 +7,29 @@ const client = global.client
const log = global.log const log = global.log
const runCommand = (command) => { const runCommand = (command, onData) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
require('child_process').exec(command, (error, stdout, stderr) => {
if (error) { process.stdout.on('data', (data) => {
reject(`error: ${error.message}`); 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; return;
} }
if (stderr) { resolve('Command executed successfully');
reject(`stderr: ${stderr}`); });
return;
} process.on('error', (error) => {
resolve(stdout); reject(`error: ${error.message}`);
}); });
}); });
} }
@ -34,8 +46,9 @@ module.exports.execute = async (interaction) => {
}); });
break; break;
case 'reload': // Run `fwconsole reload` case 'reload': // Run `fwconsole reload`
runCommand('fwconsole reload').then((res) => { await interaction.deferReply({ ephemeral: true });
interaction.reply({ content: res, ephemeral: true }); runCommand('fwconsole reload', (data) => {
interaction.editReply({ content: data, ephemeral: true });
}); });
break; break;
case 'reboot': // Run `reboot 0` case 'reboot': // Run `reboot 0`