-
Notifications
You must be signed in to change notification settings - Fork 9
/
acceptEval.js
177 lines (157 loc) · 5.54 KB
/
acceptEval.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const { ActionRowBuilder, ButtonBuilder } = require('discord.js');
const { colorResolver, getRuntime } = require('../../../util');
const util = require('util');
const {
EMBED_FIELD_VALUE_MAX_LENGTH, ACCEPT_EVAL_CODE_EXECUTION, ZERO_WIDTH_SPACE_CHAR_CODE
} = require('../../../constants');
const logger = require('@mirasaki/logger');
const { ComponentCommand } = require('../../../classes/Commands');
const clean = (text) => {
if (typeof (text) === 'string') {
return text.replace(/`/g, '`'
+ String.fromCharCode(ZERO_WIDTH_SPACE_CHAR_CODE)).replace(/@/g, '@'
+ String.fromCharCode(ZERO_WIDTH_SPACE_CHAR_CODE))
.replace(new RegExp(process.env.DISCORD_BOT_TOKEN), '<token>');
}
else return text;
};
module.exports = new ComponentCommand({
// Additional layer of protection
permLevel: 'Developer',
// Discord API data
// Overwriting the default file name with our owm custom component id
data: { name: ACCEPT_EVAL_CODE_EXECUTION },
run: async (client, interaction) => {
// Destructure from interaction and client
const { member, message } = interaction;
const { emojis, colors } = client.container;
// Editing original command interaction
const originalEmbed = message.embeds[0].data;
await message.edit({
content: `${ emojis.success } ${ member }, this code is now executing...`,
embeds: [
{
// Keep the original embed but change color
...originalEmbed,
color: colorResolver(colors.error)
}
],
// Remove decline button and disable accept button
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(ACCEPT_EVAL_CODE_EXECUTION)
.setDisabled(true)
.setLabel('Executing...')
.setStyle('Success')
)
]
});
// Slicing our code input
const CODEBLOCK_CHAR_OFFSET_START = 6;
const CODEBLOCK_CHAR_OFFSET_END = 4;
const codeInput = originalEmbed.description.slice(CODEBLOCK_CHAR_OFFSET_START, -CODEBLOCK_CHAR_OFFSET_END);
// Defer our reply
await interaction.deferReply();
// Performance measuring
let evaluated;
const startEvalTime = process.hrtime.bigint();
try {
// eslint-disable-next-line no-eval
evaluated = eval(codeInput);
if (evaluated instanceof Promise) evaluated = await evaluated;
// Get execution time
const timeSinceHr = getRuntime(startEvalTime);
const timeSinceStr = `${ timeSinceHr.seconds } seconds (${ timeSinceHr.ms } ms)`;
// String response
const codeOutput = clean(util.inspect(evaluated, { depth: 0 }));
const response = [ `\`\`\`js\n${ codeOutput }\`\`\``, `\`\`\`fix\n${ timeSinceStr }\`\`\`` ];
// Building the embed
const evalEmbed = {
color: colorResolver(),
description: `:inbox_tray: **Input:**\n\`\`\`js\n${ codeInput }\n\`\`\``,
fields: [
{
name: ':outbox_tray: Output:',
value: `${ response[0] }`,
inline: false
},
{
name: 'Time taken',
value: `${ response[1] }`,
inline: false
}
]
};
// Result fits within character limit
if (response[0].length <= EMBED_FIELD_VALUE_MAX_LENGTH) {
await message.edit({
content: `${ emojis.success } ${ member }, this code has been evaluated.`,
embeds: [ evalEmbed ],
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('accept_eval_code')
.setDisabled(true)
.setLabel('Evaluated')
.setStyle('Success')
)
]
});
}
// Output is too many characters
else {
const output = Buffer.from(codeOutput);
await message.edit({
content: `${ emojis.success } ${ member }, this code has been evaluated.`,
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('accept_eval_code')
.setDisabled(true)
.setLabel('Evaluated')
.setStyle('Success')
)
],
files: [
{
attachment: output,
name: 'evalOutput.txt'
}
]
});
}
// Reply to button interaction
interaction.editReply({ content: `${ emojis.success } ${ member }, finished code execution.` });
}
catch (err) {
const timeSinceHr = getRuntime(startEvalTime);
// Log potential errors
logger.syserr('Encountered error while executing /eval code');
console.error(err);
// Update button interaction
interaction.editReply({ content: `${ emojis.error } ${ member }, code execution error, check original embed for output.` });
// Format time stamps
const timeSinceStr = `${ timeSinceHr.seconds } seconds (${ timeSinceHr.ms } ms)`;
// Update original embed interaction
message.edit({ embeds: [
{
color: colorResolver(),
description: `:inbox_tray: **Input:**\n\`\`\`js\n${ codeInput }\n\`\`\``,
fields: [
{
name: ':outbox_tray: Output:',
value: `\`\`\`js\n${ err.stack || err }\n\`\`\``,
inline: false
},
{
name: 'Time taken',
value: `\`\`\`fix\n${ timeSinceStr }\n\`\`\``,
inline: false
}
]
}
] });
}
}
});