From b987ec97550a27485a92a76d57320b2c3b3dade4 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Fri, 23 Jul 2021 16:44:09 -0600 Subject: [PATCH] bot: add exec --- src/modules/bot.js | 82 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/modules/bot.js b/src/modules/bot.js index d7d64e7..1e0a778 100644 --- a/src/modules/bot.js +++ b/src/modules/bot.js @@ -2,10 +2,48 @@ const Command = require("../lib/command.js"); const CATEGORY = "bot"; const logger = require("npmlog"); +const child_process = require("child_process"); const {inspect} = require("util"); +const {resolve} = require("path"); const {hastebin} = require("../lib/utils.js"); +function spawn(args) { + const shell = + process.env.SHELL || (process.platform == "win32" ? "powershell" : "bash"); + + const newArgs = []; + if (shell.match(/powershell/i) && process.platform == "win32") { + newArgs.push("-NoLogo", "-Command"); + } else { + newArgs.push("-c"); + } + newArgs.push(args); + + const proc = child_process.spawn(shell, newArgs, { + cwd: resolve(__dirname, "..", ".."), + }); + return { + pid: proc.pid, + stdout: { + on: (event, handler) => + proc.stdout.on(event, (data) => { + handler(data.toString("utf8")); + }), + }, + stderr: { + on: (event, handler) => + proc.stderr.on(event, (data) => { + handler(data.toString("utf8")); + }), + }, + on: (event, handler) => + proc.on(event, (data) => { + handler(data.toString("utf8")); + }), + }; +} + const reload = new Command("reload"); reload.ownerOnly = true; reload.category = CATEGORY; @@ -70,7 +108,7 @@ _eval.callback = async function (msg, line) { return ":warning: Output (errored):\n```js\n" + out + "\n```"; } else { if (out.toString().length > 1980) { - const code = hastebin(out.toString()); + const code = await hastebin(out.toString()); return `\u2705 Output too long to send in a message: ${hf.config.haste_provider}/${code}`; } else { return "\u2705 Output:\n```js\n" + out + "\n```"; @@ -78,3 +116,45 @@ _eval.callback = async function (msg, line) { } }; hf.registerCommand(_eval); + +const exec = new Command("exec"); +exec.elevatedOnly = true; +exec.category = CATEGORY; +exec.helpText = "Executes a command"; +exec.callback = async function (msg, line) { + const proc = spawn(line); + let out = `Spawned ${proc.pid}: \`${line}'`; + proc.stdout.on("data", (data) => { + out += data + "\n"; + }); + proc.stderr.on("data", (data) => { + out += data + "\n"; + }); + + proc.on("close", async (code) => { + out += `====\nExited with ${code}`; + if (out.length > 1980) { + const code = await hastebin(out); + msg.channel.createMessage({ + content: `Output too long to send in a message: ${hf.config.haste_provider}/${code}`, + allowedMentions: { + repliedUser: false, + }, + messageReference: { + messageID: msg.id, + }, + }); + } else { + msg.channel.createMessage({ + content: `\`\`\`\n${out}\`\`\``, + allowedMentions: { + repliedUser: false, + }, + messageReference: { + messageID: msg.id, + }, + }); + } + }); +}; +hf.registerCommand(exec);