async function createEmbedsButtonsPagination(
  messageId,
  channelId,
  authorId,
  embeds,
  defaultPage = 1,
  buttonTimeout = Milliseconds.SECOND * 30,
) {
  if (embeds.length === 0) return;
  let currentPage = defaultPage;
  const createComponents = () => [
    {
      type: DiscordMessageComponentTypes.ActionRow,
      components: [
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Previous",
          customId: `${messageId}-Previous`,
          style: DiscordButtonStyles.Primary,
          disabled: currentPage === 1,
          emoji: {
            name: "⬅️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Jump",
          customId: `${messageId}-Jump`,
          style: DiscordButtonStyles.Primary,
          disabled: embeds.length <= 2,
          emoji: {
            name: "↗️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Next",
          customId: `${messageId}-Next`,
          style: DiscordButtonStyles.Primary,
          disabled: currentPage >= embeds.length,
          emoji: {
            name: "➡️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Delete",
          customId: `${messageId}-Delete`,
          style: DiscordButtonStyles.Danger,
          emoji: {
            name: "🗑️",
          },
        },
      ],
    },
  ];
  const embedMessage = await sendMessage(channelId, {
    embed: embeds[currentPage - 1],
    components: createComponents(),
  });
  if (!embedMessage) return;
  if (embeds.length <= 1) return;
  let isEnded = false;
  while (!isEnded) {
    if (!embedMessage) {
      isEnded = true;
      break;
    }
    const collectedButton = await needButton(authorId, embedMessage.channelId, {
      duration: buttonTimeout,
    });
    console.log(collectedButton);
    if (
      !collectedButton ||
      !collectedButton.customId.startsWith(messageId.toString())
    ) {
      return;
    }
    const action = collectedButton.customId.split("-")[1];
    switch (action) {
      case "Next":
        currentPage += 1;
        break;
      // deno-lint-ignore no-case-declarations
      case "Jump":
        await sendInteractionResponse(
          snowflakeToBigint(collectedButton.interaction.id),
          collectedButton.interaction.token,
          {
            type: 6,
          },
        );
        const question = await sendMessage(
          channelId,
          "To what page would you like to jump? Say `cancel` or `0` to cancel the prompt.",
        );
        const answer = await needMessage(authorId, channelId);
        await deleteMessages(channelId, [
          question.id,
          answer.id,
        ]).catch(log.error);
        const newPageNumber = Math.ceil(Number(answer.content));
        if (
          isNaN(newPageNumber) || newPageNumber < 1 ||
          newPageNumber > embeds.length
        ) {
          await sendMessage(channelId, "This is not a valid number!");
          continue;
        }
        currentPage = newPageNumber;
        editWebhookMessage(
          snowflakeToBigint(collectedButton.interaction.applicationId),
          collectedButton.interaction.token,
          {
            messageId: embedMessage.id,
            embeds: [
              embeds[currentPage - 1],
            ],
            components: createComponents(),
          },
        );
        continue;
      case "Previous":
        currentPage -= 1;
        break;
      case "Delete":
        deleteMessage(channelId, embedMessage.id);
        isEnded = true;
        break;
    }
    if (
      isEnded || !embedMessage ||
      !await sendInteractionResponse(
        snowflakeToBigint(collectedButton.interaction.id),
        collectedButton.interaction.token,
        {
          type: 7,
          data: {
            embeds: [
              embeds[currentPage - 1],
            ],
            components: createComponents(),
          },
        },
      ).catch(log.error)
    ) {
      return;
    }
  }
}
/** This function allows to create a pagination using embeds and buttons. **/
This function allows to create a pagination using embeds and buttons.
This function allows to create a pagination using embeds and buttons .
async function createEmbedsButtonsPagination(
  messageId,
  channelId,
  authorId,
  embeds,
  defaultPage = 1,
  buttonTimeout = Milliseconds.SECOND * 30,
) {
  if (embeds.length === 0) return;
  let currentPage = defaultPage;
  const createComponents = () => [
    {
      type: DiscordMessageComponentTypes.ActionRow,
      components: [
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Previous",
          customId: `${messageId}-Previous`,
          style: DiscordButtonStyles.Primary,
          disabled: currentPage === 1,
          emoji: {
            name: "⬅️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Jump",
          customId: `${messageId}-Jump`,
          style: DiscordButtonStyles.Primary,
          disabled: embeds.length <= 2,
          emoji: {
            name: "↗️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Next",
          customId: `${messageId}-Next`,
          style: DiscordButtonStyles.Primary,
          disabled: currentPage >= embeds.length,
          emoji: {
            name: "➡️",
          },
        },
        {
          type: DiscordMessageComponentTypes.Button,
          label: "Delete",
          customId: `${messageId}-Delete`,
          style: DiscordButtonStyles.Danger,
          emoji: {
            name: "🗑️",
          },
        },
      ],
    },
  ];
  const embedMessage = await sendMessage(channelId, {
    embed: embeds[currentPage - 1],
    components: createComponents(),
  });
  if (!embedMessage) return;
  if (embeds.length <= 1) return;
  let isEnded = false;
  while (!isEnded) {
    if (!embedMessage) {
      isEnded = true;
      break;
    }
    const collectedButton = await needButton(authorId, embedMessage.channelId, {
      duration: buttonTimeout,
    });
    console.log(collectedButton);
    if (
      !collectedButton ||
      !collectedButton.customId.startsWith(messageId.toString())
    ) {
      return;
    }
    const action = collectedButton.customId.split("-")[1];
    switch (action) {
      case "Next":
        currentPage += 1;
        break;
      case "Jump":
        await sendInteractionResponse(
          snowflakeToBigint(collectedButton.interaction.id),
          collectedButton.interaction.token,
          {
            type: 6,
          },
        );
        const question = await sendMessage(
          channelId,
          "To what page would you like to jump? Say `cancel` or `0` to cancel the prompt.",
        );
        const answer = await needMessage(authorId, channelId);
        await deleteMessages(channelId, [
          question.id,
          answer.id,
        ]).catch(log.error);
        const newPageNumber = Math.ceil(Number(answer.content));
        if (
          isNaN(newPageNumber) || newPageNumber < 1 ||
          newPageNumber > embeds.length
        ) {
          await sendMessage(channelId, "This is not a valid number!");
          continue;
        }
        currentPage = newPageNumber;
        editWebhookMessage(
          snowflakeToBigint(collectedButton.interaction.applicationId),
          collectedButton.interaction.token,
          {
            messageId: embedMessage.id,
            embeds: [
              embeds[currentPage - 1],
            ],
            components: createComponents(),
          },
        );
        continue;
      case "Previous":
        currentPage -= 1;
        break;
      case "Delete":
        deleteMessage(channelId, embedMessage.id);
        isEnded = true;
        break;
    }
    if (
      isEnded || !embedMessage ||
      !await sendInteractionResponse(
        snowflakeToBigint(collectedButton.interaction.id),
        collectedButton.interaction.token,
        {
          type: 7,
          data: {
            embeds: [
              embeds[currentPage - 1],
            ],
            components: createComponents(),
          },
        },
      ).catch(log.error)
    ) {
      return;
    }
  }
}
async function createEmbedsButtonsPagination ( messageId , channelId , authorId , embeds , defaultPage = 1 , buttonTimeout = Milliseconds . SECOND * 30 , ) { if ( embeds . length === 0 ) return ; let currentPage = defaultPage ; const createComponents = ( ) => [ { type : DiscordMessageComponentTypes . ActionRow , components : [ { type : DiscordMessageComponentTypes . Button , label : "Previous" , customId : ` ${ messageId } ` , style : DiscordButtonStyles . Primary , disabled : currentPage === 1 , emoji : { name : "⬅️", } , } , { type : DiscordMessageComponentTypes . Button , label : "Jump" , customId : ` ${ messageId } ` , style : DiscordButtonStyles . Primary , disabled : embeds . length <= 2 , emoji : { name : "↗️", } , } , { type : DiscordMessageComponentTypes . Button , label : "Next" , customId : ` ${ messageId } ` , style : DiscordButtonStyles . Primary , disabled : currentPage >= embeds . length , emoji : { name : "➡️", } , } , { type : DiscordMessageComponentTypes . Button , label : "Delete" , customId : ` ${ messageId } ` , style : DiscordButtonStyles . Danger , emoji : { name : "🗑️", } , } , ] , } , ] ; const embedMessage = await sendMessage ( channelId , { embed : embeds [ currentPage - 1 ] , components : createComponents ( ) , } ) ; if ( ! embedMessage ) return ; if ( embeds . length <= 1 ) return ; let isEnded = false ; while ( ! isEnded ) { if ( ! embedMessage ) { isEnded = true ; break ; } const collectedButton = await needButton ( authorId , embedMessage . channelId , { duration : buttonTimeout , } ) ; console . log ( collectedButton ) ; if ( ! collectedButton || ! collectedButton . customId . startsWith ( messageId . toString ( ) ) ) { return ; } const action = collectedButton . customId . split ( "-" ) [ 1 ] ; switch ( action ) { case "Next" : currentPage += 1 ; break ; case "Jump" : await sendInteractionResponse ( snowflakeToBigint ( collectedButton . interaction . id ) , collectedButton . interaction . token , { type : 6 , } , ) ; const question = await sendMessage ( channelId , "To what page would you like to jump? Say `cancel` or `0` to cancel the prompt." , ) ; const answer = await needMessage ( authorId , channelId ) ; await deleteMessages ( channelId , [ question . id , answer . id , ] ) . catch ( log . error ) ; const newPageNumber = Math . ceil ( Number ( answer . content ) ) ; if ( isNaN ( newPageNumber ) || newPageNumber < 1 || newPageNumber > embeds . length ) { await sendMessage ( channelId , "This is not a valid number!" ) ; continue ; } currentPage = newPageNumber ; editWebhookMessage ( snowflakeToBigint ( collectedButton . interaction . applicationId ) , collectedButton . interaction . token , { messageId : embedMessage . id , embeds : [ embeds [ currentPage - 1 ] , ] , components : createComponents ( ) , } , ) ; continue ; case "Previous" : currentPage -= 1 ; break ; case "Delete" : deleteMessage ( channelId , embedMessage . id ) ; isEnded = true ; break ; } if ( isEnded || ! embedMessage || ! await sendInteractionResponse ( snowflakeToBigint ( collectedButton . interaction . id ) , collectedButton . interaction . token , { type : 7 , data : { embeds : [ embeds [ currentPage - 1 ] , ] , components : createComponents ( ) , } , } , ) . catch ( log . error ) ) { return ; } } }
This function allows to create a pagination using embeds and buttons.
This function allows to create a pagination using embeds and buttons .
// deno-lint-ignore no-case-declarations