Getting started

Let use this library!

Before getting started...

We suggest you be familiar with discord.py and python. This is a discord-components guide so it couldn't contain a lot of python explanations.

Install

First, we need to install the library

pip install --upgrade discord-components

If you meet an error No matching distribution found for discord-components when installing, try updating the python version!

Send buttons

Assuming you have invited your bot to some server, let's code. Create any python file and copy & paste the code below. (with replacing your token with your bot's token and your prefix with your bot's prefix)

bot.py
from discord.ext.commands import Bot
from discord_components import DiscordComponents, Button

bot = Bot(command_prefix = "your prefix")


@bot.event
async def on_ready():
    DiscordComponents(bot)
    print(f"Logged in as {bot.user}!")


@bot.command()
async def button(ctx):
    await ctx.send(
        "Hello, World!",
        components = [
            Button(label = "WOW button!")
        ],
    )


bot.run("your token")

You created a button! well done. Let's add a little clarification to the code.

DiscordComponents(bot) initializes the client. It changes the bot's properties. So you could create components by justawait ctx.send(..., components = [...]). Intuitive, right?

Button(label = "WOW button!") this piece of code creates a button with the label "WOW button!".

bot.py
await ctx.send(
    "Hello, World!",
    components = [
        Button(label = "WOW button!")
    ]
)

This code makes the bot send a message containing the button.

Responding to the interaction

But when you click that button, you will meet the error This interaction failed. That's because you haven't responded to the interaction (button click). Let's edit the code.

bot.py
from discord.ext.commands import Bot
from discord_components import DiscordComponents, Button

bot = Bot(command_prefix = "your prefix")


@bot.event
async def on_ready():
    DiscordComponents(bot)
    print(f"Logged in as {bot.user}!")


@bot.command()
async def button(ctx):
    await ctx.send(
        "Hello, World!",
        components = [
            Button(label = "WOW button!")
        ]
    )

    interaction = await bot.wait_for("button_click", check = lambda i: i.component.label.startswith("WOW"))
    await interaction.respond(content = "Button clicked!")


bot.run("your token")

We just added two lines and poof! An ephemeral message (a message which only can be seen by you).

await bot.wait_for("button_click", check = lambda r: r.component.label.startswith("WOW"))

This method is the same as <discord.ext.commands.Bot>.wait_for. Except for the fact it returns Interaction. This code waits until a user clicks the button with a label starting with "WOW". WOW!

bot.py
await interaction.respond(content = "Button clicked!")

This response to the interaction. It sends an ephemeral message with the content "Button clicked!". You could just send a normal message by setting the ephemeral parameter False.

Now code!

You learned how to create a button and responding to an interaction..! Have an error or a question? Look at the Frequent questions page or go to the discord server and ask a question!

Last updated