summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-11-05 23:43:52 +0100
committerRory& <root@rory.gay>2026-11-05 23:43:52 +0100
commit4c8331fd7a2aa6544e700764457cb39acd585270 (patch)
tree83122517e13a1c1f20d36e68280ad6613a778e34 /src/api
parentKittyLogo: add missing copyright header (diff)
downloadserver-ts-4c8331fd7a2aa6544e700764457cb39acd585270.tar.xz
More in depth error logging, fix schemas for application commands
Diffstat (limited to 'src/api')
-rw-r--r--src/api/middlewares/Route.ts8
-rw-r--r--src/api/routes/applications/#application_id/commands/#command_id/index.ts91
-rw-r--r--src/api/routes/applications/#application_id/commands/index.ts79
-rw-r--r--src/api/routes/applications/#application_id/guilds/#guild_id/commands/#command_id/index.ts145
-rw-r--r--src/api/routes/applications/#application_id/guilds/#guild_id/commands/index.ts93
-rw-r--r--src/api/routes/guilds/#guild_id/application-command-index.ts126
6 files changed, 345 insertions, 197 deletions
diff --git a/src/api/middlewares/Route.ts b/src/api/middlewares/Route.ts

index 5341f100b..a93d512b5 100644 --- a/src/api/middlewares/Route.ts +++ b/src/api/middlewares/Route.ts
@@ -19,7 +19,7 @@ import { DiscordApiErrors, EVENT, FieldErrors, PermissionResolvable, Permissions, RightResolvable, Rights, SpacebarApiErrors, getPermission, getRights } from "@spacebar/util"; import { AnyValidateFunction } from "ajv/dist/core"; import { NextFunction, Request, Response } from "express"; -import { ajv } from "@spacebar/schemas"; +import { ajv, getInvalidSchemaValues } from "@spacebar/schemas"; import { BigNumber } from "bignumber.js"; const ignoredRequestSchemas = [ @@ -179,6 +179,12 @@ export function route(opts: RouteOptions) { ); if (process.env.LOG_VALIDATION_ERRORS) { console.log(`[VALIDATION ERROR] ${req.method} ${req.originalUrl} - SCHEMA='${opts.requestBody}' -`, validate?.errors); + if (process.env.LOG_VALIDATION_ERRORS_RESOLVED) { + const resolved = getInvalidSchemaValues(req.body, validate.errors!); + for (const res of resolved) { + console.log(`[VALIDATION ERROR]`, res.keyword, res.instancePath, res.value); + } + } if (process.env.LOG_VALIDATION_ERROR_SCHEMAS) console.log(validate.schema); } throw FieldErrors(fields, validate.errors!); diff --git a/src/api/routes/applications/#application_id/commands/#command_id/index.ts b/src/api/routes/applications/#application_id/commands/#command_id/index.ts
index 3e4df62ff..5df35f8cc 100644 --- a/src/api/routes/applications/#application_id/commands/#command_id/index.ts +++ b/src/api/routes/applications/#application_id/commands/#command_id/index.ts
@@ -17,6 +17,7 @@ */ import { Request, Response, Router } from "express"; +import { FindOptionsWhere, IsNull } from "typeorm"; import { route } from "@spacebar/api/middlewares"; import { Application, ApplicationCommand } from "@spacebar/database"; import { Config, FieldErrors, Snowflake } from "@spacebar/util"; @@ -24,28 +25,43 @@ import { ApplicationCommandCreateSchema, ApplicationCommandSchema } from "@space const router = Router({ mergeParams: true }); -router.get("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.get( + "/", + route({ + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const command = await ApplicationCommand.findOne({ where: { application_id: req.params.application_id as string, id: req.params.command_id as string } }); + const command = await ApplicationCommand.findOne({ where: { application_id: req.params.application_id as string, id: req.params.command_id as string } }); - if (!command) { - res.status(404).send({ code: 404, message: "Unknown application command" }); - return; - } + if (!command) { + res.status(404).send({ code: 404, message: "Unknown application command" }); + return; + } - res.send(command); -}); + res.send(command.toApplicationCommandResponse(req.user.settings?.locale)); + }, +); router.patch( "/", route({ requestBody: "ApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -77,8 +93,7 @@ router.patch( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { name: body.name.trim(), name_localizations: body.name_localizations, description: body.description?.trim() || "", @@ -94,29 +109,43 @@ router.patch( type: body.type, version: Snowflake.generate(), }; + const commandKey: FindOptionsWhere<ApplicationCommand> = { + application_id: req.params.application_id as string, + name: body.name.trim(), + guild_id: IsNull(), + }; - await ApplicationCommand.update({ name: body.name.trim() }, commandForDb); - res.send(commandForDb); + await ApplicationCommand.update(commandKey, commandForDb); + const resultingCommand = await ApplicationCommand.findOneOrFail({ where: commandKey }); + res.send(resultingCommand.toApplicationCommandResponse()); }, ); -router.delete("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.delete( + "/", + route({ + responses: { + 204: {}, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const commandExists = await ApplicationCommand.exists({ where: { application_id: req.params.application_id as string, id: req.params.command_id as string } }); + const commandExists = await ApplicationCommand.exists({ where: { application_id: req.params.application_id as string, id: req.params.command_id as string } }); - if (!commandExists) { - res.status(404).send({ code: 404, message: "Unknown application command" }); - return; - } + if (!commandExists) { + res.status(404).send({ code: 404, message: "Unknown application command" }); + return; + } - await ApplicationCommand.delete({ application_id: req.params.application_id as string, id: req.params.command_id as string }); - res.sendStatus(204); -}); + await ApplicationCommand.delete({ application_id: req.params.application_id as string, id: req.params.command_id as string, guild_id: IsNull() }); + res.sendStatus(204); + }, +); export default router; diff --git a/src/api/routes/applications/#application_id/commands/index.ts b/src/api/routes/applications/#application_id/commands/index.ts
index 538ea7ea8..bde393f4f 100644 --- a/src/api/routes/applications/#application_id/commands/index.ts +++ b/src/api/routes/applications/#application_id/commands/index.ts
@@ -18,29 +18,44 @@ import { Request, Response, Router } from "express"; import { IsNull } from "typeorm"; -import { ApplicationCommandCreateSchema, ApplicationCommandSchema } from "@spacebar/schemas"; +import { ApplicationCommandCreateSchema, ApplicationCommandListResponse, ApplicationCommandSchema } from "@spacebar/schemas"; import { route } from "@spacebar/api/middlewares"; import { Application, ApplicationCommand } from "@spacebar/database"; import { Config, FieldErrors, Snowflake } from "@spacebar/util"; const router = Router({ mergeParams: true }); -router.get("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.get( + "/", + route({ + responses: { + 200: { + body: "ApplicationCommandListResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const command = await ApplicationCommand.find({ where: { application_id: req.params.application_id as string } }); - res.send(command); -}); + const commands = await ApplicationCommand.find({ where: { application_id: req.params.application_id as string } }); + res.send(commands.map((c) => c.toApplicationCommandResponse(req.user.settings?.locale)) satisfies ApplicationCommandListResponse); + }, +); router.post( "/", route({ requestBody: "ApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -65,8 +80,7 @@ router.post( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { name: body.name.trim(), name_localizations: body.name_localizations, description: body.description?.trim() || "", @@ -86,13 +100,23 @@ router.post( const commandExists = await ApplicationCommand.exists({ where: { application_id: req.params.application_id as string, name: body.name.trim() } }); if (commandExists) { - await ApplicationCommand.update({ application_id: req.params.application_id as string, name: body.name.trim() }, commandForDb); + await ApplicationCommand.update({ application_id: req.params.application_id as string, name: body.name.trim(), guild_id: IsNull() }, commandForDb); } else { commandForDb.id = Snowflake.generate(); // Have to be done that way so the id doesn't change - await ApplicationCommand.save(commandForDb); + await ApplicationCommand.save({ + ...commandForDb, + application_id: req.params.application_id as string, + }); } - res.send(body); + const resultingCommand = await ApplicationCommand.findOneOrFail({ + where: { + application_id: req.params.application_id as string, + name: body.name.trim(), + version: commandForDb.version, + }, + }); + res.send(resultingCommand.toApplicationCommandResponse()); }, ); @@ -100,6 +124,11 @@ router.put( "/", route({ requestBody: "BulkApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandListResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -121,6 +150,8 @@ router.put( await ApplicationCommand.delete({ application_id: req.params.application_id as string, guild_id: IsNull(), id: command.id }); } + const resultingCommands: ApplicationCommand[] = []; + for (const command of body) { if (!command.type) { command.type = 1; @@ -135,8 +166,7 @@ router.put( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { name: command.name.trim(), name_localizations: command.name_localizations, description: command.description?.trim() || "", @@ -154,16 +184,25 @@ router.put( }; const commandExists = await ApplicationCommand.exists({ where: { application_id: req.params.application_id as string, name: command.name.trim() } }); + const commandKey = { application_id: req.params.application_id as string, name: command.name.trim(), guild_id: IsNull() }; if (commandExists) { - await ApplicationCommand.update({ application_id: req.params.application_id as string, name: command.name.trim() }, commandForDb); + await ApplicationCommand.update(commandKey, commandForDb); } else { commandForDb.id = Snowflake.generate(); // Have to be done that way so the id doesn't change - await ApplicationCommand.save(commandForDb); + await ApplicationCommand.save({ + ...commandForDb, + application_id: req.params.application_id as string, + }); } + + const resultingCommand = await ApplicationCommand.findOneOrFail({ + where: commandKey, + }); + resultingCommands.push(resultingCommand); } - res.send(body); + res.send(resultingCommands.map((c) => c.toApplicationCommandResponse()) satisfies ApplicationCommandListResponse); }, ); diff --git a/src/api/routes/applications/#application_id/guilds/#guild_id/commands/#command_id/index.ts b/src/api/routes/applications/#application_id/guilds/#guild_id/commands/#command_id/index.ts
index eea17006f..e5960b754 100644 --- a/src/api/routes/applications/#application_id/guilds/#guild_id/commands/#command_id/index.ts +++ b/src/api/routes/applications/#application_id/guilds/#guild_id/commands/#command_id/index.ts
@@ -17,6 +17,7 @@ */ import { Request, Response, Router } from "express"; +import { FindOptionsWhere } from "typeorm"; import { route } from "@spacebar/api/middlewares"; import { Application, ApplicationCommand, Guild, Member } from "@spacebar/database"; import { Config, FieldErrors, Snowflake } from "@spacebar/util"; @@ -24,42 +25,57 @@ import { ApplicationCommandCreateSchema, ApplicationCommandSchema } from "@space const router = Router({ mergeParams: true }); -router.get("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.get( + "/", + route({ + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); + const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); - if (!guildExists) { - res.status(404).send({ code: 404, message: "Unknown Server" }); - return; - } + if (!guildExists) { + res.status(404).send({ code: 404, message: "Unknown Server" }); + return; + } - if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { - res.status(401).send({ code: 401, message: "Missing Access" }); - return; - } + if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { + res.status(401).send({ code: 401, message: "Missing Access" }); + return; + } - const command = await ApplicationCommand.findOne({ - where: { application_id: req.params.application_id as string, id: req.params.command_id as string, guild_id: req.params.guild_id as string }, - }); + const command = await ApplicationCommand.findOne({ + where: { application_id: req.params.application_id as string, id: req.params.command_id as string, guild_id: req.params.guild_id as string }, + }); - if (!command) { - res.status(404).send({ code: 404, message: "Unknown application command" }); - return; - } + if (!command) { + res.status(404).send({ code: 404, message: "Unknown application command" }); + return; + } - res.send(command); -}); + res.send(command.toApplicationCommandResponse(req.user.settings?.locale)); + }, +); router.patch( "/", route({ requestBody: "ApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -96,8 +112,7 @@ router.patch( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { name: body.name.trim(), name_localizations: body.name_localizations, description: body.description?.trim() || "", @@ -114,54 +129,66 @@ router.patch( version: Snowflake.generate(), }; - const commandExists = await ApplicationCommand.exists({ - where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string, name: body.name.trim() }, - }); + const commandKey: FindOptionsWhere<ApplicationCommand> = { + application_id: req.params.application_id as string, + guild_id: req.params.guild_id as string, + id: req.params.command_id as string, + name: body.name.trim(), + }; + + const commandExists = await ApplicationCommand.exists({ where: commandKey }); if (!commandExists) { res.status(404).send({ code: 404, message: "Unknown application command" }); return; } - await ApplicationCommand.update( - { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string, name: body.name.trim() }, - commandForDb, - ); - res.send(commandForDb); + await ApplicationCommand.update(commandKey, commandForDb); + const resultingCommand = await ApplicationCommand.findOneOrFail({ where: commandKey }); + + res.send(resultingCommand.toApplicationCommandResponse()); }, ); -router.delete("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.delete( + "/", + route({ + responses: { + 204: {}, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); + const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); - if (!guildExists) { - res.status(404).send({ code: 404, message: "Unknown Server" }); - return; - } + if (!guildExists) { + res.status(404).send({ code: 404, message: "Unknown Server" }); + return; + } - if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { - res.status(401).send({ code: 401, message: "Missing Access" }); - return; - } + if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { + res.status(401).send({ code: 401, message: "Missing Access" }); + return; + } - const commandExists = await ApplicationCommand.exists({ - where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string }, - }); + const commandExists = await ApplicationCommand.exists({ + where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string }, + }); - if (!commandExists) { - res.status(404).send({ code: 404, message: "Unknown application command" }); - return; - } + if (!commandExists) { + res.status(404).send({ code: 404, message: "Unknown application command" }); + return; + } - await ApplicationCommand.delete({ application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string }); - res.sendStatus(204); -}); + await ApplicationCommand.delete({ application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: req.params.command_id as string }); + res.sendStatus(204); + }, +); export default router; diff --git a/src/api/routes/applications/#application_id/guilds/#guild_id/commands/index.ts b/src/api/routes/applications/#application_id/guilds/#guild_id/commands/index.ts
index 520ca9995..a9dc72c6e 100644 --- a/src/api/routes/applications/#application_id/guilds/#guild_id/commands/index.ts +++ b/src/api/routes/applications/#application_id/guilds/#guild_id/commands/index.ts
@@ -17,41 +17,57 @@ */ import { Request, Response, Router } from "express"; +import { FindOptionsWhere } from "typeorm"; import { route } from "@spacebar/api/middlewares"; import { Application, ApplicationCommand, Guild, Member } from "@spacebar/database"; import { Config, FieldErrors, Snowflake } from "@spacebar/util"; -import { ApplicationCommandCreateSchema, ApplicationCommandSchema } from "@spacebar/schemas"; +import { ApplicationCommandCreateSchema, ApplicationCommandSchema, ApplicationCommandListResponse } from "@spacebar/schemas"; const router = Router({ mergeParams: true }); -router.get("/", route({}), async (req: Request, res: Response) => { - const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); +router.get( + "/", + route({ + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); - if (!applicationExists) { - res.status(404).send({ code: 404, message: "Unknown application" }); - return; - } + if (!applicationExists) { + res.status(404).send({ code: 404, message: "Unknown application" }); + return; + } - const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); + const guildExists = await Guild.exists({ where: { id: req.params.guild_id as string } }); - if (!guildExists) { - res.status(404).send({ code: 404, message: "Unknown Server" }); - return; - } + if (!guildExists) { + res.status(404).send({ code: 404, message: "Unknown Server" }); + return; + } - if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { - res.status(401).send({ code: 401, message: "Missing Access" }); - return; - } + if (!(await Member.exists({ where: { id: req.params.application_id as string, guild_id: req.params.guild_id as string } }))) { + res.status(401).send({ code: 401, message: "Missing Access" }); + return; + } - const command = await ApplicationCommand.find({ where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string } }); - res.send(command); -}); + const command = await ApplicationCommand.find({ where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string } }); + res.send(command); + }, +); router.post( "/", route({ requestBody: "ApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -88,8 +104,7 @@ router.post( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { guild_id: req.params.guild_id as string, name: body.name.trim(), name_localizations: body.name_localizations, @@ -115,7 +130,10 @@ router.post( await ApplicationCommand.update({ application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, name: body.name.trim() }, commandForDb); } else { commandForDb.id = Snowflake.generate(); // Have to be done that way so the id doesn't change - await ApplicationCommand.save(commandForDb); + await ApplicationCommand.save({ + ...commandForDb, + application_id: req.params.application_id as string, + }); } res.send(body); @@ -126,6 +144,11 @@ router.put( "/", route({ requestBody: "BulkApplicationCommandCreateSchema", + responses: { + 200: { + body: "ApplicationCommandListResponse", + }, + }, }), async (req: Request, res: Response) => { const applicationExists = await Application.exists({ where: { id: req.params.application_id as string } }); @@ -158,6 +181,7 @@ router.put( for (const command of commandsNotInArray) { await ApplicationCommand.delete({ application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, id: command.id }); } + const resultingCommands: ApplicationCommand[] = []; for (const command of body) { if (!command.type) { @@ -173,8 +197,7 @@ router.put( }); } - const commandForDb: ApplicationCommandSchema = { - application_id: req.params.application_id as string, + const commandForDb: Partial<ApplicationCommand> = { guild_id: req.params.guild_id as string, name: command.name.trim(), name_localizations: command.name_localizations, @@ -192,19 +215,29 @@ router.put( version: Snowflake.generate(), }; - const commandExists = await ApplicationCommand.exists({ - where: { application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, name: command.name }, - }); + const commandKey: FindOptionsWhere<ApplicationCommand> = { + application_id: req.params.application_id as string, + guild_id: req.params.guild_id as string, + name: command.name, + }; + + const commandExists = await ApplicationCommand.exists({ where: commandKey }); if (commandExists) { - await ApplicationCommand.update({ application_id: req.params.application_id as string, guild_id: req.params.guild_id as string, name: command.name }, commandForDb); + await ApplicationCommand.update(commandKey, commandForDb); } else { commandForDb.id = Snowflake.generate(); // Have to be done that way so the id doesn't change - await ApplicationCommand.save(commandForDb); + await ApplicationCommand.save({ + ...commandForDb, + application_id: req.params.application_id as string, + }); } + + const resultingCommand = await ApplicationCommand.findOneOrFail({ where: commandKey }); + resultingCommands.push(resultingCommand); } - res.send(body); + res.send(resultingCommands.map((c) => c.toApplicationCommandResponse()) satisfies ApplicationCommandListResponse); }, ); diff --git a/src/api/routes/guilds/#guild_id/application-command-index.ts b/src/api/routes/guilds/#guild_id/application-command-index.ts
index 10f404e47..7699f93bc 100644 --- a/src/api/routes/guilds/#guild_id/application-command-index.ts +++ b/src/api/routes/guilds/#guild_id/application-command-index.ts
@@ -21,72 +21,86 @@ import { IsNull } from "typeorm"; import { route } from "@spacebar/api/middlewares"; import { Application, ApplicationCommand, Member } from "@spacebar/database"; import { Snowflake } from "@spacebar/util"; -import { ApplicationCommandSchema, ApplicationCommandType } from "@spacebar/schemas"; +import { ApplicationCommandSchema, ApplicationCommandType, ApplicationCommandResponse, ApplicationCommandIndexResponse } from "@spacebar/schemas"; const router = Router({ mergeParams: true }); -router.get("/", route({}), async (req: Request, res: Response) => { - const members = await Member.find({ where: { guild_id: req.params.guild_id as string, user: { bot: true } } }); - const applications: Application[] = []; +router.get( + "/", + route({ + responses: { + 200: { + body: "ApplicationCommandIndexResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const members = await Member.find({ where: { guild_id: req.params.guild_id as string, user: { bot: true } } }); + const applications: Application[] = []; - for (const member of members) { - const app = await Application.findOne({ where: { id: member.id } }); - if (app) applications.push(app); - } + for (const member of members) { + const app = await Application.findOne({ where: { id: member.id } }); + if (app) applications.push(app); + } - const applicationsSendable = []; + const applicationsSendable = []; - for (const application of applications) { - applicationsSendable.push({ - bot_id: application.bot?.id, - description: application.description, - flags: application.flags, - icon: application.icon, - id: application.id, - name: application.name, - }); - } + for (const application of applications) { + // applicationsSendable.push({ + // bot_id: application.bot?.id, + // description: application.description, + // flags: application.flags, + // icon: application.icon, + // id: application.id, + // name: application.name, + // }); + applicationsSendable.push(application.toCommandIndexApplication()); + } - const applicationCommands: ApplicationCommand[][] = []; + const applicationCommands: ApplicationCommand[][] = []; - for (const application of applications) { - applicationCommands.push(await ApplicationCommand.find({ where: { application_id: application.id, guild_id: IsNull() } })); - applicationCommands.push(await ApplicationCommand.find({ where: { application_id: application.id, guild_id: req.params.guild_id as string } })); - } + for (const application of applications) { + applicationCommands.push(await ApplicationCommand.find({ where: { application_id: application.id, guild_id: IsNull() } })); + applicationCommands.push(await ApplicationCommand.find({ where: { application_id: application.id, guild_id: req.params.guild_id as string } })); + } - const applicationCommandsSendable: ApplicationCommandSchema[] = []; + const applicationCommandsSendable: ApplicationCommandResponse[] = []; - for (const command of applicationCommands.flat()) { - applicationCommandsSendable.push({ - id: command.id, - type: command.type, - application_id: command.application_id, - guild_id: command.guild_id, - name: command.name, - name_localizations: command.name_localizations, - name_localized: (command.name_localizations && req.user.settings?.locale ? command.name_localizations[req.user.settings.locale] : null) ?? command.name, - description: command.description, - description_localizations: command.description_localizations, - description_localized: - (command.description_localizations && req.user.settings?.locale ? command.description_localizations[req.user.settings.locale] : null) ?? command.description, - options: command.type === ApplicationCommandType.CHAT_INPUT ? command.options : undefined, - default_member_permissions: command.default_member_permissions, - dm_permission: command.dm_permission, - permissions: command.permissions, - nsfw: command.nsfw, - integration_types: command.integration_types, - global_popularity_rank: command.global_popularity_rank, - contexts: command.contexts, - version: command.version, - handler: command.handler, - }); - } + for (const command of applicationCommands.flat()) { + // TODO: fall back to this if we fucked up ig? + // applicationCommandsSendable.push({ + // id: command.id, + // type: command.type, + // application_id: command.application_id, + // guild_id: command.guild_id, + // name: command.name, + // name_localizations: command.name_localizations, + // name_localized: (command.name_localizations && req.user.settings?.locale ? command.name_localizations[req.user.settings.locale] : null) ?? command.name, + // description: command.description, + // description_localizations: command.description_localizations, + // description_localized: + // (command.description_localizations && req.user.settings?.locale ? command.description_localizations[req.user.settings.locale] : null) ?? command.description, + // options: command.type === ApplicationCommandType.CHAT_INPUT ? command.options : undefined, + // default_member_permissions: command.default_member_permissions, + // dm_permission: command.dm_permission, + // permissions: command.permissions, + // nsfw: command.nsfw, + // integration_types: command.integration_types, + // global_popularity_rank: command.global_popularity_rank, + // contexts: command.contexts, + // version: command.version, + // handler: command.handler, + // }); - res.send({ - applications: applicationsSendable, - application_commands: applicationCommandsSendable, - version: Snowflake.generate(), - }); -}); + applicationCommandsSendable.push(command.toApplicationCommandResponse(req.user.settings?.locale)); + } + + res.send({ + applications: applicationsSendable, + application_commands: applicationCommandsSendable, + version: Snowflake.generate(), + } satisfies ApplicationCommandIndexResponse); + }, +); export default router;