Search code examples
pythonasync-awaitdiscorddiscord.pydiscord-buttons

Discord.py not responding to button events


Here's a test command in my bot.

@tree.command(name="button", description="testing")
async def button(interaction):
    view = discord.ui.View()  # make a view
    style = discord.ButtonStyle.primary  # makes the button blue
    item = discord.ui.Button(style=style, label="Click to continue!", custom_id="continue_button")  # make the button
    view.add_item(item=item)  # add button to view
    await interaction.response.send_message("Waiting until you press the button!", view=view)  # send message with button

    def check(i):
        return i.user.id == interaction.user.id and i.custom_id == "continue_button"

    try:
        interaction, button = await client.wait_for("button_click", check=check, timeout=10)
        # when button is pressed
        await interaction.channel.send("You pressed the button!")
    except asyncio.TimeoutError:
        # button wasn't pressed before timeout
        await interaction.channel.send("You didn't press the button within the specified time.")

However, the bot never gets the button click event! Even if the check is nothing, no custom ID, etc, the bot times out and click the button results in interaction failed. Also, I'd like to keep the button and listening within the command so that I can execute more code based on the button the user presses.

I expected the bot to send a message that I had clicked the button, but I always get a timeout even after pressing the button. I have tried removing the check, timeout, etc, but I cannot get the event to occur.


Solution

  • The issue was caused because there is not a button_click event. Instead, look for interactions that were caused by buttons:

    interaction = await client.wait_for('interaction', 
        check=lambda i:
        i.type == discord.InteractionType.component and
        i.user == interaction.user and
        i.channel == interaction.channel, timeout=30)