diff --git a/src/types/messages/components/button_component.ts b/src/types/messages/components/button_component.ts index d72cdf137..31a91f3eb 100644 --- a/src/types/messages/components/button_component.ts +++ b/src/types/messages/components/button_component.ts @@ -12,11 +12,11 @@ export interface ButtonComponent { /** For different styles/colors of the buttons */ style: ButtonStyles; /** Emoji object that includes fields of name, id, and animated supporting unicode and custom emojis. */ - emoji?: { + emoji?: string | { /** Emoji id */ - id: string | null; - /** Emoji name (can only be null in reaction emoji objects) */ - name: string | null; + id?: string; + /** Emoji name */ + name?: string; /** Whether this emoji is animated */ animated?: boolean; }; diff --git a/src/util/utils.ts b/src/util/utils.ts index 6058d542e..511890904 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -222,49 +222,63 @@ export function hasOwnProperty( } export function validateComponents(components: MessageComponents) { - if (components?.length) { - let actionRowCounter = 0; + if (!components?.length) return; - for (const component of components) { - // 5 Link buttons can not have a customId - if (isButton(component)) { - if ( - component.type === ButtonStyles.Link && - component.customId - ) { - throw new Error(Errors.LINK_BUTTON_CANNOT_HAVE_CUSTOM_ID); - } - // Other buttons must have a customId - if ( - !component.customId && component.type !== ButtonStyles.Link - ) { - throw new Error(Errors.BUTTON_REQUIRES_CUSTOM_ID); - } + let actionRowCounter = 0; - if (!validateLength(component.label, { max: 80 })) { - throw new Error(Errors.COMPONENT_LABEL_TOO_BIG); - } - - if ( - component.customId && - !validateLength(component.customId, { max: 100 }) - ) { - throw new Error(Errors.COMPONENT_CUSTOM_ID_TOO_BIG); - } + for (const component of components) { + // 5 Link buttons can not have a customId + if (isButton(component)) { + if ( + component.type === ButtonStyles.Link && + component.customId + ) { + throw new Error(Errors.LINK_BUTTON_CANNOT_HAVE_CUSTOM_ID); + } + // Other buttons must have a customId + if ( + !component.customId && component.type !== ButtonStyles.Link + ) { + throw new Error(Errors.BUTTON_REQUIRES_CUSTOM_ID); } - if (!isActionRow(component)) { - continue; + if (!validateLength(component.label, { max: 80 })) { + throw new Error(Errors.COMPONENT_LABEL_TOO_BIG); } - actionRowCounter++; - // Max of 5 ActionRows per message - if (actionRowCounter > 5) throw new Error(Errors.TOO_MANY_ACTION_ROWS); - - // Max of 5 Buttons (or any component type) within an ActionRow - if (component.components?.length > 5) { - throw new Error(Errors.TOO_MANY_COMPONENTS); + if ( + component.customId && + !validateLength(component.customId, { max: 100 }) + ) { + throw new Error(Errors.COMPONENT_CUSTOM_ID_TOO_BIG); } + + if (typeof component.emoji === "string") { + // A snowflake id was provided + if (/^[0-9]+$/.test(component.emoji)) { + component.emoji = { + id: component.emoji, + }; + } else { + // A unicode emoji was provided + component.emoji = { + name: component.emoji, + }; + } + } + } + + if (!isActionRow(component)) { + continue; + } + + actionRowCounter++; + // Max of 5 ActionRows per message + if (actionRowCounter > 5) throw new Error(Errors.TOO_MANY_ACTION_ROWS); + + // Max of 5 Buttons (or any component type) within an ActionRow + if (component.components?.length > 5) { + throw new Error(Errors.TOO_MANY_COMPONENTS); } } }