Search code examples
javascriptbuttondiscordrows

collector.on 'collect' event with button rows, discord js buttons


I'm using button rows for the first time and I use

This

collector.on('collect', Button => {
        ...
});

For my buttons collector (on message with 3 rows of buttons), as I did before

But I suppose it should look somewhat different because it runs but doesnt recognize the button


Solution

  • When a button is clicked, it fires the "interactionCreate" event. So to filter the slash commands and the message components you should use:

    client.on("interactionCreate", interaction => {
        if (interaction.type == "APPLICATION_COMMAND") {
            client.emit("slashCommand", interaction)
        }
        else if (interaction.type == "MESSAGE_COMPONENT") {
            client.emit("clickButton", interaction)
        }
    }
    

    And then you can just create that 2 collectors like this:

    client.on("slashCommand", SlashCommand => {
        //your code
    })
    
    client.on("clickButton", Button => {
        //your code
    })