Choosing an Approach¶
A bot keeps its state in memory, so a restart starts it empty: posted panels stop answering their buttons, and every value is back to its default. Three things can be made to come back, each set up its own way:
| What should come back | How | Page |
|---|---|---|
| A value people change, such as Kael's language or each player's wins | Mark the slot that holds it as saved. It is written to the database as it changes and read back at startup. | Saving Data |
| A panel posted once that has to keep working, such as a ticket desk or a role picker | Build it as a PersistentLayoutView with a persistence_key. Its message is recorded, and at startup the panel is built again and attached to it. |
Panels That Survive Restarts |
A button that carries what its click needs in its custom_id, such as Close on each ticket |
Make it a DynamicPersistentButton. Nothing is stored for it: its clicks are routed again at startup, and the id says which record they are for. |
Dynamic Persistent Buttons |
All three need persistence set up with PersistenceMiddleware (Setup and Shutdown). Nothing is
saved until you choose it.
What a Restart Keeps¶
The State Tree lists the six keys of store.state and what a
restart leaves of each. With persistence set up:
views,sessions,components, andmodalsstart empty, and session data goes with them. A persistent panel that comes back registers again, in the session it was in, with that session's data empty.applicationgets back the slots you save, as they were last written, and no others. Scoped state is thescopedslot ofapplication, so it is lost at a restart until you save it like any other slot.persistent_viewsis read back from the registry, the table that keeps one row per persistent panel: its message, its owner, and the arguments it was built with.
A persistent panel comes back built from those arguments. A value it changed after it was posted comes back only when the panel keeps it in a saved slot.
Which to Use¶
| You want | Use |
|---|---|
| A panel someone opens with a command and closes when done | A view with nothing saved. After a restart, they run the command again. |
| That panel to open again with the values it had | A view whose values live in saved slots |
| A panel that stays posted and keeps working | A persistent panel |
| A posted panel showing values that change | A persistent panel whose values live in saved slots |
| One kind of button on many messages, each for its own record | A DynamicPersistentButton |
A persistent panel's persistence_key names its row, so the same panel needs the same key on every start. A
saved value needs a key that stays the same too. Scoped state is keyed by the person's or the server's id
already. A slot you key by self.persistence_key needs a key passed as persistence_key= or set in the class
body, since without one the property returns the view's id, which is new each time a view is built.
Data Only, or Panels Too¶
PersistenceMiddleware takes the bot as bot=. With it, the slots you save come back, persistent panels are
attached to their messages again, and dynamic buttons route their clicks. Without it, only the slots come back:
from cascadeui import PersistenceMiddleware, SQLiteBackend, setup_middleware
# Saved slots come back; persistent panels and dynamic buttons do not.
await setup_middleware(PersistenceMiddleware(backend=SQLiteBackend("cascadeui.db")))
A persistent panel sent without bot= still writes its row, and the rows stay on disk, so a later start with
bot= attaches the panels again. At a start without bot=, the middleware logs a WARNING naming the persistent
panel and dynamic button classes that will not come back.