More in depth error logging, fix schemas for application commands

This commit is contained in:
Rory& committed 2026-11-05 23:43:52 +01:00
1 parent 8601d5853e
commit 4c8331fd7a
15 files changed
+472 -212

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+7 -1
View File
@@ -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!);
@@ -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;
@@ -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);
},
);
@@ -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,8 +129,56 @@ router.patch(
version: Snowflake.generate(),
};
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(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;
@@ -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);
},
);
@@ -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;
+15
View File
@@ -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;
}
}
+28 -1
View File
@@ -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;
}
}
+1 -1
View File
@@ -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: "<<undefined>>", keyword: e.keyword };
else if (e.keyword == "anyOf") return { instancePath: instancePath, value: "<<irrelevant>>", 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);
@@ -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<Snowflake, boolean>;
+33 -2
View File
@@ -16,10 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<EmbeddedActivityConfig, "supported_platforms">; // 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;
}
+33
View File
@@ -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 <https://www.gnu.org/licenses/>.
*/
export function getLocalizedValue(locale?: string, localizations?: Record<string, string>, 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
}
+1
View File
@@ -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";