From f68bccb5aabe588d9deb6c1155c87c5bf35cdb22 Mon Sep 17 00:00:00 2001 From: Kovacs Alex Date: Sat, 1 Aug 2020 17:19:21 +0300 Subject: [PATCH 1/2] feat: eval command --- src/commands/Owner/Eval.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/commands/Owner/Eval.ts diff --git a/src/commands/Owner/Eval.ts b/src/commands/Owner/Eval.ts new file mode 100644 index 00000000..516f8713 --- /dev/null +++ b/src/commands/Owner/Eval.ts @@ -0,0 +1,35 @@ +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 { Message } from 'discord.js'; + +/** @ignore */ +export default class extends Command { + constructor(client: MoonlightClient, pool: BasePool) { + super(client, pool, { + ownerOnly: true, + usage: '', + 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!)" + }); + + this.customizeResponse('toEval', 'Please provide a string to eval!'); + } + + public async run(message: Message, args: EvalCommandArgs) { + const stopwatch = new Stopwatch(); + + let evaled: string | null = null; + if (this.flags.has('async')) evaled = await eval(`(async () => {${args.toEval}})()`); + else evaled = eval(args.toEval); + + stopwatch.stop(); + + if (!evaled) return message.channel.send(`The eval didn't return any value!\nTook: ${stopwatch.getElapsedHuman}`); + return message.channel.send(`\`\`\`js\n${evaled}\`\`\`\nTook: ${stopwatch.getElapsedHuman}`) + } +} + +interface EvalCommandArgs { + toEval: string; +} \ No newline at end of file From 6c84bb1ce84e9c7a8a9c8decce89aaaea5a01758 Mon Sep 17 00:00:00 2001 From: Kovacs Alex Date: Sat, 1 Aug 2020 17:43:34 +0300 Subject: [PATCH 2/2] feat: add more to the eval command --- src/commands/Owner/Eval.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/commands/Owner/Eval.ts b/src/commands/Owner/Eval.ts index 516f8713..69987bac 100644 --- a/src/commands/Owner/Eval.ts +++ b/src/commands/Owner/Eval.ts @@ -2,6 +2,7 @@ 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 */ @@ -10,7 +11,7 @@ export default class extends Command { super(client, pool, { ownerOnly: true, usage: '', - 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!)" + 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!'); @@ -19,13 +20,17 @@ export default class extends Command { public async run(message: Message, args: EvalCommandArgs) { const stopwatch = new Stopwatch(); - let evaled: string | null = null; + let evaled: string; if (this.flags.has('async')) evaled = await eval(`(async () => {${args.toEval}})()`); else evaled = eval(args.toEval); stopwatch.stop(); - if (!evaled) return message.channel.send(`The eval didn't return any value!\nTook: ${stopwatch.getElapsedHuman}`); + 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}`) } }