Buttons#

A simple example on the use of buttons with disnake-ext-components.

First and foremost, we create a bot as per usual. Since we don’t need any prefix command capabilities, we opt for an disnake.ext.commands.InteractionBot.

examples/button.py - create a Bot object#
import os

import disnake
from disnake.ext import commands, components

bot = commands.InteractionBot()

Next, we make a component manager and register all components (current and future) to this manager.

examples/button.py - create a component manager and register components#
manager = components.ComponentManager(bot)
manager.basic_config()

Then we make a simple component.

examples/button.py - create a component#
class MyButton(components.RichButton):
    label = "0"  # Set the label of the button...

    count: int  # Define a custom_id parameter...

    async def callback(self, interaction: components.MessageInteraction) -> None:
        self.count += 1  # Increment count...
        self.label = str(self.count)  # update label to count...

        await interaction.response.edit_message(components=self)  # ...and update it!

Finally, we make a command that sends the component.

examples/button.py - create a command and send the component#
@bot.slash_command()  # pyright: ignore  # still some unknowns in disnake
async def test_button(inter: disnake.CommandInteraction) -> None:
    # Wrapping the interaction allows you to send the component as-is.
    wrapped = components.wrap_interaction(inter)
    component = MyButton(count=0)

    await wrapped.send(components=component)

    # If we had not wrapped the interaction, we would have needed to do
    # `await inter.send(components=await component.as_ui_component())`
    # instead.


# Lastly, we run the bot.
bot.run(os.getenv("EXAMPLE_TOKEN"))

Source code#

examples/button.py#
 1import os
 2
 3import disnake
 4from disnake.ext import commands, components
 5
 6bot = commands.InteractionBot()
 7manager = components.ComponentManager(bot)
 8manager.basic_config()
 9
10
11class MyButton(components.RichButton):
12    label = "0"  # Set the label of the button...
13
14    count: int  # Define a custom_id parameter...
15
16    async def callback(self, interaction: components.MessageInteraction) -> None:
17        self.count += 1  # Increment count...
18        self.label = str(self.count)  # update label to count...
19
20        await interaction.response.edit_message(components=self)  # ...and update it!
21
22
23@bot.slash_command()  # pyright: ignore  # still some unknowns in disnake
24async def test_button(inter: disnake.CommandInteraction) -> None:
25    # Wrapping the interaction allows you to send the component as-is.
26    wrapped = components.wrap_interaction(inter)
27    component = MyButton(count=0)
28
29    await wrapped.send(components=component)
30
31    # If we had not wrapped the interaction, we would have needed to do
32    # `await inter.send(components=await component.as_ui_component())`
33    # instead.
34
35
36bot.run(os.getenv("EXAMPLE_TOKEN"))