save sandbox in db

This commit is contained in:
Flummi
2017-11-24 19:10:30 +01:00
parent a8da8103f8
commit 11aee08a5d
3 changed files with 60 additions and 12 deletions

View File

@ -1,27 +1,49 @@
import { maxoutput, sandbox, bfgen, hsimports } from "./lib/sandbox.js";
import sql from "../sql.js";
import { maxoutput, sandbox, bfgen } from "./lib/sandbox.js";
const vm = require("vm")
, rp = require("request-promise");
, rp = require("request-promise")
, stringify = require("stringify-object");
let _contexts = new Map();
sql.exec("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);
});
module.exports = bot => {
bot._trigger.set("sandbox_js", {
call: /^\!js (.*)/i,
level: 100,
level: 0,
active: true,
clients: ["irc", "tg"],
f: e => {
const args = e.message.substring(3);
const context = {
e: e,
t: bot._trigger,
hsimports: hsimports,
};
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.runInNewContext(args, context);
let output = vm.runInContext(args, context, { timeout: 2000 });
if (typeof output !== undefined && output) {
output = JSON.stringify(output);
if (output !== "Converting circular structure to JSON") {
e.reply(output.length > maxoutput ? `holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)` : 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);
sql.exec("update `useless` set `data` = ? where `trigger` = 'sandbox_js';", JSON.stringify(stringify([..._contexts]).replace(/\n/g, "").replace(/\t/g, "")))
.then(() => {
e.reply(output);
})
.catch(err => {
console.log(err);
});
}
}
}