From 6b48c1e4e75f4da98e56da6857b9a79f711bfb03 Mon Sep 17 00:00:00 2001 From: BenSegal855 Date: Thu, 8 Apr 2021 00:07:41 -0400 Subject: [PATCH] fix: handle results over 2000 characters --- src/commands/System/eval.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/commands/System/eval.ts b/src/commands/System/eval.ts index 0b46da6..10c4ffc 100644 --- a/src/commands/System/eval.ts +++ b/src/commands/System/eval.ts @@ -17,15 +17,24 @@ export default class extends Command { const { result, success, type } = await this.eval(msg, code); - return msg.channel.sendTranslated(success ? 'commands:eval.success' : 'commands:eval.error', [ // TODO: handle message > 2k characters + const overflow = codeBlock('js', result).length > 2000; + + return msg.channel.sendTranslated(success ? 'commands:eval.success' : 'commands:eval.error', [ { - output: codeBlock('js', result), + output: overflow ? '' : codeBlock('js', result), type } - ]); + ], { + files: overflow + ? [{ + name: 'evalResult.js', + attachment: Buffer.from(result || '') + }] + : [] + }); } - private async eval(msg: Message, code: string) { + private async eval(msg: Message, code: string): Promise<{ result: string | undefined; success: boolean; type: Type }> { let result: unknown | undefined = undefined; let success: boolean | undefined = undefined; let type: Type | undefined = undefined; @@ -43,11 +52,11 @@ export default class extends Command { success = false; } - if (typeof result !== 'string') { - result = result instanceof Error ? result.stack : inspect(result, { depth: 3 }); // TODO: use flag for determining depth - } + const resultString = typeof result === 'string' + ? result + : result instanceof Error ? result.stack : inspect(result, { depth: 3 }); - return { result, success, type }; + return { result: resultString, success, type }; } }