#Is there any way to pass extra data to a button interaction?

1 messages · Page 1 of 1 (latest)

little bolt
#

You can pass as much data as you would ever need

carmine hamlet
#

Like, when creating the button? ```js
new MessageActionRow().addComponents(new MessageButton()
.setCustomId('cancel')
.setLabel("Cancel")
.setStyle("DANGER")),

#

i.e., a specific user id since I'm handling buttons outside of the command generating the button. Or should I just handle it in the command?

little bolt
little bolt
carmine hamlet
#

How do you handle that in a manager then? Because I have it how the guide does events and commands. Where it goes through the folder and hooks to the button with the name of the file

#

Just parse the json in each button click and check the type against the name of the file?

little bolt
#

I have no idea what manager you're talking about, but here's how I handle it:

input.custom = JSON.parse(interaction_json);
handlers[custom.type](input);
#

And for every type I have a handler inside handlers

carmine hamlet
#
else if (interaction.isButton()) {
      const Client = interaction.client;
      const button = Client.buttons.get(interaction.customId);

      if (!button) return;

      try {
        await button.execute(Client, interaction);
      } catch (e) {
        console.error(`[ERROR] ${e}`);
        try {
          await interaction.reply({ content: `There was an error executing this button function!`, ephemeral: true });
        } catch(e) {
          await interaction.editReply({ content: `There was an error executing this button function!`, ephemeral: true })
        }
      }
    }```
little bolt
#

In the handle you can check against input.custom.my_data

carmine hamlet
#

Just switch out the interaction.custoimId there for parsing the json then use the customIdJSON.type?

little bolt
#

You have to parse customId, and then you got both your initial id (which would be type in our case) and some additional data

carmine hamlet
#

Yeah, alright