Discord.js | Why buttons just can be used once?

43 Views Asked by At

I made a ticket system but buttons inside the ticket just can be used once.
Here is how it should work:

Open the ticket
Send message with button (Lock Ticket) in the ticket just opened
When Lock Ticket button is clicked, send message inside that ticket to confirm the action
There are 2 options: Confirm or Cancel. Confirm will lock the ticket (change permissions and rename channel). Cancel just delete the message and nothing happen.
If clicked confirm, new message sent with 2 buttons: UNLOCK and DELETE
If clicked unlock, it will be normal ticket, access user again, rename channel
If clicked delete, after 3 seconds it will delete the channel

However, if I clicked lock button, then click cancel. Again, click lock and confirm but it just sent message to ask UNLOCK or DELETE, channel didn't change anything. And this also cause that UNLOCK or DELETE buttons didn't work.

Here is the code to lock the ticket and handle Confirm/Cancel

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton() || !interaction.customId.startsWith('close-ticket')) return;
  await interaction.deferReply({ ephemeral: true });
  let type = interaction.customId.split('-')[2] || null;

  if (!type) {
    await interaction.channel.send({
      embeds: [
        new MessageEmbed()
          .setDescription(client.msg['xacnhan_dongticket'])
      ],
      components: [
        new MessageActionRow().addComponents(
          new MessageButton()
            .setEmoji('✔')
            .setLabel('CONFIRM')
            .setStyle('SECONDARY')
            .setCustomId('close-ticket-confirm'),
          new MessageButton()
            .setEmoji('❌')
            .setLabel('CANCEL')
            .setStyle('SECONDARY')
            .setCustomId('close-ticket-cancel')
        )
      ]
    });
  } else if (type == 'confirm') {
    await interaction.message.delete();
    await interaction.channel.send({
      embeds: [
        new MessageEmbed()
          .setDescription(client.msg['lockticket'])
      ],
      components: [
        new MessageActionRow().addComponents(
          new MessageButton()
            .setEmoji('')
            .setLabel('UNLOCK')
            .setStyle('SECONDARY')
            .setCustomId('reopen-ticket'),
          new MessageButton()
            .setEmoji('')
            .setLabel('DELETE')
            .setStyle('SECONDARY')
            .setCustomId('delete-ticket')
        )
      ]
    });

    await interaction.channel.setName(`closed-${interaction.channel.name}`);
    await interaction.channel.permissionOverwrites.edit(interaction.channel.topic, {
      VIEW_CHANNEL: false,
      SEND_MESSAGES: false
    });
  } else if (type == 'cancel') {
    await interaction.message.delete();
  }
  await interaction.deleteReply();
});

Here is the code for Unlock/Delete button (After Confirm Lock)

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton()) return;
  if (interaction.customId == 'reopen-ticket') {
    await interaction.channel.setName(`${interaction.channel.name.replaceAll('closed-', '')}`);
    await interaction.channel.permissionOverwrites.edit(interaction.channel.topic, {
      VIEW_CHANNEL: true,
      SEND_MESSAGES: true
    });
    await interaction.message.delete();
  } else if (interaction.customId == 'delete-ticket') {
    const attachment = await createTranscript(interaction.channel, { fileName: `${interaction.channel.name}.html` });
    let data = await db.findOne({});
    if (!data) return;
    await interaction.guild.channels.cache.get(data.logId).send({
      files: [attachment],
    });
    await interaction.message.delete();
    setTimeout(async () => await interaction.channel.delete(), 3000);
  }
});
Note: channel.topic is user id, it already set when create channel (because I don't wanna make database for this)

Is that because I deferReply before or something? Please help, tysm!

0

There are 0 best solutions below