#Is there any way to pass extra data to a button interaction?
1 messages · Page 1 of 1 (latest)
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?
This is what I do, Idk if there are better ways:
.setCustomId(JSON.stringify({type: `cancel`, my_data: `sdfsdfdsf`}))
I do the same, I'm kinda avoiding collectors altogether
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?
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
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 })
}
}
}```
In the handle you can check against input.custom.my_data
Just switch out the interaction.custoimId there for parsing the json then use the customIdJSON.type?
You have to parse customId, and then you got both your initial id (which would be type in our case) and some additional data
Yeah, alright