Add utils.script.runner

This commit is contained in:
pyoor 2018-05-21 12:18:09 -04:00
parent 6455376a97
commit 40eee0a442

View file

@ -87,6 +87,31 @@ class script extends utils {
return `${random.number()} % ${o.pick(objName)}.length` return `${random.number()} % ${o.pick(objName)}.length`
} }
static runner (cmds) {
/**
* Wrap command(s) in setInterval, setTimeout, loop or run directly
* @param {Object|String} cmds - Command(s) to be executed
*/
cmds = (Array.isArray(cmds)) ? cmds : [cmds]
cmds = cmds.filter(function (i) { return i !== undefined })
if (cmds.length) {
if (random.chance(50)) {
// Wrap each command in try/catch for use in setInterval, setTimeout, repeater
switch (random.number(3)) {
case 0:
return `setInterval(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )`
case 1:
return `setTimeout(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )`
case 2:
let n = random.number(random.number(30))
return `for (let i = 0; i < ${n}; i++) { ${utils.script.safely(cmds)} }`
}
} else {
return cmds
}
}
}
static safely (obj) { static safely (obj) {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
return obj.map(s => utils.script.safely(s)).join(' ') return obj.map(s => utils.script.safely(s)).join(' ')