Skip to content

Extension Strategies

The library adds to discord.py in five ways, and each new part of it takes the one its job calls for. Three Kinds of Component sorts components by what Discord does with them. This page sorts them by how the library builds them.

Diagram: what one builder returns. action_section("Put every setting back.", label="Reset", callback=reset) returns discord.py's own Section. Inside it are discord.py's TextDisplay, holding the text, and CascadeUI's StatefulButton, the Reset button, a subclass of discord.py's Button. Beside them, the panel Discord draws: the text on the left, Reset on the right.

Five Strategies

Strategy What it is Examples When it fits
Subclass A discord.ui class with behavior added to the type StatefulButton, StatefulSelect, Modal, StatefulView, StatefulLayoutView, PersistentView, PersistentLayoutView The behavior belongs to the type: dispatching a click, binding state, a custom_id contract, lifecycle hooks
Builder A function that returns discord.py's own layout components, set up for one job card(), action_section(), image_section(), link_section(), confirm_section(), stats_card(), progress_bar(), button_row(), tab_nav(), button_grid(), emoji_grid() A component needs a shorter call and safe defaults, and no new type
Wrapper A function that wraps a button's callback in behavior of its own with_loading_state(), with_confirmation(), with_cooldown() The behavior applies to any button
Pattern A class that groups components for one recognizable problem and keeps their settings and state between renders ConfirmationButtons, PaginationControls, ToggleGroup, ProgressBar (V1), PaginatedRegion, Collapsible The problem comes up often enough to deserve a name
Input A class that holds a modal field's settings and validators, and builds discord.py's input when the modal opens TextInput, Checkbox, CheckboxGroup, RadioGroup, FileUpload A modal field needs validators the modal runs

The strategies combine, as the drawing shows. A builder returns discord.py's layout, and the controls it makes for you are the library's own (StatefulButton or StatefulSelect), so their clicks dispatch. A link button sends no click and stays discord.py's, and button_grid() places the buttons your function makes, whatever their class.

Each input builds two Discord components when the modal opens: a Label holding discord.py's input, which a single subclass cannot be. The Modal the input joins runs its validators when the person submits. Code Map says which module holds each strategy.

What CascadeUI Deliberately Does Not Subclass

The layout components stay discord.py's own: Container, Section, Separator, MediaGallery, Thumbnail, ActionRow, File, TextDisplay, and the modal's Label. The one exception is emoji_grid()'s EmojiGrid, a TextDisplay that rewrites its text as its cells change. They stay plain because:

  1. They only render. No callback, no state, and nothing for the store to hold.
  2. Discord draws them. discord.py carries their data, and a subclass would add nothing to it.
  3. Builders scale better. The four builders around Section (action_section(), toggle_section(), image_section(), link_section()) are functions anyone can find in a module listing, where four Section subclasses would hide in an inheritance tree.

What Each Costs

A subclass or an input costs the most, since its type stays on the public surface. A builder adds a function, a wrapper adds behavior without a type, and a pattern costs what its problem needs.

When a Builder Earns Its Place

A new builder needs one of these:

  • Used three times. The same composition appears in three examples or pattern files. Two copies can match by chance, and a builder, once shipped, is hard to take back.
  • Prevents a refusal. It rules out a shape Discord refuses at the send.
  • Always together. Its parts never appear apart.

Where an existing builder covers the shape with a keyword or two, extend it instead.