Skip to content

Lesson 2 of 9 · 23m

Interactions, Components, and State That Survive Reality

Design slash commands, buttons, modals, and follow-up state so retries, double clicks, deploys, and permission changes do not corrupt behavior.

Users do not experience your architecture. They experience a command that either answered or hung, a button that either worked or said "This interaction failed," and a moderation action that either happened once or twice. Interaction design is therefore not UI polish; it is distributed systems work with a three-second fuse.

The first response is a promise

Discord expects an initial interaction response quickly. Depending on the operation, you can respond immediately, defer, or show a modal. In discord.js the common shapes are:

await interaction.reply({ content: "Case closed.", ephemeral: true });
await interaction.deferReply({ ephemeral: true });
await interaction.showModal(modal);
await interaction.editReply({ content: "Finished processing 418 messages." });

The mistake is treating deferReply() as a license to run a slow job inline. Deferral buys seconds, not architectural soundness. If the work can outlive the interaction process, enqueue it and edit the original response when finished.

Components outlive the command

A button click is a new interaction, not a continuation of your in-memory function call. If you store approval state only in memory, a restart or a different replica makes the button useless. Store enough durable state to reconstruct intent:

interaction_token      short-lived response credential
custom_id              routing hint, not trusted authorization
workflow_id            durable reference to pending action
expires_at             explicit expiry
actor_id/guild_id      authorization context
payload_version        schema version for forward compatibility

A good custom_id is compact and opaque, such as modcase:approve:01JXYZ, while the database row carries the authoritative action details. Never let a button label or custom_id alone prove that the clicker may execute the action.

Rebuild authorization at click time

The moderator who saw the button may not still have permission when it is clicked. The target may have gained roles, left the guild, or changed position in the hierarchy. Re-evaluate everything:

if (!await policy.canApprove(member, workflow)) {
  await interaction.reply({ content: "You can no longer approve this case.", ephemeral: true });
  return;
}

This feels redundant until a moderator is demoted and an old approval button still works. Stale authority is still authority if your code honors it.

Make actions idempotent

Retries happen because users double-click, clients reconnect, Discord redelivers, or your worker crashes after acting but before recording completion. Model transitions explicitly:

pending -> approved -> executing -> completed
                         -> failed_retryable

Store a unique action key and make the executor check current state before mutating Discord. "Timeout user twice" may be harmless; "post punishment announcement twice" and "refund twice" are not.

Field notes

  • Ephemeral responses are visibility choices, not security boundaries. Sensitive workflows still need server-side authorization.
  • Modal submissions are separate interactions; do not assume the same process remembers anything from command invocation.
  • Put workflow expiry in the data model. Old buttons become confusing attack surface and support burden.
  • Log interaction IDs in traces, but never log interaction tokens after use more broadly than required.

Was this lesson helpful?

Sign in to rate