UNPKG

djskage

Version:

A Discord.js extension for utility commands

64 lines (63 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const discord_js_1 = require("discord.js"); const child_process_1 = require("child_process"); module.exports = { name: "shell", aliases: ["sh"], description: "Execute shell commands securely within a sandboxed environment.", usage: "shell <command>", execute: async (kage, client, ctx) => { const args = ctx.content.split(" ").slice(2); if (!args.length) return ctx.reply("❌ No command provided."); const stopButton = new discord_js_1.ButtonBuilder() .setCustomId("stop_shell") .setLabel("Stop Execution") .setStyle(discord_js_1.ButtonStyle.Danger); const row = new discord_js_1.ActionRowBuilder().addComponents(stopButton); const initialMessage = await ctx.reply({ content: "```\nExecuting...\n```", components: [row], }); const process = (0, child_process_1.spawn)(args.join(" "), { shell: true, stdio: ["pipe", "pipe", "pipe"], detached: true, }); const timeout = setTimeout(() => process.kill("SIGKILL"), 120000); const collectOutput = async (p) => { return new Promise((resolve) => { let output = ""; p.stdout.on("data", (data) => { output += data.toString(); console.log(output); initialMessage .edit({ content: `\`\`\`\n${output.slice(-1900)}\n\`\`\``, components: [row], }) .catch(() => { }); }); p.stderr.on("data", (data) => { output += `Error: ${data.toString()}`; initialMessage .edit({ content: `\`\`\`\n${output.slice(-1900)}\n\`\`\``, components: [row], }) .catch(() => { }); }); p.on("close", (code) => { clearTimeout(timeout); resolve({ output, code }); }); }); }; const { output, code } = await collectOutput(process); await initialMessage.edit({ content: `\`\`\`\nProcess finished with code ${code}\nFinal output:\n${output.trim() || "No output received."}\n\`\`\``, components: [], }); }, };