Skip to content

Instantly share code, notes, and snippets.

@pythonmcpi
Last active September 16, 2025 17:47
Show Gist options
  • Select an option

  • Save pythonmcpi/83b95f6e86a8155c07d4ff924967b325 to your computer and use it in GitHub Desktop.

Select an option

Save pythonmcpi/83b95f6e86a8155c07d4ff924967b325 to your computer and use it in GitHub Desktop.
A quick example of using all the new v2 components with discord.py
# To run this example directly, you'll need to have an image file named `discordpy_logo.png` in the same directory.
import discord
from discord import app_commands
import io
DISCORDPY_LOGO_URL = "https://cdn.discordapp.com/icons/336642139381301249/3aa641b21acded468308a37eef43d7b3.png?size=256"
class ExampleButton(discord.ui.Button):
"""An example button that displays its own label."""
async def callback(self, i: discord.Interaction):
await i.response.send_message(self.label, ephemeral=True)
class ExampleContainerA(discord.ui.Container):
text = discord.ui.TextDisplay("This is a container. It can have text displays, sections, action rows (buttons and selects), separators, galleries, and files.")
class ExampleViewA(discord.ui.LayoutView):
text1 = discord.ui.TextDisplay("This is a text display. Regular markdown works here.\n**bold** *italic* __underline__ ~~strikethrough~~ ||spoiler|| `code`\n```py\nprint('Hello, Discord!')\n```\n-# headings work too")
separator = discord.ui.Separator(
spacing = discord.SeparatorSpacing.large, # can be small or large
visible = True, # whether to visually display a line
)
section1 = discord.ui.Section(
"This is a section. It has 1-3 text displays on the left, and either a button or a thumbnail on the right.",
accessory = discord.ui.Thumbnail(DISCORDPY_LOGO_URL)
)
section2 = discord.ui.Section(
accessory = ExampleButton(label = "Click me! (section accessory)")
).add_item("Example of a button accessory")
text2 = discord.ui.TextDisplay("### Media Galleries and Files\nMedia gallery items and section thumbnails support loading media from any url, or from uploaded attachments using the `attachment://filename` uri. Both support descriptions (alt text) and spoiler-marking.")
gallery = discord.ui.MediaGallery(
discord.MediaGalleryItem(DISCORDPY_LOGO_URL, description = "discord.py logo"),
discord.MediaGalleryItem("attachment://discordpy_logo.png", spoiler = True),
)
file1 = discord.ui.File("attachment://discordpy_logo.png", spoiler = True)
file2 = discord.ui.File("attachment://example.txt")
text3 = discord.ui.TextDisplay("File components can only refer to uploaded attachments. These can be spoilered, but cannot have descriptions, and do not display any previews.\nButtons and selects still work, but must be explicitly assigned to an ActionRow in code.")
row1 = discord.ui.ActionRow().add_item(ExampleButton(label = "Button 1"))
@row1.button(label = "Button 2")
async def button2(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message("Button 2", ephemeral = True)
row2 = discord.ui.ActionRow()
@row2.select(placeholder = "Select an option...", options = [
discord.SelectOption(label = "Option A", value = "option a"),
discord.SelectOption(label = "Option B", value = "option b"),
])
async def select(self, interaction: discord.Interaction, select: discord.ui.Select):
await interaction.response.send_message(f"You picked {select.values[0]}", ephemeral = True)
containera = ExampleContainerA()
containerb = discord.ui.Container(accent_color = discord.Color.og_blurple()).add_item(
discord.ui.TextDisplay("Containers can have accent colors and can be marked as spoilers.")
)
# there is also a new id= attribute on all components as well as a find_item function, but usage is not demonstrated here
@app_commands.command(name="example")
async def example(i: discord.Interaction):
view = ExampleViewA()
file1 = discord.File("discordpy_logo.png")
file2 = discord.File(io.BytesIO(b"example text (no preview shown in client)"), filename = "example.txt")
await i.response.send_message(view=view, files=[file1, file2])
@iancheung0202
Copy link

How would I install it to my bot right now?

@sawofs
Copy link

sawofs commented Sep 16, 2025

How would I install it to my bot right now?

Copy paste

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment