Skip to content

Class Settings

A view's behavior is set by class attributes, such as instance_limit or exit_policy, and The Five Pillars lists them all. This page covers the checks they get when the class is defined, and changing one for a single view.

Mistakes Caught Early

A view's class attributes are checked when the class is defined, so a typo fails when the module is imported, not when someone clicks:

from cascadeui import StatefulLayoutView


class LobbyView(StatefulLayoutView):
    instance_policy = "rejct"  # ValueError when the module is imported

A value of the wrong type raises TypeError, and a bool counts as the wrong type where a number is expected (instance_limit = True). A value of the right type that is out of range or not one of the options raises ValueError. The message names the class, the attribute, the value, and what it accepts, and Errors lists every case.

Discord IDs are converted instead of refused. user_id, guild_id, allowed_users, register_participant(), unregister_participant(), and the user_id= and guild_id= of the scoped-state calls take an int or anything with an integer .id, such as a discord.Member or a discord.Object:

import discord

# ... with view a view built in a command, and opponent another member
view.allowed_users = {member, 12345, discord.Object(id=99999)}
await view.register_participant(opponent)

Changing a Setting for One View

To change a class attribute for a single view, call set_class_attribute():

view = CounterView(interaction=interaction)  # ... any view class of your own
view.set_class_attribute("instance_limit", 5)
await view.send()

The value is checked as it would be on the class. The change applies from the view's next render, so you can, for example, relabel a pattern's buttons for one person's language before it is sent:

from cascadeui import PaginatedLayoutView

pages = await PaginatedLayoutView.from_data(
    items, per_page=5, formatter=format_page, interaction=interaction
)
pages.set_class_attribute("next_button_label", "Suivant")
await pages.send()

It raises ValueError for a name the class does not have, for methods and properties other than timeout and nav_rebuild, for the view's own data (user_id, guild_id, allowed_users), and for the few attributes the library reads from the class itself, such as session_continuity. Set those on the class.

The change stays with the panel through pop(), which builds the view again, and an ephemeral panel's Continue button, which brings back the view itself. A restart does not keep it, since a restored panel is rebuilt from its constructor arguments alone. To keep a setting across restarts, take it as an argument and set it in __init__:

from cascadeui import PersistentLayoutView


class LobbyView(PersistentLayoutView):
    def __init__(self, *, players: int = 4, **kwargs):
        super().__init__(**kwargs)
        self.set_class_attribute("participant_limit", players)