How to `update() ` the same interaction multiple times?

So I have this simple slash command that sends 3 buttons: hello, hi, and wassup
async function execute(interaction) {
const hello = new ButtonBuilder()
.setCustomId('hello')
.setLabel('Say Hello')
.setStyle(ButtonStyle.Primary);

const hi = new ButtonBuilder()
.setCustomId('hi')
.setLabel('Say Hi')
.setStyle(ButtonStyle.Primary);

const wassup = new ButtonBuilder()
.setCustomId('wassup')
.setLabel('Say wassup')
.setStyle(ButtonStyle.Primary);

const row = new ActionRowBuilder().addComponents(hello, hi, wassup);

const response = await interaction.reply({
content: 'Choose your messages',
components: [row],
});

const userChoice = await response.awaitMessageComponent();
switch (userChoice.customId) {
case "hello": return await userChoice.update("hello");
case "hi": return await userChoice.update("hi")
case "wassup": return await userChoice.update("wassup")
}
};
async function execute(interaction) {
const hello = new ButtonBuilder()
.setCustomId('hello')
.setLabel('Say Hello')
.setStyle(ButtonStyle.Primary);

const hi = new ButtonBuilder()
.setCustomId('hi')
.setLabel('Say Hi')
.setStyle(ButtonStyle.Primary);

const wassup = new ButtonBuilder()
.setCustomId('wassup')
.setLabel('Say wassup')
.setStyle(ButtonStyle.Primary);

const row = new ActionRowBuilder().addComponents(hello, hi, wassup);

const response = await interaction.reply({
content: 'Choose your messages',
components: [row],
});

const userChoice = await response.awaitMessageComponent();
switch (userChoice.customId) {
case "hello": return await userChoice.update("hello");
case "hi": return await userChoice.update("hi")
case "wassup": return await userChoice.update("wassup")
}
};
When a user click one of the buttons, the message should change into the chosen message, which works as intended. However, it only work once. The next click will result in "Interaction failed". I tried duplicating the awaitMessageComponent() function call but it still doesn't work. Any idea?
12 Replies
d.js toolkit
d.js toolkit2y ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
chewie
chewie2y ago
After calling .update() for the first time, you can use .editReply()
invictus
invictusOP2y ago
Should I make a new variable or can I just directly call .editReply() from userChoice?
chewie
chewie2y ago
You can call it on userChoice
invictus
invictusOP2y ago
Okay, let me try Apparently, I ran into another problem. It looks like the function only run twice (First after the slash command is called, second after the user responds)? After that, my console.log no longer logs any things. The next interaction only got intercepted by interactionCreate listener, which is on a different file. So the .editReply() code doesn't even get called.
chewie
chewie2y ago
You only await one button, so that makes sense Use a collector if you want to collect multiple
d.js docs
d.js docs2y ago
guide Popular Topics: Collectors read more
invictus
invictusOP2y ago
Ohhh What If I put the awaitMessageComponent on loop?
chewie
chewie2y ago
No Please dont
invictus
invictusOP2y ago
hehe it works though 😁
chewie
chewie2y ago
I mean, whatever floats your goat
invictus
invictusOP2y ago
Well, thanks anyway. I noticed that awaitMessageComponent() halts the function execution, saving the rest for the next interaction. So, the loop is safe here, as it only runs when there is a new interaction.

Did you find this page helpful?