bot: add exec
This commit is contained in:
parent
6bca271729
commit
b987ec9755
1 changed files with 81 additions and 1 deletions
|
@ -2,10 +2,48 @@ const Command = require("../lib/command.js");
|
||||||
const CATEGORY = "bot";
|
const CATEGORY = "bot";
|
||||||
|
|
||||||
const logger = require("npmlog");
|
const logger = require("npmlog");
|
||||||
|
const child_process = require("child_process");
|
||||||
const {inspect} = require("util");
|
const {inspect} = require("util");
|
||||||
|
const {resolve} = require("path");
|
||||||
|
|
||||||
const {hastebin} = require("../lib/utils.js");
|
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");
|
const reload = new Command("reload");
|
||||||
reload.ownerOnly = true;
|
reload.ownerOnly = true;
|
||||||
reload.category = CATEGORY;
|
reload.category = CATEGORY;
|
||||||
|
@ -70,7 +108,7 @@ _eval.callback = async function (msg, line) {
|
||||||
return ":warning: Output (errored):\n```js\n" + out + "\n```";
|
return ":warning: Output (errored):\n```js\n" + out + "\n```";
|
||||||
} else {
|
} else {
|
||||||
if (out.toString().length > 1980) {
|
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}`;
|
return `\u2705 Output too long to send in a message: ${hf.config.haste_provider}/${code}`;
|
||||||
} else {
|
} else {
|
||||||
return "\u2705 Output:\n```js\n" + out + "\n```";
|
return "\u2705 Output:\n```js\n" + out + "\n```";
|
||||||
|
@ -78,3 +116,45 @@ _eval.callback = async function (msg, line) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
hf.registerCommand(_eval);
|
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);
|
||||||
|
|
Loading…
Reference in a new issue