discord-interactions.py

Components V2

Containers, sections, thumbnails, galleries, files and separators.

Components V2 replace embeds: a card (container) holds Markdown text, images, files and buttons, in the order you choose.

A card in a few lines#

from discord_interactions import container, create_button, section card = (    container(accent_color=0x5865F2)    .add("## Minecraft server")    .add(section("**Online**", "12 players connected").thumbnail("https://example.com/logo.png"))    .separator()    .row(create_button("join", "Join", style="success")))await ctx.respond(components=[card])

A string passed to add() becomes a text block. build() is optional: ctx.respond and every sending function accept the builder directly.

Building blocks#

Builder Role
container() The card, with border color and spoiler
textDisplay() or a string Markdown text, 4000 characters in total per message
section() 1 to 3 texts with a button or a thumbnail on the right
thumbnail() Square image used as a section accessory
mediaGallery() 1 to 10 images or videos
fileComponent() Attached file shown inside the card
separator() Divider and spacing, small or large
.row() Row of buttons or a select menu

Images and files#

import discordfrom discord_interactions import container, mediaGallery file = discord.File("report.pdf")card = container("## Monthly report").add(mediaGallery("https://example.com/chart.png")).file("report.pdf")await ctx.respond(components=[card], files=[file])

A file sent with files= is referenced as attachment://name. .file("report.pdf") adds that prefix for you.

Separators#

container("Title").separator().add("Thin divider")container("Title").separator(spacing="large").add("Large gap")container("Title").separator(divider=False).add("Space without a line")

discord.js style#

The camelCase names from discord.js work too, if you come from JavaScript:

from discord_interactions import ContainerBuilder, textDisplay card = (    ContainerBuilder()    .setAccentColor(0x57F287)    .addTextDisplayComponents(textDisplay("Hello"))    .addSeparatorComponents()    .build())

Prefer the short form in new code: it reads better and produces the same output.

Type a name: create_button, ModalBuilder, cooldown.