import sql from "../sql"; import { maxoutput, sandbox, bfgen } from "./lib/sandbox"; import vm from "vm"; import rp from "request-promise"; import stringify from "stringify-object"; let _contexts = new Map(); sql.any("select prefix, sandbox from nxy_users where sandbox != 'NULL'") .then(rows => rows.forEach(row => eval(`_contexts.set(row.prefix, ${JSON.parse(row.sandbox)});`))) .catch(err => console.log("nichts vorhanden lol", err)); export default bot => { bot._trigger.set("sandbox_js", new bot.trigger({ call: /^(\.|\/)js (.*)/i, set: "uwe", f: e => { const args = e.message.trim().substring(4); if (!_contexts.has(e.user.prefix)) _contexts.set(e.user.prefix, vm.createContext({ })); if (args.match(/^reset$/)) { _contexts.set(e.user.prefix, vm.createContext({ })); return e.reply("Sandbox resetted!"); } let context = vm.createContext(_contexts.get(e.user.prefix)); try { let output = vm.runInContext(args, context, { timeout: 2000 }); if (typeof output !== undefined && output) { output = JSON.stringify(output); if (output.length > maxoutput) return e.reply(`holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)`); else { _contexts.set(e.user.prefix, context); let tmp = JSON.stringify(stringify(_contexts.get(e.user.prefix)) .replace(/\n/g, "") .replace(/\t/g, "") .split("[native code]").join("")); sql.any("insert into nxy_users (nick, prefix, sandbox) values (lower($1), $3, $2) on conflict (prefix) do update set sandbox = $2, nick = lower($1)", [ (e.type === "tg" ? e.user.username || e.user.nick : e.user.nick), tmp, e.user.prefix ]).then(() => e.reply(output)) .catch(err => console.log(err)); } } } catch (err) { e.reply(err.message.length > maxoutput ? `holy fuck, Error wäre viel zu lang! (${err.message.length} Zeichen :DDDDDD)` : JSON.stringify(err.message)); } } })); bot._trigger.set("sandbox", new bot.trigger({ call: /^\!(hs|py|cpp|bf|php|lua|bash) .*/i, set: "uwe", f: e => { let args = e.message.trim().substring(1).split(" "); const lang = args.shift(); args = args.join(" "); sandbox(lang, args).then( resolve => e.reply(`${e.user.nick}: ${resolve}`), reject => e.reply(reject.replace("\n", " ")) ); } })); bot._trigger.set("sandbox_rs", new bot.trigger({ call: /^\!rs (.*)/i, set: "uwe", f: e => { const args = e.message.substring(4); const params = { channel: "stable", code: "fn main() { " + args + " }", crateType: "bin", mode: "debug", tests: false }; const options = { url: "https://play.rust-lang.org/execute", method: "POST", body: params, json: true }; rp(options).then(body => { e.reply(body.success ? (body.stdout.length > maxoutput ? `holy fuck, Ausgabe wäre viel zu lang! (${body.stdout.length} Zeichen :DDDDDD)` : body.stdout) : "error!"); }) .catch(err => { e.reply(err); }); } })); bot._trigger.set("bfgen", new bot.trigger({ call: /^\!bfgen .*/i, set: "uwe", f: e => { let args = e.message.trim().substring(7); let output = bfgen(args); e.reply(output.length > maxoutput ? `holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)` : output); } })); };