chipmunkbot3/plugins/eval.js

131 lines
3.9 KiB
JavaScript

const vm = require('vm')
const util = require('util')
let clearString = ''
for (let i = 0; i < 100; i++) { clearString += '\n' }
function inject (bot) {
bot.eval = function eval(code) {
const context = bot.eval.context
return vm.runInContext(code, context)
}
reset()
function reset () {
const context = vm.createContext() // create the context
context._reset = reset // reset function
vm.runInContext('const reset = _reset', context)
delete context._reset
context.global = context // global
const Error = vm.runInContext('Error', context) // errors
Error.stackTraceLimit = 0
/*context._Error = () => Error()
vm.runInContext('const Error = _Error', context)
delete context._Error*/
function _log (...data) { // console
bot.core.run('minecraft:tellraw @a ' + JSON.stringify(util.format(...data)))
}
// TODO: Implement more
context.console = {
log (...data) { _log(...data) },
warn (...data) { _log(...data) },
dir (item, options) {
bot.core.run('minecraft:tellraw @a ' + JSON.stringify(util.inspect(item)))
},
/*time: [Function: time],
timeEnd: [Function: timeEnd],
timeLog: [Function: timeLog],*/
trace (...data) { _log('Trace:', ...data) },
//assert: [Function: assert],
clear () { bot.core.run('minecraft:tellraw @a ' + JSON.stringify(clearString)) },
/*count: [Function: count],
countReset: [Function: countReset],
group: [Function: group],
groupEnd: [Function: groupEnd],
table: [Function: table],*/
debug (...data) { _log(...data) },
info (...data) { _log(...data) },
// dirxml: [Function: dirxml],
error (...data) { _log(...data) },
/*groupCollapsed: [Function: groupCollapsed],
profile: [Function: profile],
profileEnd: [Function: profileEnd],
timeStamp: [Function: timeStamp],*/
context () { return context.console }
}
context.core = bot.core.run // core function
// set/clear interval/timeout/immediate
//context.clearInterval = clearInterval
context.clearTimeout = clearTimeout
//context.setInterval = setInterval
context.setTimeout = setTimeout
//context.clearImmediate = clearImmediate
//context.setImmediate = setImmediate
bot.eval.context = context
}
}
/*function coloredInspect (func) {
const oldColors = util.inspect.colors
const prefix = '\x1b['
util.inspect.colors = {
reset: [ '\xa7', 'r' ],
bold: [ '\xa7', 'l' ],
dim: [ '', '' ],
italic: [ '\xa7', 'o' ],
underline: [ '\xa7', 'n' ],
blink: [ '\xa7', 'k' ],
inverse: [ '', '' ],
hidden: [ '', '' ],
strikethrough: [ '\xa7', 'm' ],
doubleunderline: [ '\xa7', 'n' ],
black: [ '\xa7', '0' ],
red: [ '\xa7', '4' ],
green: [ '\xa7', '2' ],
yellow: [ '\xa7', '6' ],
blue: [ '\xa7', '1' ],
magenta: [ '\xa7', '6' ],
cyan: [ '\xa7', '3' ],
white: [ '\xa7', 'f' ],
bgBlack: [ '\xa7', '0' ],
bgRed: [ '\xa7', '4' ],
bgGreen: [ '\xa7', '2' ],
bgYellow: [ '\xa7', '6' ],
bgBlue: [ '\xa7', '1' ],
bgMagenta: [ '\xa7', '6' ],
bgCyan: [ '\xa7', '3' ],
bgWhite: [ '\xa7', 'f' ],
framed: [ '', '' ],
overlined: [ '\xa7', 'n' ],
gray: [ '\xa7', '7' ],
redBright: [ '\xa7', 'c' ],
greenBright: [ '\xa7', 'a' ],
yellowBright: [ '\xa7', 'e' ],
blueBright: [ '\xa7', '9' ],
magentaBright: [ '\xa7', 'd' ],
cyanBright: [ '\xa7', 'b' ],
whiteBright: [ '\xa7', 'f' ],
bgGray: [ '\xa7', '7' ],
bgRedBright: [ '\xa7', 'c' ],
bgGreenBright: [ '\xa7', 'a' ],
bgYellowBright: [ '\xa7', 'e' ],
bgBlueBright: [ '\xa7', '9' ],
bgMagentaBright: [ '\xa7', 'd' ],
bgCyanBright: [ '\xa7', 'b' ],
bgWhiteBright: [ '\xa7', 'f' ],
}
const result = func().replace(/\x1b\[\xa7.?/, m => m.slice(prefix.length))
util.inspect.colors = oldColors
return result
}*/
module.exports = inject