aboutsummaryrefslogtreecommitdiff
path: root/src/commands/owner/eval.ts
blob: 44326b289585873ca3e9ce3613af0db11ba3a6e0 (plain)
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
/* eslint-disable @typescript-eslint/no-unused-vars */
import { BotCommand } from '../../lib/extensions/BotCommand';
import { MessageEmbed, Message } from 'discord.js';
import { inspect, promisify } from 'util';
import { exec } from 'child_process';

const clean = (text) => {
	if (typeof text === 'string')
		return text
			.replace(/`/g, '`' + String.fromCharCode(8203))
			.replace(/@/g, '@' + String.fromCharCode(8203));
	else return text;
};

export default class EvalCommand extends BotCommand {
	public constructor() {
		super('eval', {
			aliases: ['eval', 'ev'],
			category: 'dev',
			description: {
				content: 'Use the command to eval stuff in the bot.',
				usage: 'eval [--depth #] <code> [--sudo] [--silent] [--delete]',
				examples: ['eval message.guild.name', 'eval this.client.ownerID']
			},
			args: [
				{
					id: 'depth',
					match: 'option',
					type: 'number',
					flag: '--depth',
					default: 0
				},
				{
					id: 'silent',
					match: 'flag',
					flag: '--silent'
				},
				{
					id: 'code',
					match: 'rest',
					type: 'string',
					prompt: {
						start: 'What would you like to eval?',
						retry: 'Invalid code to eval. What would you like to eval?'
					}
				}
			],
			ownerOnly: true,
			clientPermissions: ['EMBED_LINKS']
		});
	}

	public async exec(
		message: Message,
		{ depth, code, silent }: { depth: number; code: string; silent: boolean }
	): Promise<void> {
		const embed: MessageEmbed = new MessageEmbed();

		try {
			let output;
			const me = message.member,
				member = message.member,
				bot = this.client,
				guild = message.guild,
				channel = message.channel,
				config = this.client.config,
				sh = promisify(exec),
				models = this.client.db.models,
				got = await import('got');
			output = eval(code);
			output = await output;
			if (typeof output !== 'string') output = inspect(output, { depth });
			output = output.replace(
				new RegExp(this.client.token, 'g'),
				'[token omitted]'
			);
			output = clean(output);
			embed
				.setTitle('✅ Evaled code successfully')
				.addField(
					'📥 Input',
					code.length > 1012
						? 'Too large to display. Hastebin: ' +
								(await this.client.util.haste(code))
						: '```js\n' + code + '```'
				)
				.addField(
					'📤 Output',
					output.length > 1012
						? 'Too large to display. Hastebin: ' +
								(await this.client.util.haste(output))
						: '```js\n' + output + '```'
				)
				.setColor('#66FF00')
				.setFooter(
					message.author.username,
					message.author.displayAvatarURL({ dynamic: true })
				)
				.setTimestamp();
		} catch (e) {
			embed
				.setTitle('❌ Code was not able to be evaled')
				.addField(
					'📥 Input',
					code.length > 1012
						? 'Too large to display. Hastebin: ' +
								(await this.client.util.haste(code))
						: '```js\n' + code + '```'
				)
				.addField(
					'📤 Output',
					e.length > 1012
						? 'Too large to display. Hastebin: ' +
								(await this.client.util.haste(e))
						: '```js\n' +
								e +
								'```Full stack:' +
								(await this.client.util.haste(e.stack))
				)
				.setColor('#FF0000')
				.setFooter(
					message.author.username,
					message.author.displayAvatarURL({ dynamic: true })
				)
				.setTimestamp();
		}
		if (!silent) {
			await message.util.send(embed);
		} else {
			try {
				await message.author.send(embed);
				await message.react('<a:Check_Mark:790373952760971294>');
			} catch (e) {
				await message.react('❌');
			}
		}
	}
}