Nuestr1k
Nuestr1k

Reputation: 1

Discord bot on Disnake. How do I make the button inactive after clicking?

I'm writing a diskord bot on Disnake. How would it be correct to add disabled to the code so that the buttons are inactive after clicking on them?

The button lights up active

@bot.slash_command()
async def buttons(inter: disnake.ApplicationCommandInteraction):
    await inter.response.send_message(
        embed = disnake.Embed( title="Heading", description="Description", color=0x992d22),
        components=[
            disnake.ui.Button(label="button1", style=disnake.ButtonStyle.secondary, custom_id="button1"),
            disnake.ui.Button(label="button2", style=disnake.ButtonStyle.secondary, custom_id="button2"),
            disnake.ui.Button(label="button3", style=disnake.ButtonStyle.secondary, custom_id="button3"),
        ],
     )

@bot.listen("on_button_click")
async def help_listener(inter: disnake.MessageInteraction):

    if inter.component.custom_id == "button1":
        embed = disnake.Embed(title="Heading button1", description="Description button1", color=0x992d22)
        await inter.response.edit_message(embed=embed)

    elif inter.component.custom_id == "button2":
        embed = disnake.Embed(title="Heading button2", description="Description button2", color=0x992d22)
        await inter.response.edit_message(embed=embed)

    elif inter.component.custom_id == "button3":
        embed = disnake.Embed(title="Heading button3", description="Description button3", color=0x992d22)
        await inter.response.edit_message(embed=embed)

Upvotes: 0

Views: 637

Answers (1)

Dominik
Dominik

Reputation: 3602

You can get the view from the current message and then modify things.

Here's a quick example:

if interaction.component.custom_id == "your_name":
    view = disnake.ui.View.from_message(interaction.message)
    for item in view.children:
        if isinstance(item, disnake.ui.Button):
            if item.custom_id == "button_you_want_to_disable":
                item.disabled = True
    await interaction.followup.edit_message(message_id=interaction.message.id, view=view)

What we are doing:

  • Getting the view from the interaction message
  • Going over every "children" and check if it's a button
  • Check for the custom_id
  • Disable the button if the IDs match

Remember to set a custom_id for every button so that we can disable it properly in the code

Upvotes: 0

Related Questions