diff --git a/src/Util.ts b/src/Util.ts index 5fbe018..560892f 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -1,7 +1,9 @@ -import { promisify } from 'util'; +/* eslint-disable no-param-reassign */ +import { promisify, isArray } from 'util'; import childProcess from 'child_process'; import nodemailer from 'nodemailer'; import { Message, TextChannel, PrivateChannel } from 'eris'; +import { outputFile } from 'fs-extra'; import { Client } from '.'; import { Command, RichEmbed } from './class'; @@ -74,4 +76,21 @@ export default class Util { } return array; } + + public splitString(string: string, length: number): string[] { + if (!string) return []; + if (Array.isArray(string)) string = string.join('\n'); + if (string.length <= length) return [string]; + const arrayString: string[] = []; + let str: string = ''; + let pos: number; + while (string.length > 0) { + pos = string.length > length ? string.lastIndexOf('\n', length) : outputFile.length; + if (pos > length) pos = length; + str = string.substr(0, pos); + string = string.substr(pos); + arrayString.push(str); + } + return arrayString; + } } diff --git a/src/commands/announce.ts b/src/commands/announce.ts index 2bf0af4..f825536 100644 --- a/src/commands/announce.ts +++ b/src/commands/announce.ts @@ -20,7 +20,7 @@ export default class Announce extends Command { if (args[0] === '-e') await this.client.util.exec(`echo "\n\n**************************************************************************\nEMERGENCY SYSTEM BROADCAST MESSAGE | Library of Code sp-us (root enforced)\n--------------------------------------------------------------------------\n\n\n${args.slice(1).join(' ')}\n\n\n\n\n\n\n\n\n\n\n\n\n" | wall -n`); else await this.client.util.exec(`echo "\nSYSTEM BROADCAST MESSAGE | Library of Code sp-us (root enforced)\n\n\n${args.join(' ')}" | wall -n`); message.delete(); - return notification.edit(`${this.client.stores.emojis.success} ***Sent announcement to all active terminals***`); + return notification.edit(`${this.client.stores.emojis.success} ***Sent${args[0] === '-e' ? ' emergency' : ''} announcement to all active terminals***`); } catch (error) { return this.client.util.handleError(error, message, this); } diff --git a/src/commands/eval.ts b/src/commands/eval.ts new file mode 100644 index 0000000..44efeab --- /dev/null +++ b/src/commands/eval.ts @@ -0,0 +1,52 @@ +/* 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]) { + try { + 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}`); + } catch (error) { + return message.channel.createMessage(`${this.client.stores.emojis.error} ${error}`); + } + } + + 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/exec.ts b/src/commands/exec.ts new file mode 100644 index 0000000..6728171 --- /dev/null +++ b/src/commands/exec.ts @@ -0,0 +1,41 @@ +import { Message } from 'eris'; +import axios from 'axios'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class Exec extends Command { + constructor(client: Client) { + super(client); + this.name = 'exec'; + this.description = 'Executes command'; + this.aliases = ['ex']; + this.enabled = true; + this.permissions = { users: ['253600545972027394', '278620217221971968'] }; + } + + public async run(message: Message, args: string[]) { + try { + if (!args.length) return this.client.commands.get('help').run(message, [this.name]); + + const response = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Executing \`${args.join(' ')}\`***`); + const result = await this.client.util.exec(args.join(' ')); + + if (result.length <= 1975) return response.edit(`\`\`\`bash\n${result}\n\`\`\``); + const splitResult = this.client.util.splitString(result, 1975); + + if (splitResult[5]) { + try { + const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', splitResult.join('')); + return response.edit(`${this.client.stores.emojis.success} Your command execution output can be found on https://snippets.cloud.libraryofcode.org/${data.key}`); + } catch (error) { + return response.edit(`${this.client.stores.emojis.error} ${error}`); + } + } + + await response.delete(); + return splitResult.forEach((m) => message.channel.createMessage(`\`\`\`bash\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..8f7379d 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -4,3 +4,5 @@ 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'; +export { default as Exec } from './exec';