Responding to Clicks¶
Discord expects an answer to every click, pick, and submit within 3 seconds. CascadeUI answers for you, so a callback only changes data and redraws. This page covers how that works, the three helpers for callbacks that need to answer themselves, and how double clicks are handled.
The Usual Callback¶
A callback that changes the view and calls refresh() needs nothing more:
import discord
async def increment(self, interaction: discord.Interaction):
self.count += 1
self.build_ui()
await self.refresh()
CascadeUI makes sure the click is answered, whichever way the callback goes:
- It redraws.
refresh()edits the message as the answer to the click, so one request does both. A callback that dispatches an action instead is answered the same way, since the view redraws as part of the dispatch. - It finishes without answering. The click is acknowledged as soon as the callback returns. This covers a callback that changed nothing on screen, or edited some other message.
- It is still running after 2.5 seconds. The click is acknowledged then, before Discord's 3 seconds run
out, and the callback carries on. Its
refresh()edits the message in a request of its own.
Do not call interaction.response.defer()
A defer takes the click's answer, so refresh() has to edit the message in a second request. And a
click that waited its turn behind a slow one may already be acknowledged when its callback starts, and a
defer() then raises discord.InteractionResponded and ends the callback before it redraws. To
acknowledge early, use safe_defer().
Replying: respond()¶
To say something to the person who clicked, call respond() instead of
interaction.response.send_message():
async def claim(self, interaction: discord.Interaction):
if self.claimed_by is not None:
await self.respond(interaction, "Already claimed.", ephemeral=True)
return
self.claimed_by = interaction.user.id
self.build_ui()
await self.refresh()
respond() sends the reply as the click's answer when it can, and as a followup when the click was already
acknowledged, so it works at any point in a callback. It takes the keywords send_message() takes, such as
embed= and file=, and returns the message it posted. Keep that message to edit or delete the reply later.
Opening a Modal: open_modal()¶
Open modals with open_modal(), and handle the submission in the modal's
own callback:
from cascadeui import Modal, TextInput
async def rename(self, interaction: discord.Interaction):
field = TextInput("Name", max_length=32)
async def on_submit(interaction: discord.Interaction, values):
self.name = field.value
self.build_ui()
await self.refresh()
modal = Modal(title="Rename", inputs=[field], callback=on_submit)
await self.open_modal(interaction, modal)
A modal has to be the first answer to a click: Discord refuses one after the click was acknowledged. When
that has already happened, open_modal() sends the person an ephemeral "Could not open the dialog. Please
try again." instead, and returns False. Pass fallback_message= to change the text.
The submission is an interaction of its own, with its own 3 seconds, and it is answered the way a click is:
the refresh() in on_submit answers it.
Do not wait for the modal inside the click's callback. Discord reports nothing when the person closes a modal without submitting it, so a callback waiting for the submission never ends, and every later click on the view waits behind it.
Slow Work: safe_defer()¶
When a callback's work regularly takes more than a second or so, such as a slow database query or a call to
another service, acknowledge the click at once with safe_defer():
async def reload_scores(self, interaction: discord.Interaction):
await self.safe_defer(interaction)
self.scores = await fetch_scores()
self.build_ui()
await self.refresh()
Without it, the click is still acknowledged at 2.5 seconds, and safe_defer() only answers sooner. The cost is
the one-request edit: after the acknowledgement, refresh() edits the message in a request of its own.
safe_defer() does nothing when the click was already acknowledged.
Clicks Take Turns¶
Clicks on one view run one at a time, in the order they arrived, so two quick clicks never redraw the panel over each other. A click waiting its turn is still answered in time: the 2.5-second timer runs while it waits. When its turn comes, the click is judged against the panel as it is then: if a click ahead of it drew the panel again with this control disabled, it runs nothing and is answered with the panel.
A click that waited longer than that is acknowledged before its callback starts. Its refresh() then edits
the message in a request of its own, and open_modal() sends its fallback message instead of the modal.
A callback that opens a Modal and awaits its
wait() gives up its turn while it waits, so other clicks on the view run, and
takes the turn back when the modal ends. Discord sends nothing when someone closes the dialog, so that wait() never
returns, and holding the turn would leave the panel dead for everyone. A plain discord.ui.Modal's wait() keeps
the turn: give it a timeout, or handle the submission in its callback instead of awaiting it.
Double Clicks¶
When someone clicks a button a second time while their first click on it is still running, CascadeUI drops the second click and answers it with the panel as it is now. A slow Save clicked twice saves once, and an Approve button on a screen that moves to the next item cannot approve an item the person never saw.
The rule is narrow:
- Only two clicks in a row, by one person, on one button, count. Another person's click runs, and so does a click sent after the person clicked something else in between, such as fixing a field before submitting again.
- Once the first click has shown the person something (a reply, an edit, or a modal), their next click is a new request.
- A pick in a select always runs, since it carries its own values.
For a button where every click counts, such as a counter, pass allow_double_click=True:
from cascadeui import StatefulButton
StatefulButton(label="+1", callback=self.increment, allow_double_click=True)
action_section() and
button_row() take the same keyword, and a plain
discord.ui.Button can set button.allow_double_click = True. The library's own previous and next page
buttons set it.
More on double clicks
What counts as showing the person something. The library sees the replies and edits made through
it: respond(), open_modal(), a view's send(), and refresh(). A followup sent with
interaction.followup.send() is not seen, so until the callback finishes, the person's next click is
still dropped. Reply through respond().
Controls that hold one state for everyone. A toggle, a cycle button, a Collapsible trigger, an
option in a multi-select choice_row(), and a confirm prompt's two buttons follow a stricter rule: a
click sent before the previous click on that control finished is dropped, whoever sent it, since it
would flip what that click already flipped. ToggleButton(allow_double_click=True) turns this off. The
builders (toggle_button(), cycle_button(), toggle_section(), choice_row(), confirm_section(),
and Collapsible) do not take the keyword.
Dynamic persistent buttons. A DynamicPersistentButton follows the per-person rule. Set
allow_double_click = True on its class to run every click.
Changing the Defaults¶
Four class attributes control all of the above:
auto_defer(defaultTrue): acknowledges a click the callback left unanswered, when the callback returns or afterauto_defer_delay, whichever comes first.auto_defer_delay(default2.5): seconds a callback may run before its click is acknowledged. It must be under3.0, and a larger value raisesValueErrorwhen the class is defined.serialize_interactions(defaultTrue): runs clicks on one view one at a time.ack_first(defaultFalse): acknowledges every click before the access check and the callback.
auto_defer = False hands the 3 seconds to your callback. The library still acknowledges a click while
it does its own work (the access check, a navigation in progress, waiting for its turn), but it stops before
your callback starts and does not acknowledge after it. A callback that leaves the click unanswered then
makes Discord show "didn't respond in time".
If the library's own work ran past auto_defer_delay, the callback finds the click already acknowledged.
Clicks the library handles itself, such as its page buttons, its Exit button, and dropped clicks, are still
answered.
ack_first = True is for a callback that blocks the event loop, such as heavy computation between
awaits, which would also hold up the 2.5-second timer. Every click is acknowledged before anything else
runs. In exchange, refresh() always takes a second request, and open_modal() always sends its fallback
message, since the click is acknowledged before the modal could be its answer.
serialize_interactions = False lets clicks on one view run at the same time. Use it only where clicks
touch separate data, such as a read-only display. A view whose clicks change shared data, such as a game
board, keeps the default. Without it, a dropped click is acknowledged without the current panel.
Less common cases
Someone the view does not accept. They are told "You cannot interact with this."
(unauthorized_message) and the callback does not run. See Who Can Click.
A click on a closed or changed panel. A click on a view that has closed, or on a control the view has since disabled or removed, is acknowledged and its callback does not run. When the panel on the person's screen is older than the view's, the view redraws it.
A callback that answers nothing. When the callback leaves
a click, pick, or submission unanswered and the library would acknowledge it, the library answers with the
view's current render instead whenever a render was skipped, or a select pick on the view's own message has
not been redrawn, so the screen catches up with no extra request. When the library acknowledged the click
first (ack_first, or a callback slower than auto_defer_delay), a toggle's new label or a change the
callback made for a pick is drawn with one edit of the message, the edit the callback's own refresh()
would make. That edit follows a raise too, unless the raise cut a rebuild short, and a click that changed
nothing spends no edit.
A callback that keeps a pick for a later step draws it, marking the option default and calling
refresh(). When the answer (or the render a reply through respond() sends first) shows the select
still offering the value picked and marking none, the log gets one WARNING per view class naming the
select: the person sees its placeholder, which reads as the pick undone unless the select is meant to
clear after each pick. A choice_row() dropdown is named by its
custom_id or its options, with selected= as the fix.
The render answer needs serialize_interactions. A DynamicPersistentButton click keeps its plain
acknowledgement, as does a modal submission that arrives while another click on the view runs. A
submission whose opening pick still waits on the modal (modal.wait()) is answered with the components alone,
while they are as the pick found them. A V1 view's embed or content comes from its nav_rebuild, else its
on_state_changed(), else the dict its build_ui() returns.
A callback that raises. The view's on_error() logs the error and sends the person an ephemeral
"Something went wrong" message showing error_message. It runs before the library acknowledges the
click, as in discord.py, so an override may answer through interaction.response until the click's
2.5-second timer answers. respond() works either way, and an answer that comes too late is replaced
by the default message, with a warning in the log. Whatever the override leaves unanswered is
acknowledged after it.
It runs in the click's turn, so the view's next click waits for it: do slow work, such as a message to the bot owner, in a task. See When a Callback Raises.
"Didn't respond in time" during a burst of clicks. When many clicks queue on one view, a click
can wait past auto_defer_delay for its turn. The timer acknowledges it and the click runs normally,
but that acknowledgement still has to reach Discord within the 3 seconds. When it arrives late, Discord
shows "didn't respond in time" although the callback ran and the panel updated, and the bot logs a
warning naming the control. Turn on performance sampling to see how long
clicks wait on a busy view.
Answering an interaction yourself.
| To do this | Use |
|---|---|
| Send several messages | respond() for the first, then interaction.followup.send() |
| Edit or delete a reply later | The message respond() returns |
| Send another CascadeUI view | That view's own send(), not respond(view=...) |
Views from plain discord.py have none of this: there, acknowledging is up to you.
Related¶
- How Discord Interactions Work: the platform rules behind the 3 seconds.
- Modals and Inputs: building the modal
open_modal()opens. - Who Can Click:
owner_only,allowed_users, and the refusal message. - Performance: measuring how long clicks wait.