Why is my interaction collector not collecting? (discord.js)

Viewed 1148

I am a beginner coder in javascript and especially discord.js After wrapping my head around the discord.js guide, I still cannot get the collector to work.

client.on('interactionCreate', interaction => {
  if (!interaction.isButton()) return;
  const filter = i => i.customId === '1' || i.customId === "2";
  const collectchannel = interaction.channel;
  const collector = collectchannel.createMessageComponentCollector( filter, { time: 10000 });

  collector.on('collect', i => {
    if (i.customId === '1') {
      client.commands.get('help').execute(interaction, 1, Discord);
    } else if (i.customId === '2') {
      client.commands.get('help').execute(interaction, 2, Discord);
    }
  });

  collector.on('end', collected => {
    console.log(`Collected ${collected.size} interactions.`);
  });
});

When I press my button, the collect function doesn't do anything. I tried placing a console.log("test") but it doesn't fire. However, the collector.on('end', collected => { does in fact fire. This may be because I'm not a good coder. If you can, please help!

1 Answers

You have a MessageComponentCollector inside an event handler for the interactionCreate event, which, in this circumstance, is probably not what you want.

Here's what's happening:

  1. You click the button

  2. Your eventCreate handler fires, checks if it's a button being clicked and starts the MessageComponentCollector

  3. The MessageComponentCollector waits for a button click, and doesn't fire yet because one has already happened (the one that fired your interactionCreate handler in the first place)

  4. Whenever you click the button again, the collector fires, but the interactionCreate handler does too, and you go back to #2, starting another collector


What you probably want is to handle your button clicks without a collector:

client.on('interactionCreate', interaction => {
    if (!interaction.isButton()) return;
    if (i.customId === '1') {
        client.commands.get('help').execute(interaction, 1, Discord);
    } else if (i.customId === '2') {
        client.commands.get('help').execute(interaction, 2, Discord);
    }
});
Related