Skip to content

Ephemeral Views

An ephemeral panel is one only the person who opened it can see. Discord lets a bot change one only through an interaction, and each interaction works for 15 minutes. This page covers how the library keeps a panel in use reachable, what it does before its last interaction runs out, and how to set it up.

Sending an Ephemeral Panel

Pass ephemeral=True to send():

import discord

from cascadeui import StatefulLayoutView, card


class SettingsView(StatefulLayoutView):
    timeout = None  # (1)!

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_item(card("## Settings", "Only you can see this panel."))
        self.add_item(discord.ui.ActionRow(self.make_exit_button()))


@bot.tree.command(description="Open your settings")
async def settings(interaction: discord.Interaction):
    await SettingsView(interaction=interaction).send(ephemeral=True)
  1. No timeout, so the panel stays open however long it goes unused. When nobody has clicked it for 13.5 minutes, the library shows a Continue button, described below.

  2. Only the person who ran the command sees the panel, and only they can click it.

  3. They can dismiss it, which hides it for them. Discord does not tell the bot, so the view stays open until it times out or closes.
  4. A prefix command has no interaction to reply through, so discord.py posts its panel publicly, and the view is a public panel. Slash commands, and hybrid commands invoked as one, send ephemeral panels.
  5. A persistent view cannot be ephemeral, since nothing can reach an ephemeral message after a restart: send(ephemeral=True) raises ValueError.

The 15-Minute Limit

Only an interaction reaches an ephemeral message: the command whose reply sent it, or a later click on it that the view answered by changing the panel. Discord keeps each one usable for 15 minutes, and the library edits through the newest, so every such click gives the panel another 15 minutes. A click answered with a reply instead, such as respond(), gives it nothing: that click's interaction belongs to the reply.

Sending the view again past its command's 15 minutes posts through the click on the panel being answered, or else the newest interaction that reaches the panel. Discord shows a private panel only to the person whose interaction posts it, so a private send takes only an interaction of the person who ran the command. When none of theirs reaches the panel, the send raises RuntimeError rather than going out publicly, which is where discord.py's Context.send would post it.

Once the newest interaction is 15 minutes old:

  • A click still works. Each click brings a new interaction, so the view's answer to a click still redraws the panel.
  • Other changes stop showing. A redraw that is not the answer to a click on the panel (a change made by another person, a timer, a background task) does not reach the panel. The library logs it at DEBUG and goes on.
  • A close outside a click cannot change the panel. The view still closes, but its buttons are not disabled and the message is not deleted. A click on them afterwards fails with "didn't respond in time". A close made while answering a click goes through that click, so it still lands.

The library acts 90 seconds before the newest interaction runs out, and what it does depends on the view's timeout:

timeout Continue button 90 seconds before the limit
900 seconds or less (180 by default) Off The view times out, unless its own timeout came first, so the close still disables its buttons.
More than 900 seconds, or None On A Continue button replaces the panel.

A panel with the button off times out early only when its own timeout would come after the limit: a timeout between 13.5 and 15 minutes, or clicks answered with replies, which restart the timeout but give the panel no new 15 minutes.

The Continue Button

Ninety seconds before the newest interaction runs out, a Continue Session button replaces the panel's components. On a StatefulLayoutView the button is all the message shows, and a StatefulView keeps its embed above it, since only its components are replaced. A panel in use never shows it: every click answered by changing the panel moves the 15 minutes on, and the button waits for a click still being handled.

Clicking it brings the panel back on the same message: the view runs on_load() and draws itself from its state, and that redraw answers the click. The click is a new interaction, so the panel has 15 minutes again, and the button can come back 13.5 minutes later, so a panel someone keeps continuing never ends. The button works however late it is clicked, since its own click carries the redraw.

Kael's Settings panel, sent so only Kael sees it. 13.5 minutes after Kael's last click, a Continue Session button replaces the panel. Kael clicks it: the same panel comes back on the same message and has 15 minutes again

While the button shows, state changes do not redraw the panel; the redraw at the Continue shows them. A refresh() from your own code (from a modal submitted late, for example) keeps the button and leaves out what that code rebuilt. Sending the same view again posts its own panel on the new message, not the button.

What the Panel Keeps

The Continue resumes the view itself, so everything stays as it was: its attributes, navigation stack, session and session data, undo history, participants, attached views, and any reference your code kept. Load what may have changed while the button showed in on_load(), which runs at every Continue as it does at the send and on every push or pop:

class GameView(StatefulLayoutView):
    timeout = None

    def __init__(self, *, game_id, **kwargs):
        super().__init__(**kwargs)
        self.game_id = game_id

    async def on_load(self):
        self.game = await load_game(self.game_id)  # (1)!
        if self.game.finished:
            await self.exit()  # (2)!

    def build_ui(self):
        self.clear_items()
        self.add_item(card(f"## Game {self.game_id}", f"Turn {self.game.turn}"))
  1. Your own lookup, read at the send and at every Continue.
  2. exit() ends the panel instead of bringing it back. At a Continue the close goes through the click, so the message is frozen or deleted by exit_policy.

When the Continue Fails

on_reopen_failure(interaction, error) runs when the panel does not come back:

  • on_load() or the redraw raised, or Discord refused the edit: error is the exception, and the log holds it. A raise after the redraw reached the message is not a failed Continue, since the panel is back: it goes to on_error() like any click's.
  • No interaction reaches the message (the click's own answer failed, and the older ones have run out): error is None.

Either way the Continue button stays, so the person can click it again. The default replies privately with reopen_failure_message. Change the text with the class attribute, or override the hook:

class GameView(StatefulLayoutView):
    async def on_reopen_failure(self, interaction, error=None):
        text = MESSAGES[interaction.locale]["continue_failed"]  # (1)!
        await self.respond(interaction, text, ephemeral=True)
  1. Your own translations.

A Continue With No Panel Behind It

The button can outlast its view. A restart ends every view, and a view that times out after its last interaction ran out cannot reach the message to close it. A click on the button then finds no view, and the library answers it: the button turns grey, and the person gets session_ended_message privately, read from the view's class (None sends nothing). A class that overrides on_session_ended() answers people its own way, and no view is left to run it on, so its button only turns grey, as a with_confirmation prompt does for such a class.

The library starts answering these clicks for a bot at its first send, the first modal it opens, or when persistence is set up with bot=, so after a restart a click is answered once one of those has happened. The button's custom_id names the view's class, which the library finds once the class's module has been imported; before that, the click gets the library's default line. A Continue given its own custom_id in build_refresh_button() is left to the bot.

Changing the Button

Class attribute Default What it sets
refresh_warning_seconds 90 How many whole seconds before the newest interaction runs out the button appears, or a view without it times out (an int of 1 or more)
refresh_button_label "Continue Session" The button's label
refresh_button_emoji 🔄 The button's emoji
refresh_button_style ButtonStyle.primary The button's style
reopen_failure_message "Could not refresh this view. Try again, or reopen it from the original command." The private reply when the panel does not come back. None sends nothing

An emoji Discord refuses is dropped: the button goes out with its label alone, and a warning is logged. For a custom ID or a row of its own, override build_refresh_button(), starting from super().build_refresh_button() so the button still runs the Continue.

Turning It On or Off

auto_refresh_ephemeral decides it instead of the timeout: True turns the button on, and False turns it off. None, the default, reads the timeout as in the table above. The view's own timeout still ends it: each click the panel answers starts its 15 minutes again, so with a timeout under 13.5 minutes the button appears only when something other than such a click keeps the view open, such as a render another view's change triggers.

class ScoreCard(StatefulLayoutView):
    timeout = None
    auto_refresh_ephemeral = False  # (1)!
  1. A card nobody needs after 15 minutes. discord.py gives an ephemeral view declared timeout=None 900 seconds, and the view times out 90 seconds before its newest interaction runs out, so this one closes 13.5 minutes after it was sent or last clicked.

examples/v2_battleship.py turns it on for each player's private fleet panel, which lives as long as the game.

Across Navigation

push() and pop() hand the panel's newest interaction to the screen they show, so the 15 minutes count from the send or the latest click on any screen of the panel.

Each screen follows its own auto_refresh_ephemeral. A screen left at None takes the setting of the screen it was pushed from, and Back returns a screen to the setting it had. A screen's own timeout is not read for this.

A Panel the Person Dismissed

Discord does not tell the bot when someone dismisses an ephemeral panel, so the library cannot tell a dismissed panel from one the person will come back to. A panel with timeout = None and the Continue button on stays open until its exit() or a restart.

On a class with an instance limit that refuses new panels (instance_policy = "reject", or a panel kept for its players), the person cannot open another until then. Give such a panel a long timeout instead of none, such as 86400 (a day).

Less common cases

A second click on Continue after the panel came back, from a screen still showing the button, is answered with the panel.

The timeout of a view with the button on reads None after the send. discord.py sets an ephemeral view declared timeout=None to 900 seconds, and the library puts None back so the panel does not close while its Continue button shows. A send that fails leaves the timeout as it was.

A public panel the library could not read back through its channel (a user-installed app reads no channel) is reached only through interactions too, and is edited through its newest one the same way.

Patterns. The rules live in send(), so every pattern sent with ephemeral=True follows them.