Merge pull request #1371 from CyberL1/fix/interaction-component-checks

Add component checks
This commit is contained in:
Cyber 2025-10-28 17:39:27 +01:00 committed by GitHub
commit 67d8b03525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,16 +16,56 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { InteractionCallbackSchema, InteractionCallbackType, MessageCreateSchema, MessageType } from "@spacebar/schemas";
import { ButtonStyle, InteractionCallbackSchema, InteractionCallbackType, MessageComponentType, MessageCreateSchema, MessageType } from "@spacebar/schemas";
import { route } from "@spacebar/api";
import { Request, Response, Router } from "express";
import { emitEvent, InteractionSuccessEvent, Message, MessageCreateEvent, pendingInteractions, User } from "@spacebar/util";
import { emitEvent, FieldErrors, InteractionSuccessEvent, Message, MessageCreateEvent, pendingInteractions, User } from "@spacebar/util";
const router = Router({ mergeParams: true });
router.post("/", route({}), async (req: Request, res: Response) => {
const body = req.body as InteractionCallbackSchema;
const errors: Record<string, { code?: string; message: string }> = {};
const knownComponentIds: string[] = [];
for (const row of body.data.components || []) {
if (!row.components) {
continue;
}
if (row.components.length < 1 || row.components.length > 5) {
errors[`data.components[${body.data.components!.indexOf(row)}].components`] = {
code: "BASE_TYPE_BAD_LENGTH",
message: `Must be between 1 and 5 in length.`,
};
}
for (const component of row.components) {
if (component.type == MessageComponentType.Button && component.style != ButtonStyle.Link) {
if (component.custom_id?.trim() === "") {
errors[`data.components[${body.data.components!.indexOf(row)}].components[${row.components.indexOf(component)}].custom_id`] = {
code: "BUTTON_COMPONENT_CUSTOM_ID_REQUIRED",
message: "A custom id required",
};
}
if (knownComponentIds.includes(component.custom_id!)) {
errors[`data.components[${body.data.components!.indexOf(row)}].components[${row.components.indexOf(component)}].custom_id`] = {
code: "COMPONENT_CUSTOM_ID_DUPLICATED",
message: "Component custom id cannot be duplicated",
};
} else {
knownComponentIds.push(component.custom_id!);
}
}
}
}
if (Object.keys(errors).length > 0) {
throw FieldErrors(errors);
}
const interactionId = req.params.interaction_id;
const interaction = pendingInteractions.get(req.params.interaction_id);