Skip to main content

Buttons

Components allow users to interact with your bot through interactive UI elements. In this section, we will discuss how bots can receive and process information from these elements through the library.


AbhigyanTripsused /buttons
Disnake BotBot05/19/2024
Need help?

The code for this command is given below.

buttons.py
# At the top of the file.
import disnake
from disnake.ext import commands

# The slash command that responds with a message.
@bot.slash_command()
async def buttons(inter: disnake.ApplicationCommandInteraction):
await inter.response.send_message(
"Need help?",
components=[
disnake.ui.Button(label="Yes", style=disnake.ButtonStyle.success, custom_id="yes"),
disnake.ui.Button(label="No", style=disnake.ButtonStyle.danger, custom_id="no"),
],
)


@bot.listen("on_button_click")
async def help_listener(inter: disnake.MessageInteraction):
if inter.component.custom_id not in ["yes", "no"]:
# We filter out any other button presses except
# the components we wish to process.
return

if inter.component.custom_id == "yes":
await inter.response.send_message("Contact us at https://discord.gg/disnake!")
elif inter.component.custom_id == "no":
await inter.response.send_message("Got it. Signing off!")

Building and sending buttons

Button styles

NameSyntaxColor
PrimaryButtonStyle.primary / ButtonStyle.blurpleBlurple
SecondaryButtonStyle.secondary / ButtonStyle.grey / ButtonStyle.grayGrey
SuccessButtonStyle.success / ButtonStyle.green Green
DangerButtonStyle.danger / ButtonStyle.redRed
LinkButtonStyle.link / ButtonStyle.urlGrey

Disnake BotBot05/19/2024
Link

note

Link buttons cannot have a custom_id, and do not send an interaction event when clicked.

Disabled buttons

Receiving button callback

Views vs. low-level components