Skip to content

Who Can Click

A view decides who may use its controls before any callback runs. By default that is only the person who opened it. This page covers opening a panel to more people, refusing someone in your own words, and participants: the people a view counts as taking part in it.

Who May Click

Each click is checked against three rules, in order:

  1. allowed_users is set. Only the people in it may click, and owner_only is not read.
  2. owner_only is True, the default. Only the view's owner may click.
  3. Otherwise anyone may click.

The owner is the person whose command or click sent the view, kept as view.user_id. Someone refused is told "You cannot interact with this." in a reply only they see, and the callback does not run.

This challenge can be answered by the two players and no one else:

import discord

from cascadeui import StatefulButton, StatefulLayoutView, card


class ChallengeView(StatefulLayoutView):
    unauthorized_message = "This match is between two other players."

    def __init__(self, *, opponent: discord.Member, **kwargs):
        super().__init__(**kwargs)
        self.allowed_users = {self.user_id, opponent}  # (1)!
        self.add_item(card(f"<@{self.user_id}> challenged {opponent.mention}."))
        self.add_item(
            discord.ui.ActionRow(StatefulButton(label="Forfeit", callback=self.forfeit))
        )

    async def forfeit(self, interaction):
        await self.respond(interaction, f"{interaction.user.mention} forfeits.", ephemeral=False)
        await self.exit()
  1. A Member is turned into its id, so the set can mix members and ids.

Ren, who is not in allowed_users, clicks Forfeit and is refused in a reply only Ren sees: the callback does not run and the panel is not edited. Mira, who is in the list, clicks Forfeit: the callback runs and the view closes with its button disabled

StatefulView, StatefulLayoutView, and the patterns built on them (paginated, tabs, wizard, form, menu, leaderboard) default to owner_only = True. The panels made for a whole channel default to False: PersistentView, PersistentLayoutView, PersistentLeaderboardLayoutView, the roles panels, and DisplayLayoutView.

Opening a Panel to Everyone

A poll, a lobby, or a scoreboard anyone may page through turns owner_only off:

class PollView(StatefulLayoutView):
    owner_only = False

Listing Who May Click

allowed_users is a set of Discord ids, or of anything carrying one, such as a Member. Set it in __init__ after super().__init__(), or assign a new set later, when someone joins. Each click is checked against the set as it is then.

The list replaces owner_only completely, so it must name the owner for the owner to click. An empty set admits no one, the owner included. None, the default, turns the list off and leaves the choice to owner_only.

One Control for the Owner Alone

On a panel open to everyone, owner_only=True on a single button or select keeps that control for the owner, such as a lobby's Start button. Every library button and select takes it, the role, channel, user, and mentionable selects included, and so do action_section() and a RowButton entry in button_row() or tab_nav():

StatefulButton(label="Start", callback=self.start, owner_only=True)
button_row([RowButton("Start", self.start, owner_only=True), RowButton("Leave", self.leave)])

Only the owner passes. allowed_users is not read here, so the other players are refused too. A refused click gets the view's refusal, as below.

Changing the Refusal

unauthorized_message sets the words. on_unauthorized() replaces the reply, for a mention or an embed:

class GameView(StatefulLayoutView):
    async def on_unauthorized(self, interaction):
        await self.respond(
            interaction, f"{interaction.user.mention}, this game is already full.", ephemeral=True
        )

The click is already refused when on_unauthorized() runs, so it decides only what the person is told.

Your Own Rule

For a rule of your own, such as a role, override interaction_check() and keep the built-in checks with super():

class AdminView(StatefulLayoutView):
    async def interaction_check(self, interaction):
        if not await super().interaction_check(interaction):
            return False
        if not interaction.user.guild_permissions.administrator:
            await self.respond(interaction, "Admins only.", ephemeral=True)
            return False
        return True

The click's 3-second limit is already running when interaction_check() runs: the library sets its acknowledgement going before the checks. A check that waits on something, such as a role lookup that misses the cache or a database read, spends that time, and past auto_defer_delay (2.5 seconds) the library acknowledges the click while the check waits. Reply through self.respond(), which sends a follow-up once the click is acknowledged, where interaction.response.send_message() raises InteractionResponded. See Responding to Clicks.

Participants

A participant is someone the view counts as taking part beside its owner: an opponent, a player in a lobby. Registering one does three things:

  • Counts them toward participant_limit, the most people the view holds, its owner included.
  • Counts them toward the class's instance_limit, as the owner is counted: with instance_limit = 1, a player in one game can neither open a second nor join another. See How Many Can Exist.
  • Lists them in view.participants, a frozenset of ids.

It does not let them click. Who may click is still the rules above, so a view with participants either lists them in allowed_users or turns owner_only off.

This lobby takes up to eight players, and only the host can start it:

import discord

from cascadeui import StatefulButton, StatefulLayoutView, card


class LobbyView(StatefulLayoutView):
    owner_only = False  # (1)!
    participant_limit = 8
    participant_limit_message = "This lobby is full."

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.build_ui()

    def build_ui(self):
        self.clear_items()
        players = ", ".join(f"<@{uid}>" for uid in sorted(self.participants)) or "nobody yet"
        self.add_item(card(f"## Lobby\nHost: <@{self.user_id}>\nPlayers: {players}"))
        self.add_item(
            discord.ui.ActionRow(
                StatefulButton(label="Join", callback=self.join),
                StatefulButton(label="Leave", callback=self.leave),
                StatefulButton(label="Start", callback=self.start, owner_only=True),
            )
        )

    async def join(self, interaction):
        if await self.register_participant(interaction.user, interaction=interaction):  # (2)!
            self.build_ui()
            await self.refresh()

    async def leave(self, interaction):
        self.unregister_participant(interaction.user)
        self.build_ui()
        await self.refresh()

    async def start(self, interaction):
        await self.respond(interaction, "Starting.", ephemeral=False)
  1. Anyone may click Join. Start keeps its own check.
  2. register_participant() comes with every view. It returns False when the lobby is full or the player is in another game, and the library has already told them.

register_participant() returns True for the owner without adding them, since the owner is counted already. Pass interaction= so a refusal goes to the person joining. Without it, a full view sends nothing, and a refusal for the instance limit goes to the view's own interaction, which the owner sees.

Capping the Number of Players

participant_limit counts the owner, so participant_limit = 8 leaves room for seven more. A join past it returns False and the joiner is told "This session is full." (participant_limit_message). on_participant_limit(user_id, interaction=None) replaces that reply, for one that names the joiner or offers a waitlist.

A Fixed Roster

When the players are known before the panel goes out, auto_register_participants = True registers everyone in allowed_users but the owner as the view is sent:

class DuelView(StatefulLayoutView):
    participant_limit = 2
    auto_register_participants = True

    def __init__(self, *, opponent, **kwargs):
        super().__init__(**kwargs)
        self.allowed_users = {self.user_id, opponent}
        self.add_item(card("Duel!"))

All of them are registered or none is. When one is refused, because they are in another game or the view is full, the ones already registered are released and nothing is posted: send() returns None, and the person who ran the command is told why.

Choosing a Shape

Panel owner_only allowed_users Participants
One person's panel True (default) None none
A match between known players not read the players, owner included auto_register_participants = True
An open lobby with a cap False None a Join button and participant_limit
A panel for the whole channel False None none
Less common cases

A view with no owner. A view sent without an interaction or a command context, and given no user_id=, has no owner, so owner_only, on the view or on a control, admits everyone. allowed_users still applies. A persistent panel restored after a restart keeps the owner it was sent with.

Setting the owner. user_id= passed to the constructor names the owner, as does view.user_id assigned later. Both take a member for its id. It must be a Discord id: an id of your own there becomes the owner, and under owner_only no one can click. See Building and Sending.

Leaving. unregister_participant() takes what register_participant() takes, so interaction.user works in both. A participant leaves the instance count at once, and every participant leaves it when the view closes.