Nubis / Commands
Bouwen
Commands
Define slash commands with command(), and the settings Discord reads on them.
De documentatie van Nubis is in het Engels, net als de API van het framework en zijn meldingen.
A command file default-exports command(...). The file's path is its name; see Project layout.
// commands/remind.ts → /remind
import { command, option } from "nubis";
export default command({
description: "Remind you about something",
ephemeral: true,
options: {
what: option.string({ required: true, maxLength: 200 }),
minutes: option.integer({ minValue: 1, maxValue: 1440 }),
},
run: async ({ options, reply }) => {
// options.what: string · options.minutes: number | undefined
const minutes = options.minutes ?? 10;
await reply(`I'll remind you about "${options.what}" in ${minutes} minutes.`);
},
});The run context
run gets one object:
| Field | What it is |
|---|---|
options | The option values, typed from options. See Options. |
reply(message) | Sends a message. The first answers the command; later ones are follow-ups. |
editReply(message) | Edits the first reply, e.g. to show progress. |
defer({ ephemeral }) | Shows "thinking…" now. Nubis does it on its own for slow handlers. |
showModal(modal) | Opens a modal. It has to be the first response. |
api | Discord's REST API, typed: api.guilds.banUser(...), api.channels.createMessage(...). |
interaction | The raw interaction, for anything Nubis doesn't cover yet. |
Replies explains how these fit together.
Settings
| Setting | What it does |
|---|---|
description | Shown in Discord's command picker. Required, 1-100 characters. |
options | The command's options, by name. |
ephemeral | Replies, and the automatic "thinking…", are visible only to the user. |
guards | Checks that run before run. See Guards and middleware. |
middleware | Code that wraps run. |
defaultMemberPermissions | Hides the command from members without these permissions. |
contexts | Where it can be used: "guild", "bot-dm", "private-channel". |
integrationTypes | "guild", and "user" to make it usable when installed to a user's account. |
nsfw | Only shown in age-restricted channels. |
The last four are read by Discord on top-level commands only. For a command with subcommands, set them in the folder's _folder.ts.
Permissions that hold
defaultMemberPermissions is a default: a server's admins can change who sees the command in their server settings. When it matters, pair it with a guard, which is checked every time the command runs:
import { command, hasPermission } from "nubis";
import { PermissionFlagsBits } from "discord-api-types/v10";
export default command({
description: "Clear messages",
defaultMemberPermissions: [PermissionFlagsBits.ManageMessages],
guards: [hasPermission(PermissionFlagsBits.ManageMessages)],
run: ({ reply }) => reply("Cleared."),
});Registering commands
Discord has to be told about commands before they show up. nubis dev does it for your test server every time it starts and whenever a command changes. For everyone else, run nubis sync --global when you deploy; see Running.