Updating Discord Bot Response with Button Interaction

I’m working on a Discord bot using discord.py where the bot sends several messages with a select menu. After making 8 selections, the bot displays a ‘Continue’ button. When the button is clicked, the expected message update fails and shows ‘Interaction failed’.

Here’s what should happen:

  1. The bot sends an initial message with a select menu.
  2. The user makes 8 choices, updating the message each time.
  3. Once the 8 selections are done, a ‘Continue’ button appears.
  4. Clicking the button should update the message, but it doesn’t.

I’ve tried changing the order of message sending and editing, as well as using different interaction objects, but nothing seems to work. Below is a simplified version of my code:

async def start_quiz(interaction):
    await interaction.response.send_message(embed=initial_embed, view=StartButton())

class StartButton(discord.ui.View):
    @discord.ui.button(label='Start', style=discord.ButtonStyle.blurple)
    async def start_callback(self, interaction, button):
        await ask_question(interaction, 1)

async def ask_question(interaction, question_num):
    if question_num <= 8:
        await interaction.response.edit_message(embed=question_embed, view=QuestionSelect())
    else:
        await show_result(interaction)

class QuestionSelect(discord.ui.View):
    @discord.ui.select()
    async def select_callback(self, interaction, select):
        # Increment question count for simplicity
        await ask_question(interaction, int(interaction.message.embeds[0].footer.text) + 1)

async def show_result(interaction):
    await interaction.response.edit_message(embed=result_embed, view=ContinueButton())

class ContinueButton(discord.ui.View):
    @discord.ui.button(label='Continue', style=discord.ButtonStyle.blurple)
    async def continue_callback(self, interaction, button):
        await interaction.response.edit_message(embed=final_embed, view=None)

Any advice on how to fix this issue?

I’ve faced this exact problem before, and I think I know what’s going on. The issue is likely with the interaction timing out. When you click the ‘Continue’ button, the original interaction from the select menu has already expired.

Here’s what worked for me:

In your ContinueButton class, modify the continue_callback method like this:

async def continue_callback(self, interaction, button):
    await interaction.response.defer()
    await interaction.followup.edit_message(embed=final_embed, view=None)

By deferring the response and then using followup.edit_message(), you’re creating a new interaction that won’t time out. This should solve the ‘Interaction failed’ error.

Also, make sure your bot has the ‘Message Content Intent’ enabled in the Discord Developer Portal. Without it, your bot might not be able to read message content, which could cause issues with button interactions.

Let me know if this solves your problem!

hey there! i’ve run into similar issues before. have u tried using interaction.followup.edit_message() instead of interaction.response.edit_message() in the continue_callback? sometimes the original interaction expires and u need to use followup. also, double-check ur button permissions. hope this helps!

I encountered a similar problem in one of my Discord bot projects. The issue might be related to interaction timeouts. Try wrapping your button callback in a try-except block to catch any potential errors. Also, consider using interaction.followup.edit_message() instead of interaction.response.edit_message() in the continue_callback method. This approach often resolves interaction failures, especially when there’s a delay between the initial interaction and the button click. Additionally, ensure that your bot has the necessary permissions in the channel where it’s sending messages. If the problem persists, you might want to check Discord’s API status for any ongoing issues.