From 7449aedd92ff32f5fa31914aef766162b580a9ac Mon Sep 17 00:00:00 2001 From: Matthew R Date: Wed, 15 Apr 2020 15:13:29 -0400 Subject: [PATCH] add eval command --- src/commands/eval.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/commands/eval.ts diff --git a/src/commands/eval.ts b/src/commands/eval.ts new file mode 100644 index 0000000..4531d13 --- /dev/null +++ b/src/commands/eval.ts @@ -0,0 +1,68 @@ +import axios from 'axios'; +import { inspect } from 'util'; +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class Eval extends Command { + constructor(client: Client) { + super(client); + this.name = 'eval'; + this.description = 'Evaluates native JS code'; + this.aliases = ['e']; + this.permissions = 4; + this.enabled = true; + this.guildOnly = false; + } + + public async run(message: Message, args: string[]) { + try { + const evalMessage = message.content.slice(this.client.config.prefix.length).trim().split(' ').slice(1); + let evalString = evalMessage.join(' ').trim(); + let evaled: any; + let depth = 0; + + if (args[0] && args[0].startsWith('-d')) { + depth = Number(args[0].replace('-d', '')); + if (!depth || depth < 0) depth = 0; + const index = evalMessage.findIndex((v) => v.startsWith('-d')) + 1; + evalString = evalMessage.slice(index).join(' ').trim(); + } + if (args[0] === '-a') { + const index = evalMessage.findIndex((v) => v === '-a') + 1; + evalString = `(async () => { ${evalMessage.slice(index).join(' ').trim()} })()`; + } + + try { + // eslint-disable-next-line no-eval + evaled = await eval(evalString); + if (typeof evaled !== 'string') { + evaled = inspect(evaled, { depth }); + } + if (evaled === undefined) { + evaled = 'undefined'; + } + } catch (error) { + evaled = error.stack; + } + + evaled = evaled.replace(new RegExp(this.client.config.token, 'gi'), 'juul'); + // evaled = evaled.replace(new RegExp(this.client.config.emailPass, 'gi'), 'juul'); + // evaled = evaled.replace(new RegExp(this.client.config.cloudflare, 'gi'), 'juul'); + + + const display = this.client.util.splitString(evaled, 1975); + if (display[5]) { + try { + const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', display.join('')); + return message.channel.createMessage(`${this.client.util.emojis.SUCCESS} Your evaluation evaled can be found on https://snippets.cloud.libraryofcode.org/${data.key}`); + } catch (error) { + return message.channel.createMessage(`${this.client.util.emojis.ERROR} ${error}`); + } + } + + return display.forEach((m) => message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``)); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +}