mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 09:50:07 +00:00
Merge pull request #910 from discordeno/fix-emoji-button-type
allow default unicode emojis
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -222,49 +222,63 @@ export function hasOwnProperty<T extends {}, Y extends PropertyKey = string>(
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user