pythondiscorddiscord.py

How to change response sent by discord bot with a button


With discord.py, I’m creating a bot that, following a command, sends a sequence of responses with a select menu. Based on the choice made in the menu, it updates the text and the select menu displayed in the response. After 8 select menus, I modify the response again with new text and a "Continue" button. Then, I send a message in the channel. When the user clicks the "Continue" button, it should modify the response again, but at this step, it fails—the response doesn’t update and displays "Interaction failed."

async def cmd_choixpeau(interaction : discord.Interaction) :
    class bouton_choixpeau_suivant(discord.ui.View) :
            @discord.ui.button(label="C'est partis", style=discord.ButtonStyle.blurple)
            async def suivant_callback(self, interaction : discord.Interaction, button) :
                await questionnaire_choixpeau(interaction, Q1, id)

    await interaction.response.send_message(embed = embed_choixpeau, view = bouton_choixpeau_suivant(), ephemeral = True)
async def questionnaire_choixpeau(interaction : discord.Interaction, numQ : dict, id : int) :
    class select_questionnaire(discord.ui.View):
        @discord.ui.select( ... )
        async def select_callback(self, ctx : discord.Interaction, select) : 
            if numQ == Q1 :
                await questionnaire_choixpeau(ctx, Q2, id)
            elif numQ == Q2 :
                await questionnaire_choixpeau(ctx, Q3, id)
            elif numQ == Q3 :
                await questionnaire_choixpeau(ctx, Q4, id)
            elif numQ == Q4 :
                await questionnaire_choixpeau(ctx, Q5, id)
            elif numQ == Q5 :
                await questionnaire_choixpeau(ctx, Q6, id)
            elif numQ == Q6 :
                await questionnaire_choixpeau(ctx, Q7, id)
            elif numQ == Q7 :
                await questionnaire_choixpeau(ctx, Q8, id)
            elif numQ == Q8 :
                await attribution_maison(ctx, id)

    await interaction.response.edit_message(embed = embed, attachments = [], view = select_questionnaire())
async def attribution_maison(interaction : discord.Interaction, id : int) :
    . . . 
    await annonce_maison(interaction, id, max_key[0])
async def annonce_maison(interaction : discord.Interaction, id : int, maison : str) :
    class bouton_tuto_suivant(discord.ui.View) :
        def __init__(self, maison):
            super().__init__()  # Appel au constructeur parent pour initialiser la vue
            self.maison = maison
        
        @discord.ui.button(label="Continuer", style=discord.ButtonStyle.blurple)
        async def suivant_callback(self, ctx : discord.Interaction, button) : 
            
            if maison == "Gryffondor" :
                bienvenue_gryffondor = discord.Embed(...)
                await ctx.response.edit_message(embed = bienvenue_gryffondor, view = None)
            ...
    embed = discord.Embed(...)
    await interaction.response.edit_message(embed = embed, view = bouton_tuto_suivant(maison))

I tried changing the order of sending the message and updating the response, as well as using the interaction from the function and the one from the button, but it didn’t work.


Solution

  • In the "suivant_callback," try editing the response outside of an "if" block directly to see what happens.