niles-bot / niles

Niles - a Discord bot for interfacing with Google Calendar
http://nilesbot.com
MIT License
61 stars 22 forks source link

Component Collection (Kevin Kumar) #195

Open mchangrh opened 3 years ago

mchangrh commented 3 years ago

code fragment sent by Kevin Kumar#4660

export default async function (
  interaction: CommandInteraction | ButtonInteraction | SelectMenuInteraction | Message,
  frage: InteractionReplyOptions,
  onConfirm?: (i: ButtonInteraction) => void,
  customJa?: string
) {
  const uniqueId = nanoid();
  if (!interaction) return console.log("error" + "🚀 No interaction found in confirm");
  const ja = new MessageButton()
    .setCustomId(uniqueId)
    .setLabel(customJa ?? "Ja")
    .setStyle("SUCCESS");
  if (interaction instanceof Message) {
    interaction.reply({
      ...frage,
      components: [new MessageActionRow().addComponents([ja])],
    });
  } else {
    interaction.reply({
      content: `<@${interaction.user.id}>\n ${frage.content}`,
      components: [new MessageActionRow().addComponents([ja])],
      ephemeral: true,
      ...frage,
    });
  }

  const collector = interaction.channel.createMessageComponentCollector({
    componentType: "BUTTON",
    time: COLLECTOR_TIME,
  });
  collector.on("collect", async (i: ButtonInteraction) => {
    if (i.customId == uniqueId) {
      await onConfirm(i);
    }
  });
  collector.on("dispose", (i) => {
    if (collector.collected.size === 0) {
      i.reply({
        content: COLLECTORDISPOSEMESSAGE,
        ephemeral: true,
      });
    }
  });
}