Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle eval results over 2000 characters #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/commands/System/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
}

}