|
| 1 | +import type { AMASession } from '@chatsift/core'; |
| 2 | +import type { RESTPostAPIChannelMessageJSONBody } from '@discordjs/core'; |
| 3 | +import { ButtonStyle, ComponentType } from '@discordjs/core'; |
| 4 | +import { DiscordAPIError } from '@discordjs/rest'; |
| 5 | +import { badRequest } from '@hapi/boom'; |
| 6 | +import type { Selectable } from 'kysely'; |
| 7 | +import type { NextHandler, Response } from 'polka'; |
| 8 | +import { z } from 'zod'; |
| 9 | +import { context } from '../../context.js'; |
| 10 | +import { isAuthed } from '../../middleware/isAuthed.js'; |
| 11 | +import { discordAPIAma } from '../../util/discordAPI.js'; |
| 12 | +import { snowflakeSchema } from '../../util/schemas.js'; |
| 13 | +import type { TRequest } from '../route.js'; |
| 14 | +import { Route, RouteMethod } from '../route.js'; |
| 15 | + |
| 16 | +const promptSchema = z.union([ |
| 17 | + z.strictObject({ |
| 18 | + prompt: z |
| 19 | + .object({ |
| 20 | + description: z.string().max(4_000).optional(), |
| 21 | + plainText: z.string().max(100).optional(), |
| 22 | + imageURL: z.url().optional(), |
| 23 | + thumbnailURL: z.url().optional(), |
| 24 | + }) |
| 25 | + .strict(), |
| 26 | + }), |
| 27 | + z.strictObject({ |
| 28 | + prompt_raw: z.strictObject({ |
| 29 | + content: z.string().optional(), |
| 30 | + embeds: z.array(z.any()).optional(), |
| 31 | + }), |
| 32 | + }), |
| 33 | +]); |
| 34 | + |
| 35 | +const bodySchema = z.intersection( |
| 36 | + z.strictObject({ |
| 37 | + modQueueId: snowflakeSchema.nullable(), |
| 38 | + flaggedQueueId: snowflakeSchema.nullable(), |
| 39 | + guestQueueId: snowflakeSchema.nullable(), |
| 40 | + title: z.string().min(1).max(255), |
| 41 | + answersChannelId: snowflakeSchema, |
| 42 | + promptChannelId: snowflakeSchema, |
| 43 | + }), |
| 44 | + promptSchema, |
| 45 | +); |
| 46 | + |
| 47 | +export type CreateAMABody = z.infer<typeof bodySchema>; |
| 48 | + |
| 49 | +export type CreateAMAResult = Selectable<AMASession>; |
| 50 | + |
| 51 | +export default class CreateAMA extends Route<CreateAMAResult, CreateAMABody> { |
| 52 | + public readonly info = { |
| 53 | + method: RouteMethod.post, |
| 54 | + path: '/v3/guilds/:guildId/ama/amas', |
| 55 | + } as const; |
| 56 | + |
| 57 | + public override readonly bodyValidationSchema = bodySchema; |
| 58 | + |
| 59 | + public override readonly middleware = [ |
| 60 | + ...isAuthed({ fallthrough: false, isGlobalAdmin: false, isGuildManager: true }), |
| 61 | + ]; |
| 62 | + |
| 63 | + public override async handle(req: TRequest<CreateAMABody>, res: Response, next: NextHandler) { |
| 64 | + const data = req.body as CreateAMABody; |
| 65 | + const { guildId } = req.params as { guildId: string }; |
| 66 | + |
| 67 | + // TODO(DD): Reconsider? |
| 68 | + const messageBodyBase: RESTPostAPIChannelMessageJSONBody = |
| 69 | + 'prompt_raw' in data |
| 70 | + ? data.prompt_raw |
| 71 | + : { |
| 72 | + content: data.prompt.plainText, |
| 73 | + embeds: [ |
| 74 | + { |
| 75 | + color: 0x7289da, // blurple |
| 76 | + title: data.title, |
| 77 | + description: data.prompt.description, |
| 78 | + image: data.prompt.imageURL ? { url: data.prompt.imageURL } : undefined, |
| 79 | + thumbnail: data.prompt.thumbnailURL ? { url: data.prompt.thumbnailURL } : undefined, |
| 80 | + timestamp: new Date().toISOString(), |
| 81 | + }, |
| 82 | + ], |
| 83 | + }; |
| 84 | + |
| 85 | + let promptMessage; |
| 86 | + try { |
| 87 | + promptMessage = await discordAPIAma.channels.createMessage(data.promptChannelId, { |
| 88 | + ...messageBodyBase, |
| 89 | + components: [ |
| 90 | + { |
| 91 | + type: ComponentType.ActionRow, |
| 92 | + components: [ |
| 93 | + { |
| 94 | + type: ComponentType.Button, |
| 95 | + style: ButtonStyle.Primary, |
| 96 | + label: 'Submit a question', |
| 97 | + custom_id: 'submit-question', |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + } catch (error) { |
| 104 | + if (error instanceof DiscordAPIError && error.status === 400 && 'prompt_raw' in data) { |
| 105 | + return next(badRequest('invalid prompt_raw data')); |
| 106 | + } |
| 107 | + |
| 108 | + throw error; |
| 109 | + } |
| 110 | + |
| 111 | + const created: CreateAMAResult = await context.db |
| 112 | + .insertInto('AMASession') |
| 113 | + .values({ |
| 114 | + guildId, |
| 115 | + title: data.title, |
| 116 | + answersChannelId: data.answersChannelId, |
| 117 | + promptChannelId: data.promptChannelId, |
| 118 | + promptMessageId: promptMessage.id, |
| 119 | + modQueueId: data.modQueueId, |
| 120 | + flaggedQueueId: data.flaggedQueueId, |
| 121 | + guestQueueId: data.guestQueueId, |
| 122 | + ended: false, |
| 123 | + createdAt: new Date(), |
| 124 | + }) |
| 125 | + .returningAll() |
| 126 | + .executeTakeFirstOrThrow(); |
| 127 | + |
| 128 | + res.statusCode = 200; |
| 129 | + res.setHeader('Content-Type', 'application/json'); |
| 130 | + return res.end(JSON.stringify(created)); |
| 131 | + } |
| 132 | +} |
0 commit comments