From 92e4ee5cc8e236eb67ab3085400fcc2202c228fc Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 00:57:58 +0100 Subject: [PATCH 1/7] Fixes --- src/Client.ts | 2 +- src/commands/announce.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index 440e653..901fc67 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -10,7 +10,7 @@ import { Command } from './class'; export default class Client extends Eris.Client { - public config: { 'token': string; 'cloudflare': string; 'prefix': string; }; + public config: { 'token': string; 'cloudflare': string; 'prefix': string; 'emailPass': string; }; public util: Util; 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); } From d082b7be7097637400bece631bbf569da509bd59 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 00:58:20 +0100 Subject: [PATCH 2/7] Added string splitter --- src/Util.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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; + } } From 2f0795b34fb62de73da4b6cb292b335c0c8c0bf3 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 00:58:56 +0100 Subject: [PATCH 3/7] 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'; From 22bd0f5a429e88035e06e96a1b7ceec93d99fe1f Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 01:02:01 +0100 Subject: [PATCH 4/7] Added haste upload fallback so command won't disable --- src/commands/eval.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/commands/eval.ts b/src/commands/eval.ts index be7896c..e8b55ce 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -36,8 +36,12 @@ export default class Eval extends Command { 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}`); + 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) => { From e1d7237bced00b347795bf07d1530abae1e04cb8 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 01:20:11 +0100 Subject: [PATCH 5/7] Better code --- src/commands/eval.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/commands/eval.ts b/src/commands/eval.ts index e8b55ce..44efeab 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -44,9 +44,7 @@ export default class Eval extends Command { } } - return display.forEach((m) => { - message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``); - }); + return display.forEach((m) => message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``)); } catch (error) { return this.client.util.handleError(error, message, this); } From 27c4202eb71af9e63db94d6547e1ad54f172d864 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 01:21:25 +0100 Subject: [PATCH 6/7] Added exec command --- src/commands/exec.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/commands/exec.ts 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); + } + } +} From f3347c95e05f41dc34d38c750940814b75cf2f18 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 01:22:33 +0100 Subject: [PATCH 7/7] index --- src/commands/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commands/index.ts b/src/commands/index.ts index 0f1d40a..8f7379d 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -5,3 +5,4 @@ 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';