Skip to content

Instantly share code, notes, and snippets.

@appellation
Last active January 24, 2017 23:27
Show Gist options
  • Save appellation/5406eba6d4a64ae3e17222d3f62c6998 to your computer and use it in GitHub Desktop.
Save appellation/5406eba6d4a64ae3e17222d3f62c6998 to your computer and use it in GitHub Desktop.
function Eval(msg, args) {
const input = ':arrow_right: **Input:**\n```js\n' + args.join(' ') + '\n```';
return new Promise((resolve, reject) => {
let ev;
try {
ev = eval(args.join(' '));
if(ev && ev instanceof Promise) {
ev.then(resolve).catch(reject);
return;
}
resolve(ev);
} catch(err) {
reject(err);
}
}).then(res => {
let out;
if(typeof res === 'object' && typeof res !== 'string') {
out = require('util').inspect(res);
if(typeof out == 'string' && out.length > 1900) {
out = res.toString();
}
} else {
out = res;
}
return msg.edit(input + ':white_check_mark: **Output:**\n```js\n' + out + '\n```');
}).catch(err => {
return msg.edit(input + ':x: **Error:**\n```js\n' + (err.message || err) + '\n```');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment