run: async()

in src/actions/Expand.ts [37:123]


  run: async (client: Client, interaction: ButtonInteraction) => {
    if (interaction.message.embeds.length == 0) {
      interaction.deferUpdate();
      return;
    }
    if (interaction.message.components.length != 1) {
      // either missing buttons or already showing expand buttons (or other buttons)
      interaction.deferUpdate();
      return;
    }
    const customId = interaction.customId;
    const matchResults = customId.match(/expand:(\d)/);
    if (!matchResults || matchResults.length != 2) {
      interaction.deferUpdate();
      return;
    }

    const count = parseInt(matchResults[1]);
    if (count == 0) {
      interaction.deferUpdate();
      return;
    }
    if (count == 1) {
      await performExpandAction(interaction, 1, 1).catch((e) => {
        if (LOG_ERRORS) {
          console.log(e);
        }
      });
      return;
    }

    const existingActions = actionsFromRow(interaction.message.components[0]);
    const mainRow = rowFromActions(existingActions, { count: count });

    var row = new ActionRowBuilder<ButtonBuilder>();
    var newRows = [mainRow, row];

    for (var i = 0; i <= count; i++) {
      if (row.components.length == 5) {
        row = new ActionRowBuilder<ButtonBuilder>();
        newRows.push(row);
      }
      if (i == 0) {
        const button = new ButtonBuilder()
          .setCustomId(`expand_picker:close`)
          .setLabel(`❌`)
          .setStyle(ButtonStyle.Secondary);
        row.addComponents(button);
      } else {
        const button = new ButtonBuilder()
          .setCustomId(`expand_picker:${i}`)
          .setLabel(`🔭 ${i}`)
          .setStyle(ButtonStyle.Secondary);
        row.addComponents(button);
      }
    }

    await interaction.update({ components: newRows });

    const collector = interaction.message.createMessageComponentCollector({
      componentType: ComponentType.Button,
      time: 6000,
    });
    collector.on("collect", (i) => {
      if (
        i.user.id === interaction.user.id &&
        i.customId.startsWith("expand_picker:")
      ) {
        collector.stop();
        const matchResults = i.customId.match(/expand_picker:(\d)/);
        if (matchResults && matchResults.length == 2) {
          const step = parseInt(matchResults[1]);
          if (step) {
            performExpandAction(i, step, count).catch(console.log);
            return;
          }
        }
        i.deferUpdate();
      }
      // else don't defer reply because it is not our button and should be handled by whatever owns it
    });

    collector.on("end", (collected) => {
      // put old buttons back
      interaction.editReply({ components: [mainRow] });
    });
  },