Discord button only works once discord.js

Viewed 562

I have posted a button but it only works once enter image description here but after that when I use it again

enter image description here

Also

How can I reply to message if I use deferUpdate it shows

Code

client.on('ready', () => {
  client.user.setActivity('people and managing the server', {
    type: 'WATCHING',
  });

  const channel = client.channels.cache.get('894171605608042496');

  const row = new MessageActionRow().addComponents(
    new MessageButton()
      .setCustomId('openTicket')
      .setLabel('Create ticket')
      .setEmoji('')
      .setStyle('SECONDARY')
  );

  channel
    .send({
      embeds: [
        {
          title: 'SGAS Tickets',
          color: '#388e3c',
          description: 'To create a ticket react with ',
        },
      ],
      components: [row],
    })
    .then(() => {
      const filter = () => {
        return true;
      };

      const collector = channel.createMessageComponentCollector({
        filter,
        time: 15 * 1000,
      });
      collector.on('collect', (i) => {
        i.deferUpdate().then(() => {
          Ticket.count({}, (err, result) => {
            if (err) {
              console.log(err);
            } else {
              const ticketNumber = result + 1;
              const ticketString = convertNumber(ticketNumber);
              const ticket = new Ticket({
                tickedId: ticketString,
              });
              ticket.save((err) => {
                if (err) {
                  console.log(err);
                } else {
                  const myguild = client.guilds.cache.get('887277806386565150');
                  if (!myguild) {
                    console.log('guild not found');
                    return;
                  }
                  const category = myguild.channels.cache.find(
                    (c) =>
                      c.id === '887277807279947826' &&
                      c.type == 'GUILD_CATEGORY'
                  );
                  if (!category) {
                    console.log('category not found');
                    return;
                  }
                  myguild.channels
                    .create(`Ticket#${ticketString}`, {
                      type: 'GUILD_TEXT',
                    })
                    .then(async (myc) => {
                      myc.setParent(category).then(() => {
                        myc.send(
                          `Hello <@${i.user.id}>, your question will be solved here shortly`
                        );
                        i.reply({
                          content: `Go to <#${myc.id}>, for your question`,
                          ephemeral: true,
                        });
                      });
                    });
                }
              });
            }
          });
        });
      });
    });
});
1 Answers

you are using a component collector - which expires in 15 seconds, as seen in your code. That means after 15 seconds your bot stops listening for button clicks. My recommendation is to use the interactionCreate event to listen for that button: see docs https://discord.js.org/#/docs/main/stable/class/ButtonInteraction

Example:

client.on("interactionCreate", (interaction) => {
 if(!interaction.isButton()) return;
 if(interaction.customId === "openTicket") {
  // your ticket code here
 }
});
Related