From 4c8331fd7a2aa6544e700764457cb39acd585270 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 5 Nov 2026 23:43:52 +0100 Subject: [PATCH] More in depth error logging, fix schemas for application commands --- assets/openapi.json | Bin 1152195 -> 1164565 bytes assets/schemas.json | Bin 577253 -> 587117 bytes src/api/middlewares/Route.ts | 8 +- .../commands/#command_id/index.ts | 91 ++++++---- .../#application_id/commands/index.ts | 79 ++++++--- .../#guild_id/commands/#command_id/index.ts | 157 ++++++++++-------- .../guilds/#guild_id/commands/index.ts | 93 +++++++---- .../#guild_id/application-command-index.ts | 126 +++++++------- src/database/entities/Application.ts | 15 ++ src/database/entities/ApplicationCommand.ts | 29 +++- src/schemas/Validator.ts | 2 +- .../api/bots/ApplicationCommandSchema.ts | 15 +- src/schemas/api/developers/Application.ts | 35 +++- src/util/util/Localization.ts | 33 ++++ src/util/util/index.ts | 1 + 15 files changed, 472 insertions(+), 212 deletions(-) create mode 100644 src/util/util/Localization.ts diff --git a/assets/openapi.json b/assets/openapi.json index 509181b407297a6dee2ebc28c672ff9dce7853cd..483462cab0d34658e1e1add36bf461a541ef0bf2 100644 GIT binary patch delta 2524 zcmcImZERE58P>Vq*PquwLJ}v&ffy((*aVfNX&YQJ2qE?GHo?#u#UbRQSuio=qwOfd zV?bIoNSZVJcIpJ4h=OQ4aB9IBNm@C|N>c=lTb58*DP~>Q(b2_xXhpP3dag|&Q_;Hp z=#L}cd(Qj5=Y5`Yp67b-@bZ1TOG0=mxF6yJo$?V-289}k_OROUY01oBYx-K8qOF4v z%RPXDMmwbRawd3@MX|fF6^4I2|Ei#uy*ri5)HFILU&V)&|b*fz6+3Q zt3X@jz+0#3FJ@R9K(jU<=yTA=>+m5sa8+F0)o9LAN(s>J2?8#B6{>c z0cD^36!t|+pra27a3$N24()wN2P;9I4)I&O8Md~tGOTKm%5dh03C`@+X~Mrg*N4a% zCn}!>{uh|t;C`81n5gC(;)}2^C6{E@phplWL;Rw!HRur%J5aI~<9HSiM@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s}9{>OV 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 = { 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 = { + 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 = { 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 = { 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 = { name: body.name.trim(), name_localizations: body.name_localizations, description: body.description?.trim() || "", @@ -114,8 +129,56 @@ router.patch( version: Snowflake.generate(), }; + const commandKey: FindOptionsWhere = { + 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(commandKey, commandForDb); + const resultingCommand = await ApplicationCommand.findOneOrFail({ where: commandKey }); + + res.send(resultingCommand.toApplicationCommandResponse()); + }, +); + +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; + } + + 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 (!(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, name: body.name.trim() }, + 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) { @@ -123,45 +186,9 @@ router.patch( 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.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); }, ); -router.delete("/", route({}), 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; - } - - 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 (!(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 }, - }); - - 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); -}); - 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 = { 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 = { 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 = { + 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; diff --git a/src/database/entities/Application.ts b/src/database/entities/Application.ts index 558d6469a..25e8c7558 100644 --- a/src/database/entities/Application.ts +++ b/src/database/entities/Application.ts @@ -17,6 +17,7 @@ */ import { Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, RelationId } from "typeorm"; +import { ApplicationCommandIndexApplication } from "@spacebar/schemas"; import { BaseClass } from "./BaseClass"; import { Team } from "./Team"; import { User } from "./User"; @@ -148,4 +149,18 @@ export class Application extends BaseClass { onDelete: "CASCADE", }) emojis: Emoji[]; + + toCommandIndexApplication(): ApplicationCommandIndexApplication { + return { + id: this.id, + name: this.name, + description: this.description, + icon: this.icon ?? null, + permissions: undefined, // TODO + bot: this.bot?.toPartialUser(), + bot_id: this.bot?.id ?? this.id, // TODO HACK: this is uh, an assumption as bot users are created with the app's id + flags: this.flags.toString(), + embedded_activity_config: undefined, + } satisfies ApplicationCommandIndexApplication; + } } diff --git a/src/database/entities/ApplicationCommand.ts b/src/database/entities/ApplicationCommand.ts index e22e5f417..d906f554a 100644 --- a/src/database/entities/ApplicationCommand.ts +++ b/src/database/entities/ApplicationCommand.ts @@ -18,15 +18,17 @@ import { Column, Entity } from "typeorm"; import { BaseClass } from "./BaseClass"; -import type { +import { ApplicationCommandHandlerType, ApplicationCommandOption, ApplicationCommandIndexPermissions, Snowflake, ApplicationIntegrationType, InteractionContextType, + ApplicationCommandResponse, } from "@spacebar/schemas"; import { ApplicationCommandType } from "@spacebar/schemas/api/bots/ApplicationCommandSchema"; +import { getLocalizedValue } from "@spacebar/util"; @Entity({ name: "application_commands", @@ -85,4 +87,29 @@ export class ApplicationCommand extends BaseClass { @Column({ default: 0 }) handler?: ApplicationCommandHandlerType; + + toApplicationCommandResponse(locale?: string): ApplicationCommandResponse { + return { + id: this.id, + type: this.type, + application_id: this.application_id, + guild_id: this.guild_id, + name: this.name, + name_localizations: this.name_localizations, + name_localized: getLocalizedValue(locale, this.name_localizations, this.name), + description: this.description, + description_localizations: this.description_localizations, + description_localized: getLocalizedValue(locale, this.description_localizations, this.description), + options: this.type === ApplicationCommandType.CHAT_INPUT ? this.options : undefined, + default_member_permissions: this.default_member_permissions, + dm_permission: this.dm_permission, + permissions: this.permissions, + nsfw: this.nsfw, + integration_types: this.integration_types, + global_popularity_rank: this.global_popularity_rank, + contexts: this.contexts, + version: this.version, + handler: this.handler, + } satisfies ApplicationCommandResponse; + } } diff --git a/src/schemas/Validator.ts b/src/schemas/Validator.ts index 73672c183..8dc265104 100644 --- a/src/schemas/Validator.ts +++ b/src/schemas/Validator.ts @@ -79,7 +79,7 @@ export function getInvalidSchemaValues(data: object, errors: typeof ajv.errors): else if (e.keyword == "required") return { instancePath: instancePath + `/${e.params.missingProperty}`, value: "<>", keyword: e.keyword }; else if (e.keyword == "anyOf") return { instancePath: instancePath, value: "<>", keyword: e.keyword }; - console.log("Resolving:", { instancePath, eInstancePath: e.instancePath, eKeyword: e.keyword, eParams: e.params }); + // console.log("Resolving:", { instancePath, eInstancePath: e.instancePath, eKeyword: e.keyword, eParams: e.params }); return { ...getInvalidSchemaValue(data, instancePath), keyword: e.keyword }; }) .filter((x) => x != undefined); diff --git a/src/schemas/api/bots/ApplicationCommandSchema.ts b/src/schemas/api/bots/ApplicationCommandSchema.ts index 4ffc37a25..f1de72a91 100644 --- a/src/schemas/api/bots/ApplicationCommandSchema.ts +++ b/src/schemas/api/bots/ApplicationCommandSchema.ts @@ -21,14 +21,10 @@ import { ApplicationCommandOption, Snowflake, StringStringDictionary } from "@sp export interface ApplicationCommandSchema { id?: Snowflake; type?: ApplicationCommandType; - application_id: Snowflake; - guild_id?: Snowflake; name: string; name_localizations?: StringStringDictionary; - name_localized?: string | null; description: string; description_localizations?: StringStringDictionary; - description_localized?: string | null; options?: ApplicationCommandOption[]; default_member_permissions: string | null; /* @@ -38,12 +34,21 @@ export interface ApplicationCommandSchema { permissions?: ApplicationCommandIndexPermissions; nsfw?: boolean; integration_types?: ApplicationIntegrationType[]; - global_popularity_rank?: number; contexts?: InteractionContextType[]; version: Snowflake; handler?: ApplicationCommandHandlerType; } +export type ApplicationCommandListResponse = ApplicationCommandResponse[]; +export interface ApplicationCommandResponse extends ApplicationCommandSchema { + id: Snowflake; + application_id: Snowflake; + guild_id?: Snowflake; + name_localized?: string | null; + description_localized?: string | null; + global_popularity_rank?: number; +} + export interface ApplicationCommandIndexPermissions { user?: boolean; roles?: Record; diff --git a/src/schemas/api/developers/Application.ts b/src/schemas/api/developers/Application.ts index 62fc818b8..1a4e45a17 100644 --- a/src/schemas/api/developers/Application.ts +++ b/src/schemas/api/developers/Application.ts @@ -16,10 +16,10 @@ along with this program. If not, see . */ -import { ApplicationSku, PartialUser, Snowflake, Team } from "@spacebar/schemas"; +import { ApplicationSku, PartialUser, Snowflake, Team, StringStringDictionary, ChannelType, ApplicationCommandResponse } from "@spacebar/schemas"; export interface ApplicationCommand { - id: string; + id: Snowflake; application_id: string; name: string; description: string; @@ -29,14 +29,23 @@ export interface ApplicationCommand { export interface ApplicationCommandOption { type: ApplicationCommandOptionType; name: string; + name_localizations?: StringStringDictionary; description: string; + description_localizations?: StringStringDictionary; required?: boolean; + autocomplete?: boolean; + min_length?: number; + max_length?: number; + min_value?: number; + max_value?: number; + channel_types?: ChannelType[]; choices?: ApplicationCommandOptionChoice[]; options?: ApplicationCommandOption[]; } export interface ApplicationCommandOptionChoice { name: string; + name_localizations?: StringStringDictionary; value: string | number; } @@ -63,6 +72,24 @@ export interface ApplicationCommandInteractionDataOption { options?: ApplicationCommandInteractionDataOption[]; } +export interface ApplicationCommandIndexApplication { + id: Snowflake; + name: string; + description: string; + icon: string | null; + permissions?: unknown; // TODO + bot?: PartialUser; + bot_id?: Snowflake; + flags: string; + embedded_activity_config?: Pick; // TODO: is this correct? +} + +export interface ApplicationCommandIndexResponse { + applications: ApplicationCommandIndexApplication[]; + application_commands: ApplicationCommandResponse[]; + version: Snowflake; +} + export interface Application { id: Snowflake; name: string; @@ -346,18 +373,22 @@ export enum EmbeddedActivityReleasePhase { } export type ApplicationAllowlistResponse = AllowlistedUser[]; + export interface AllowlistedUser { user: PartialUser; state: ApplicationMembershipState; } + export enum ApplicationMembershipState { INVITED = 1, ACCEPTED = 2, } + export interface AllowlistAddRequestSchema { username: string; discriminator?: string | null; } + export interface AllowlistAcceptSchema { token: string; } diff --git a/src/util/util/Localization.ts b/src/util/util/Localization.ts new file mode 100644 index 000000000..db77920d1 --- /dev/null +++ b/src/util/util/Localization.ts @@ -0,0 +1,33 @@ +/* + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2026 Spacebar and Spacebar Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +export function getLocalizedValue(locale?: string, localizations?: Record, fallback?: string): string { + if (!localizations) { + if (fallback) return fallback; + throw new Error("getLocalizedValue: neither localizations nor flalback were provided!"); + } + if (!locale) { + if (fallback) return fallback; + if ("en-US" in localizations) return localizations["en-US"]; + if ("en-GB" in localizations) return localizations["en-GB"]; + throw new Error("getLocalizedValue: no target locale was provided, and the localizations neither had en-US nor en-GB!"); + } + + if (locale in localizations) return localizations[locale!]; + return getLocalizedValue(undefined, localizations, undefined); // too lazy to duplicate code +} diff --git a/src/util/util/index.ts b/src/util/util/index.ts index c21227555..9df5e1fda 100644 --- a/src/util/util/index.ts +++ b/src/util/util/index.ts @@ -29,6 +29,7 @@ export * from "./Intents"; export * from "./InvisibleCharacters"; export * from "./JSON"; export * from "./KittyLogo"; +export * from "./Localization"; export * from "./Logo"; export * from "./MessageFlags"; export * from "./networking";