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.
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:
- They only render. No callback, no state, and nothing for the store to hold.
- Discord draws them. discord.py carries their data, and a subclass would add nothing to it.
- 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 fourSectionsubclasses 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.
Related¶
- Layout Builders: the builders at work
- Wrappers, Emoji, and V1 Composites: the three wrappers
- Modals and Inputs: the inputs and their validators
- Code Map: the module that holds each strategy