From 2f0795b34fb62de73da4b6cb292b335c0c8c0bf3 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 00:58:56 +0100 Subject: [PATCH] Added eval command --- src/commands/eval.ts | 50 +++++++++++++++++++++++++++++++++++++++++++ src/commands/index.ts | 1 + 2 files changed, 51 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..be7896c --- /dev/null +++ b/src/commands/eval.ts @@ -0,0 +1,50 @@ +/* eslint-disable no-eval */ +import { Message } from 'eris'; +import { inspect } from 'util'; +import axios from 'axios'; +import { Client, config } from '..'; +import { Command } from '../class'; + +export default class Eval extends Command { + constructor(client: Client) { + super(client); + this.name = 'eval'; + this.aliases = ['e']; + this.description = 'Evaluate JavaScript code'; + this.enabled = true; + this.permissions = { users: ['253600545972027394', '278620217221971968'] }; + } + + public async run(message: Message) { + try { + const evalMessage = message.content.slice(config.prefix.length).split(' ').slice(1).join(' '); + let evaled: any; + let output: string; + + try { + evaled = await eval(evalMessage); + if (typeof evaled !== 'string') output = output && inspect(evaled, { depth: 1 }); + } catch (error) { + output = error.stack; + } + + if (output) { + output = output.replace(RegExp(config.prefix, 'gi'), 'juul'); + output = output.replace(RegExp(config.emailPass, 'gi'), 'juul'); + output = output.replace(RegExp(config.cloudflare, 'gi'), 'juul'); + } + + const display = this.client.util.splitString(output, 1975); + if (display[5]) { + const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', display.join('')); + return message.channel.createMessage(`${this.client.stores.emojis.success} Your evaluation output can be found on https://snippets.cloud.libraryofcode.org/${data.key}`); + } + + return display.forEach((m) => { + message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``); + }); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 07817bd..0f1d40a 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -4,3 +4,4 @@ export { default as Ping } from './ping'; export { default as Announce } from './announce'; export { default as CWG } from './cwg'; export { default as Help } from './help'; +export { default as Eval } from './eval';