fix: check for duplicated component IDs
This commit is contained in:
parent
180200dc9c
commit
56dd6c87ee
@ -16,7 +16,7 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
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 { route } from "@spacebar/api";
|
||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, Router } from "express";
|
||||||
import { emitEvent, FieldErrors, InteractionSuccessEvent, Message, MessageCreateEvent, pendingInteractions, User } from "@spacebar/util";
|
import { emitEvent, FieldErrors, InteractionSuccessEvent, Message, MessageCreateEvent, pendingInteractions, User } from "@spacebar/util";
|
||||||
@ -27,6 +27,7 @@ router.post("/", route({}), async (req: Request, res: Response) => {
|
|||||||
const body = req.body as InteractionCallbackSchema;
|
const body = req.body as InteractionCallbackSchema;
|
||||||
|
|
||||||
const errors: Record<string, { code?: string; message: string }> = {};
|
const errors: Record<string, { code?: string; message: string }> = {};
|
||||||
|
const knownComponentIds: string[] = [];
|
||||||
|
|
||||||
for (const row of body.data.components || []) {
|
for (const row of body.data.components || []) {
|
||||||
if (!row.components) {
|
if (!row.components) {
|
||||||
@ -39,6 +40,26 @@ router.post("/", route({}), async (req: Request, res: Response) => {
|
|||||||
message: `Must be between 1 and 5 in 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) {
|
if (Object.keys(errors).length > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user