Passing down arguments of Commands to the Interaction Handlers

I have a command that takes a user argument like /clan member @MindLabor and this command creates a message with Buttons. I also have a button interaction handler which runs when the button is clicked. But in that interaction handler I cannot find the arguments of my original command that created the message. Is there a good way of "passing" the data from where the buttons are built to the handler? Button Message Creation:
return interaction.reply({
content: `Choose the action you want to perform on this user/member:`,
ephemeral: true,
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId('add-member').setLabel('Add').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('promote-member').setLabel('Promote').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('demote-member').setLabel('Demote').setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId('kick-member').setLabel('Kick').setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId('remove-member').setLabel('Remove').setStyle(ButtonStyle.Danger)
)
]
});
return interaction.reply({
content: `Choose the action you want to perform on this user/member:`,
ephemeral: true,
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId('add-member').setLabel('Add').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('promote-member').setLabel('Promote').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('demote-member').setLabel('Demote').setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId('kick-member').setLabel('Kick').setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId('remove-member').setLabel('Remove').setStyle(ButtonStyle.Danger)
)
]
});
Button Handler
public async run(interaction: ButtonInteraction) {
// const clanName = interaction.options.getString('name'); // DOESNT WORK
const memberId = interaction.message.interaction?.user.id;
console.log(memberId);
console.log(interaction);
switch (interaction.customId) {
case "add-member":
console.log("add-member"); // WORKS
}
// AHHHHHHHahgdjgha ajks ja!!
}
public async run(interaction: ButtonInteraction) {
// const clanName = interaction.options.getString('name'); // DOESNT WORK
const memberId = interaction.message.interaction?.user.id;
console.log(memberId);
console.log(interaction);
switch (interaction.customId) {
case "add-member":
console.log("add-member"); // WORKS
}
// AHHHHHHHahgdjgha ajks ja!!
}
Solution:
That way, my button handler can check if the label starts with the unique-label, then I can checke if the user who clicked the button matches the user who requested the button, and finally I get whatever other information I need to actually preform the action (in your case that looks like it owuld be another userID)
Jump to solution
4 Replies
Ben
Ben2y ago
The best way I've found is to pass information through the custom ID's themselves. I usually use the format of unique-label|userId-that-requested-button|extra-info
Solution
Ben
Ben2y ago
That way, my button handler can check if the label starts with the unique-label, then I can checke if the user who clicked the button matches the user who requested the button, and finally I get whatever other information I need to actually preform the action (in your case that looks like it owuld be another userID)
Favna
Favna2y ago
ThisPoint there is no real other way with the Discord API. The only alternative is having a Map that you export from somewhere then write keys to it and read from it in the handlers. Basically dependency injection.
Goosy
Goosy2y ago
ah I see, will do for my purpose that hurts my soul but I'll keep it in mind xD
Want results from more Discord servers?
Add your server