summaryrefslogtreecommitdiff
path: root/src/bot/commands/owner/eval.ts
blob: 37dbbdecc8e27413752673ce3ab57eb62752866f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// https://github.com/almostSouji/cor/blob/master/src/bot/commands/owner/eval.ts
import { execSync } from 'child_process';
import { Command } from 'discord-akairo';
import { Message } from 'discord.js';
import util from 'util';
import { postHaste } from '../../util';
import { MESSAGES, SENSITIVE_PATTERN_REPLACEMENT } from '../../util/constants';

export default class EvalCommand extends Command {
	public constructor() {
		super('eval', {
			category: 'owner',
			aliases: ['eval', 'js', 'e'],
			clientPermissions: ['SEND_MESSAGES'],
			description: {
				content: 'Evaluate JavaScript code.',
			},
			ownerOnly: true,
			args: [
				{
					id: 'code',
					match: 'text',
					prompt: {
						start: 'what code would you like to evaluate?',
					},
				},
				{
					id: 'terminal',
					flag: ['--t'],
					match: 'flag',
				},
				{
					id: 'input',
					match: 'flag',
					flag: ['--input', '--in', '--i'],
				},
				{
					id: 'noout',
					match: 'flag',
					flag: ['--noout', '--nout', '--no'],
				},
				{
					id: 'notype',
					match: 'flag',
					flag: ['--notype', '--notp'],
				},
				{
					id: 'notime',
					match: 'flag',
					flag: ['--notime', '--noti'],
				},
				{
					id: 'haste',
					match: 'flag',
					flag: ['--haste', '--h'],
				},
			],
		});
	}

	private _clean(text: any): any {
		if (typeof text === 'string') {
			text = text.replace(/`/g, `\`${String.fromCharCode(8203)}`).replace(/@/g, `@${String.fromCharCode(8203)}`);

			return text.replace(new RegExp(this.client.token!, 'gi'), SENSITIVE_PATTERN_REPLACEMENT);
		}

		return text;
	}

	public async exec(
		msg: Message,
		{
			code,
			terminal,
			input,
			noout,
			notype,
			notime,
			haste,
		}: {
			code: string;
			terminal: boolean;
			input: boolean;
			noout: boolean;
			notype: boolean;
			notime: boolean;
			haste: boolean;
		},
	): Promise<Message | Message[] | void> {
		if (terminal) {
			try {
				const exec = execSync(code).toString();
				return msg.util!.send(exec.substring(0, 1900), { code: 'fix' });
			} catch (err) {
				return msg.util!.send(err.toString(), { code: 'fix' });
			}
		}

		let evaled;
		try {
			const hrStart = process.hrtime();
			evaled = eval(code); // eslint-disable-line no-eval

			// eslint-disable-next-line
			if (evaled != null && typeof evaled.then === 'function') evaled = await evaled;
			const hrStop = process.hrtime(hrStart);

			let response = '';
			if (input) {
				response += MESSAGES.COMMANDS.EVAL.INPUT(code);
			}
			if (!noout) {
				response += MESSAGES.COMMANDS.EVAL.OUTPUT(this._clean(util.inspect(evaled, { depth: 0 })));
			}
			if (!notype && !noout) {
				response += `• Type: \`${typeof evaled}\``;
			}
			if (!noout && !notime) {
				response += ` • time taken: \`${(hrStop[0] * 1e9 + hrStop[1]) / 1e6}ms\``;
			}
			if (haste) {
				const hasteLink = await postHaste(this._clean(util.inspect(evaled)), 'js');
				response += `\n• Full Inspect: ${hasteLink}`;
			}
			if (response.length > 20000) {
				try {
					const hasteLink = await postHaste(this._clean(util.inspect(evaled)));
					return msg.util?.send(MESSAGES.COMMANDS.EVAL.LONG_OUTPUT(hasteLink));
				} catch (hasteerror) {
					return msg.util?.send(MESSAGES.COMMANDS.EVAL.ERRORS.TOO_LONG);
				}
			}
			if (response.length > 0) {
				await msg.util?.send(response);
			}
		} catch (err) {
			this.client.logger.error('[EVAL ERROR]', err.stack);
			return msg.util?.send(MESSAGES.COMMANDS.EVAL.ERRORS.CODE_BLOCK(this._clean(err)));
		}
	}
}