-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.ts
301 lines (277 loc) · 11.7 KB
/
run.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { Subcommand } from '@sapphire/plugin-subcommands'
import { ApplyOptions } from '@sapphire/decorators'
import { MessageActionRow, Modal, type ModalActionRowComponent, TextInputComponent } from 'discord.js'
import { clamp, serialize } from '../../lib/utils/general'
import { RunOptionsPJS, runPJS } from '../../lib/responses/runPJS'
import type { RunOptionsWebpage } from '../../lib/responses/runWebpage'
import { runWebpage } from '../../lib/responses/runWebpage'
import type { RunOptionsSQL } from '../../lib/responses/runSQL'
import { runSQL } from '../../lib/responses/runSQL'
import { deferReply, extractFileType } from '../../lib/utils/discord'
import { parseProgram } from '../../lib/utils/khan'
import { programs } from 'ka-api'
import config from '../../config'
import { ErrorMessages, RunEnvironmentOptionKeys, RunEnvironments, RunEnvironmentTitles } from '../../lib/constants'
@ApplyOptions<Subcommand.Options>({
description: 'Run code on Khan Academy',
preconditions: ['UserRateLimit'],
subcommands: [
{
name: 'pjs',
chatInputRun: 'chatInputPJS',
},
{
name: 'html',
chatInputRun: 'chatInputWebpage',
},
{
name: 'sql',
chatInputRun: 'chatInputSQL',
},
],
})
export class UserCommand extends Subcommand {
readonly #OPTION_DESCRIPTION_PROGRAM = 'Give me a program ID or URL to run it instead'
readonly #OPTION_DESCRIPTION_FILE = 'Give me a file to run it instead'
readonly #UNSUPPORTED_FILE_TYPE = "I can't work with that type of file"
readonly #FILE_FETCH_ERROR = "I couldn't get that file from Discord"
readonly #INVALID_PROGRAM_TYPE = 'That looks like the wrong type of program'
public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder //
.setName(this.name)
.setDescription(this.description)
.addSubcommand((subcommand) =>
subcommand //
.setName('pjs')
.setDescription('Runs Processing.js code on Khan Academy')
.addIntegerOption((option) =>
option //
.setName('width')
.setDescription("What's the canvas width?")
)
.addIntegerOption((option) =>
option //
.setName('height')
.setDescription('Whats the canvas height?')
)
.addBooleanOption((option) =>
option //
.setName('loop-protector')
.setDescription('Should the loop detector be enabled?')
)
.addBooleanOption((option) =>
option //
.setName('screenshot')
.setDescription('Should I take a screenshot of the canvas?')
)
.addNumberOption((option) =>
option //
.setName('delay')
.setDescription('How long should I wait before taking the screenshot?')
)
.addBooleanOption((option) =>
option //
.setName('animated')
.setDescription('Should I record a short GIF instead?')
)
.addStringOption((option) =>
option //
.setName('program')
.setDescription(this.#OPTION_DESCRIPTION_PROGRAM)
)
.addAttachmentOption((option) =>
option //
.setName('file')
.setDescription(this.#OPTION_DESCRIPTION_FILE)
)
)
.addSubcommand((subcommand) =>
subcommand //
.setName('html')
.setDescription('Runs HTML on Khan Academy')
.addIntegerOption((option) =>
option //
.setName('width')
.setDescription("What's the webpage width?")
)
.addIntegerOption((option) =>
option //
.setName('height')
.setDescription("What's the webpage height?")
)
.addBooleanOption((option) =>
option //
.setName('boilerplate')
.setDescription('Should I wrap the HTML in some boilerplate?')
)
.addStringOption((option) =>
option //
.setName('program')
.setDescription(this.#OPTION_DESCRIPTION_PROGRAM)
)
.addAttachmentOption((option) =>
option //
.setName('file')
.setDescription(this.#OPTION_DESCRIPTION_FILE)
)
)
.addSubcommand((subcommand) =>
subcommand //
.setName('sql')
.setDescription('Runs SQL on Khan Academy')
.addIntegerOption((option) =>
option //
.setName('width')
.setDescription("What's the output width?")
)
.addIntegerOption((option) =>
option //
.setName('height')
.setDescription("What's the output height?")
)
.addStringOption((option) =>
option //
.setName('program')
.setDescription(this.#OPTION_DESCRIPTION_PROGRAM)
)
.addAttachmentOption((option) =>
option //
.setName('file')
.setDescription(this.#OPTION_DESCRIPTION_FILE)
)
),
{ idHints: ['1013180516712857651', '1020204329111658577'] }
)
}
public async chatInputPJS(interaction: Subcommand.ChatInputInteraction) {
const options = {
width: interaction.options.getInteger('width'),
height: interaction.options.getInteger('height'),
delay: interaction.options.getNumber('delay'),
loopProtector: interaction.options.getBoolean('loop-protector'),
canvas: interaction.options.getBoolean('canvas'),
animated: interaction.options.getBoolean('animated'),
}
const program = interaction.options.getString('program')
if (!program || options.width !== null) options.width = this.normalizeWidth(options.width)
if (!program || options.height !== null) options.height = this.normalizeHeight(options.height)
options.delay = clamp(options.delay !== null ? options.delay : config.run.delay.min, config.run.delay.min, config.run.delay.max)
options.loopProtector = options.loopProtector ?? true
options.canvas = options.canvas ?? true
options.animated = options.animated ?? false
if (options.animated) {
if (options.width !== null && options.width > config.run.animation.maxWidth) options.width = config.run.width.default
if (options.height !== null && options.height > config.run.animation.maxHeight) options.height = config.run.height.default
}
await this.chatInput(interaction, RunEnvironments.PJS, options as RunOptionsPJS)
}
public async chatInputWebpage(interaction: Subcommand.ChatInputInteraction) {
const options = {
width: interaction.options.getInteger('width'),
height: interaction.options.getInteger('height'),
boilerplate: interaction.options.getBoolean('boilerplate'),
}
const file = interaction.options.getAttachment('file'),
program = interaction.options.getString('program')
if (!program || options.width !== null) options.width = this.normalizeWidth(options.width)
if (!program || options.height !== null) options.height = this.normalizeHeight(options.height)
options.boilerplate = file !== null || program !== null ? false : options.boilerplate ?? true
await this.chatInput(interaction, RunEnvironments.Webpage, options as RunOptionsWebpage)
}
public async chatInputSQL(interaction: Subcommand.ChatInputInteraction) {
const options = {
width: interaction.options.getInteger('width'),
height: interaction.options.getInteger('height'),
}
const program = interaction.options.getString('program')
if (!program || options.width !== null) options.width = this.normalizeWidth(options.width)
if (!program || options.height !== null) options.height = this.normalizeHeight(options.height)
await this.chatInput(interaction, RunEnvironments.SQL, options as RunOptionsSQL)
}
private normalizeWidth(width: number | null) {
if (width === null) return config.run.width.default
return clamp(width, config.run.width.min, config.run.width.max)
}
private normalizeHeight(height: number | null) {
if (height === null) return config.run.height.default
return clamp(height, config.run.height.min, config.run.height.max)
}
private async chatInput(
interaction: Subcommand.ChatInputInteraction,
environment: RunEnvironments,
options: RunOptionsPJS | RunOptionsWebpage | RunOptionsSQL
) {
const file = interaction.options.getAttachment('file'),
program = interaction.options.getString('program')
if (file || program) {
await deferReply(interaction)
let code
if (file) {
if (
!file.contentType ||
!RunEnvironmentFileTypes[environment].includes(extractFileType(file.contentType) as string) ||
typeof file.attachment !== 'string'
) {
await interaction.reply(this.#UNSUPPORTED_FILE_TYPE)
return
}
const response = await fetch(file.attachment)
if (!response.ok) {
await interaction.reply(this.#FILE_FETCH_ERROR)
return
}
code = await response.text()
} else {
const id = parseProgram(program!)
if (!id) {
await interaction.reply(ErrorMessages.InvalidProgram)
return
}
const data = await programs.getProgramJSON(id, { width: 1, height: 1, userAuthoredContentType: 1, revision: { code: 1 } }).catch((reason) => {
if (reason.response?.status === 404) return null
else throw reason
})
if (!data) {
await interaction.reply(ErrorMessages.ProgramNotFound)
return
}
if (data.userAuthoredContentType !== environment) {
await interaction.reply(this.#INVALID_PROGRAM_TYPE)
return
}
if (options.width === null) options.width = data.width
if (options.height === null) options.height = data.height
code = data.revision.code
}
await RunEnvironmentFunctions[environment](interaction, code, options)
} else {
const modal = this.createModal(environment, options)
await interaction.showModal(modal)
}
}
private createModal(environment: RunEnvironments, options: RunOptionsPJS | RunOptionsWebpage | RunOptionsSQL) {
return new Modal()
.setCustomId(`${environment}${serialize(options as unknown as Record<string, boolean | number>, RunEnvironmentOptionKeys[environment])}`)
.setTitle(`${RunEnvironmentTitles[environment]} Input`)
.setComponents(
new MessageActionRow<ModalActionRowComponent>().addComponents(
new TextInputComponent() //
.setCustomId('input')
.setLabel(environment == RunEnvironments.Webpage ? 'HTML' : 'Code')
.setStyle('PARAGRAPH')
)
)
}
}
const RunEnvironmentFileTypes: Record<RunEnvironments, string[]> = {
[RunEnvironments.PJS]: ['text/plain', 'application/javascript'],
[RunEnvironments.Webpage]: ['text/plain', 'text/html'],
[RunEnvironments.SQL]: ['text/plain', 'application/x-sql'],
}
const RunEnvironmentFunctions: Record<RunEnvironments, typeof runPJS | typeof runWebpage | typeof runSQL> = {
[RunEnvironments.PJS]: runPJS,
[RunEnvironments.Webpage]: runWebpage,
[RunEnvironments.SQL]: runSQL,
}