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 data from useless where trigger = 'sandbox_js'") .then(rows => { eval("_contexts = new Map([..."+JSON.parse(rows[0].data)+"])"); }) .catch(err => { console.log("nichts vorhanden lol", err); }); export default bot => { bot._trigger.set("sandbox_js", new bot.trigger({ call: /^\!js (.*)/i, f: e => { const args = e.message.trim().substring(4); if (!_contexts.has(`${e.network}.${e.channel}.${e.user.nick}`)) _contexts.set(`${e.network}.${e.channel}.${e.user.nick}`, vm.createContext({ })); if (args.match(/^reset$/)) { _contexts.set(`${e.network}.${e.channel}.${e.user.nick}`, vm.createContext({ })); return e.reply("Sandbox resetted!"); } let context = vm.createContext(_contexts.get(`${e.network}.${e.channel}.${e.user.nick}`)); 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.network}.${e.channel}.${e.user.nick}`, context); let tmp = JSON.stringify(stringify([..._contexts]) .replace(/\n/g, "") .replace(/\t/g, "") .split("[native code]").join("")); sql.any("update useless set data = $1 where trigger = 'sandbox_js';", [tmp]) .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, f: e => { let args = e.message.trim().substring(1).split(" "); const lang = args.shift(); args = args.join(" "); sandbox(lang, args).then( resolve => e.reply(resolve), reject => e.reply(reject.replace("\n", " ")) ); } })); bot._trigger.set("sandbox_rs", new bot.trigger({ call: /^\!rs (.*)/i, 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, 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); } })); };