Skip to content

Features in their own files

A feature, such as tickets, can live in its own file as a sub-app, which is a plain Discord instance. Mount it with app.route(), like a sub-app in Hono. Discord has no URL paths, so the mount prefix is a command name, and the sub-app’s commands become its subcommands.

src/features/tickets.ts
import { ApplicationCommandOptionType, Discord } from "discord0";
export const tickets = new Discord()
.command(
{
name: "open",
description: "Open a ticket",
options: [
{
type: ApplicationCommandOptionType.String,
name: "reason",
description: "Why",
required: true,
},
],
},
(c) => `Opened: ${c.options.reason}`,
)
.command("close", "Close a ticket", () => "Closed")
.autocomplete("open", (c) => [])
.component("ticket-close:{id}", (c) => c.update("Closed"));
src/index.ts
import { Discord } from "discord0";
import { tickets } from "./features/tickets";
const app = new Discord()
.command("ping", "Replies with Pong!", () => "Pong!")
.route("ticket", "Manage tickets", tickets); // /ticket open, /ticket close
export default app;

A handler doesn’t know where it’s mounted. c.options holds the subcommand’s own options, flat and typed as they would be on a top-level command. Names in the sub-app are relative to it, so autocomplete("open") answers /ticket open once mounted.

Call Result
route("ticket", "Manage tickets", sub) The sub-app’s commands become subcommands of /ticket.
route({ name, description, ... }, sub) The same, with top-level command fields such as default_member_permissions.
route(sub) The sub-app’s commands, including context menus, join the app at the top level.

A sub-app can mount another sub-app, which becomes a subcommand group, such as /ticket admin ban. Discord allows only a command, a group, and a subcommand, so mounting one more level throws.

Components, modals, scheduled tasks, webhook routes, and event handlers register on the main app unchanged, so mounting doesn’t rename their custom IDs, names, or paths. Keep custom IDs unique across features, for example with a feature prefix such as ticket-close:{id}.

Mounting copies the sub-app and seals it. Registering anything on a sealed sub-app throws, rather than leaving the new handler silently unrouted. Register everything on a sub-app before you mount it, and mount inner sub-apps before outer ones.

The main app’s settings apply to every handler, so a sub-app built with constructor options or lifecycle hooks (beforeHandle, afterHandle, onError) throws when mounted.

Mounting checks the result against Discord’s limits and throws with the mount in the message, such as route("ticket"): Duplicate command: ticket. It rejects:

  • Names that aren’t 1 to 32 lowercase letters, numbers, -, or _, and descriptions that aren’t 1 to 100 characters.
  • A command path that already exists, and a duplicate autocomplete handler, component, modal, task name, or webhook path.
  • More than 25 subcommands or groups at one level.
  • More than 100 slash commands, or 15 user or 15 message commands.
  • A command over 8000 characters across its names, descriptions, and choice values.
  • In a sub-app mounted under a name, context menu commands, and commands that set top-level fields such as default_member_permissions or define their own subcommand options. Set top-level fields on the mount instead.

A mount that fails changes nothing, and leaves the sub-app unsealed.