This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |