Search code examples
pythondiscord.pypycord

Python py-cord / discord.py(2.0.0): Button interaction failed


If I click on my Button everything works as expected, but in Discord is's showing:

enter image description here

This is the Button Code:

# Start Button
start_b = Button(label="Starten", style=discord.ButtonStyle.gray)

async def start_callback(interaction: discord.Interaction):
    if not is_server_running():
        msg = await interaction.channel.send(embed=discord.Embed(title='START: Starte Server'))
        start_server()
        await msg.delete()
    else:
        msg = await interaction.channel.send(embed=discord.Embed(title='Server läuft noch'))
        time.sleep(5)
        await msg.delete()

start_b.callback = start_callback

...

view.add_item(start_b)
await bot.get_channel(Some ID).send(embed=discord.Embed(title="Dashboard"), view=view)

I guess I have to acknowledge the button interaction somehow. Is there a way to do this without sending reply messages?


Solution

  • Finally I found it 🥳

    You have to add this at the top of your callback:

    await interaction.response.defer()
    

    In my example this would be:

    # Start Button
    start_b = Button(label="Starten", style=discord.ButtonStyle.gray)
    
    async def start_callback(interaction: discord.Interaction):
    
    await interaction.response.defer() # <---- Here
    
        if not is_server_running():
            msg = await interaction.channel.send(embed=discord.Embed(title='START: Starte Server'))
            start_server()
            await msg.delete()
        else:
            msg = await interaction.channel.send(embed=discord.Embed(title='Server läuft noch'))
            time.sleep(5)
            await msg.delete()
    
    start_b.callback = start_callback
    
    ...
    
    view.add_item(start_b)
    await bot.get_channel(Some ID).send(embed=discord.Embed(title="Dashboard"), view=view)