Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
feat: eval command (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexthemaster authored Aug 1, 2020
2 parents 000d93c + 6c84bb1 commit e728547
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/commands/Owner/Eval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Command } from '../../lib/structures/Command';
import { MoonlightClient } from '../../lib/Client';
import { BasePool } from '../../lib/structures/Pools/Base/BasePool';
import { Stopwatch } from '../../lib/util';
import { inspect } from 'util';
import { Message } from 'discord.js';

/** @ignore */
export default class extends Command {
constructor(client: MoonlightClient, pool: BasePool<string, Command>) {
super(client, pool, {
ownerOnly: true,
usage: '<toEval:string>',
description: "Evaluates JavaScript. You can use the --async flag to be able to use await inside the command (be sure to have a return statement!) or the --inspect=number (number being the depth of the inspection) flag to display more things inside an object!"
});

this.customizeResponse('toEval', 'Please provide a string to eval!');
}

public async run(message: Message, args: EvalCommandArgs) {
const stopwatch = new Stopwatch();

let evaled: string;
if (this.flags.has('async')) evaled = await eval(`(async () => {${args.toEval}})()`);
else evaled = eval(args.toEval);

stopwatch.stop();

if (this.flags.has('inspect')) evaled = inspect(evaled, undefined, Number(this.flags.get('inspect')) || undefined);

evaled = String(evaled);
if (!evaled.length) return message.channel.send(`The eval didn't return any value!\nTook: ${stopwatch.getElapsedHuman}`);
if (evaled.length >= 1940) return message.channel.send(`Output too long... attached the output in a file!\nTook: ${stopwatch.getElapsedHuman}`, { files: [{ name: 'output.txt', attachment: Buffer.from(evaled) }] });
return message.channel.send(`\`\`\`js\n${evaled}\`\`\`\nTook: ${stopwatch.getElapsedHuman}`)
}
}

interface EvalCommandArgs {
toEval: string;
}

0 comments on commit e728547

Please sign in to comment.