2020-02-01 23:23:36 +00:00
|
|
|
import { Command } from '@plugins/Command';
|
|
|
|
import { defaultEmbed } from '@util/DefaultEmbed';
|
2020-01-19 18:55:35 +00:00
|
|
|
import { inspect } from 'util';
|
|
|
|
import { runInNewContext } from 'vm';
|
|
|
|
|
|
|
|
function parseBlock(script: string) {
|
|
|
|
const cbr = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm;
|
|
|
|
const result = cbr.exec(script);
|
|
|
|
if (result) {
|
|
|
|
return result[4];
|
|
|
|
}
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run(
|
|
|
|
script: string,
|
|
|
|
ctx: object,
|
|
|
|
opts: object
|
|
|
|
): Promise<string | Error> {
|
|
|
|
try {
|
|
|
|
const result = await runInNewContext(
|
|
|
|
`(async () => { ${script} })()`,
|
|
|
|
ctx,
|
|
|
|
opts
|
|
|
|
);
|
|
|
|
if (typeof result !== 'string') {
|
|
|
|
return inspect(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeCodeBlock(data: string, lang?: string) {
|
|
|
|
return `\`\`\`${lang}\n${data}\n\`\`\``;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const command = new Command(
|
|
|
|
'eval',
|
|
|
|
async (lifeguard, msg, args, dbUser) => {
|
|
|
|
const start = Date.now();
|
|
|
|
|
|
|
|
const script = parseBlock(args.join(' '));
|
|
|
|
const exec = await run(
|
|
|
|
script,
|
|
|
|
{
|
|
|
|
lifeguard,
|
|
|
|
msg,
|
2020-01-25 17:02:34 +00:00
|
|
|
defaultEmbed,
|
2020-01-19 18:55:35 +00:00
|
|
|
dbUser,
|
|
|
|
},
|
|
|
|
{ filename: msg.guild?.id.toString() }
|
|
|
|
);
|
|
|
|
|
|
|
|
const end = Date.now();
|
|
|
|
|
|
|
|
if (typeof exec === 'string') {
|
2020-01-25 17:02:34 +00:00
|
|
|
const embed = defaultEmbed()
|
2020-01-19 18:55:35 +00:00
|
|
|
.addField('Input', makeCodeBlock(script, 'js'))
|
|
|
|
.addField('Output', makeCodeBlock(exec, 'js'))
|
2020-01-25 17:02:34 +00:00
|
|
|
.setFooter(`Script Executed in ${end - start}ms`);
|
2020-01-19 18:55:35 +00:00
|
|
|
msg.channel.send(embed);
|
|
|
|
} else {
|
2020-01-25 17:02:34 +00:00
|
|
|
const embed = defaultEmbed()
|
2020-01-19 18:55:35 +00:00
|
|
|
.addField('Input', makeCodeBlock(script, 'js'))
|
|
|
|
.addField('Output', makeCodeBlock(`${exec.name}: ${exec.message}`))
|
2020-01-25 17:02:34 +00:00
|
|
|
.setFooter(`Script Executed in ${end - start}ms`);
|
2020-01-19 18:55:35 +00:00
|
|
|
msg.channel.send(embed);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
level: 5,
|
|
|
|
usage: ['eval {code}'],
|
|
|
|
}
|
|
|
|
);
|