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 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) {
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;
}
resolve('Command executed successfully');
});
process.on('error', (error) => {
reject(`error: ${error.message}`);
return;
}
if (stderr) {
reject(`stderr: ${stderr}`);
return;
}
resolve(stdout);
});
});
}
@ -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`