Determining which button was clicked
This tutorial will explain how you can determine which button was clicked by a user. This is useful if your bot has more than one button, and you want to be able to link specific functionality to each
It's possible to have multiple buttons per row, and multiple rows per message. The rule is that there can be one select, or five buttons per row, and up to five rows per message. This is covered in the video here:
The basic timeline for when a button is clicked is as follows:
- 1.The button is added to the message when the bot creates the message
- 2.A user sees the message and potentially clicks one or more buttons
- 3.The ButtonInteraction event is fired
- 4.Any responses you have connected to the event node will be executed
When you add a button to a message you will be asked to enter a Custom ID. This ID is how we will determine which button was clicked. For this reason, the Custom ID should be unique for each button. Make a note of what the custom ID is for each button you have, as we'll need it later.
To determine which button was clicked we will use a Switch Case node. This node lets us check the value of a variable against multiple values. We'll connect it to the Button Interaction event so we can check the value of the customId field of the interaction event.
We can double click on the switch case node to open it. In this case we are checking the value of the buttoninteraction.customId to see which button was clicked.
In this example I am checking for two possible values- custom_id_1 and custom_id_btn2. This correlates to two buttons I have with those as their custom IDs. If the button which I set the custom ID to custom_id_btn2 is clicked then the responses connected to that output will be executed.
It's possible to have an unlimited number of buttons and have the switch case node check for an unlimited number of values. Any responses can be connected to each output, allowing you to