const { exec } = require('child_process'); const Discord = require('discord.js'); const { on } = require('events'); const pool = global.pool const fpbx = global.fpbx const client = global.client const log = global.log const runCommand = (command, onData) => { try { return new Promise((resolve, reject) => { const process = exec(command); let output = ''; const timeout = setTimeout(() => { process.kill(); resolve({ output: output, code: 1 }); }, 60000); process.stdout.on('data', (data) => { output += data; if (onData) { onData(data); } }); process.stderr.on('data', (data) => { onData(data); }); process.on('close', (code) => { clearTimeout(timeout); if (code !== 0) { resolve({ output: output, code: code }); } resolve({ output: output, code: code }); }); process.on('error', (error) => { reject(`error: ${error.message}`); }); }); } catch (err) { log.error(err) } } module.exports = {}; module.exports.execute = async (interaction) => { if (interaction.user.id !== process.env.OWNER_ID) { await interaction.reply({ content: "You do not have permission to run this command.", ephemeral: true }); return; } const subcommand = interaction.options.getSubcommand(); switch (subcommand) { case 'fwconsole': // Run an arbitrary fwconsole command const command = interaction.options.getString('command'); await interaction.deferReply({ ephemeral: true }); var output = ''; runCommand(`fwconsole ${command}`, (data) => { output += data; if (output.length >= 1500) { output = output.substring(output.length - 1500); } interaction.editReply({ content: `\`\`\`ansi\n${output}\`\`\``, ephemeral: true }); }).then((fullOutput) => { output = output.length > 1500 ? output.substring(output.length - 1500) : output; const buffer = Buffer.from(fullOutput.output, 'utf-8'); const attachment = new Discord.AttachmentBuilder(buffer, {name: 'output.txt'}); interaction.editReply({ content: `\`\`\`ansi\n${output}\`\`\`\nProcess returned code ${fullOutput.code}`, files: [attachment], ephemeral: true }); }) break; case 'asterisk': // Run arbitrary asterisk command with asterisk -x "command" const asteriskCommand = interaction.options.getString('command'); await interaction.deferReply({ ephemeral: true }); var output = ''; runCommand(`asterisk -x "${asteriskCommand}"`, (data) => { output += data; if (output.length >= 1500) { output = output.substring(output.length - 1500); } interaction.editReply({ content: `\`\`\`ansi\n${output}\`\`\``, ephemeral: true }); }).then((fullOutput) => { output = output.length > 1500 ? output.substring(output.length - 1500) : output; const buffer = Buffer.from(fullOutput.output, 'utf-8'); const attachment = new Discord.AttachmentBuilder(buffer, {name: 'output.txt'}); interaction.editReply({ content: `\`\`\`ansi\n${output}\`\`\`\nProcess returned code ${fullOutput.code}`, files: [attachment], ephemeral: true }); }) break; case 'shell': // Run any arbitrary shell command const shellCommand = interaction.options.getString('command'); await interaction.deferReply({ ephemeral: true }); var output = ''; runCommand(shellCommand, (data) => { output += data; if (output.length >= 1500) { output = output.substring(output.length - 1500); } interaction.editReply({ content: `\`\`\`ansi\n${output}\`\`\``, ephemeral: true }); }).then((fullOutput) => { output = output.length > 1500 ? output.substring(output.length - 1500) : output; const buffer = Buffer.from(fullOutput.output, 'utf-8'); const attachment = new Discord.AttachmentBuilder(buffer, {name: 'output.txt'}); interaction.editReply({ content: `Process returned code ${fullOutput.code}`, files: [attachment], ephemeral: true }); }) break; } }