SWAGcommands
Commands

Options and autocomplete

Collect Discord option values and suggest choices as someone types.

Slash command options are discord.js ApplicationCommandOptionData objects. SWAGcommands includes them in the manifest you deploy. In the callback, read rich values from interaction.options; use args only when string normalization is what you want.

import { ApplicationCommandOptionType } from "discord.js";
import { CommandType } from "swagcommands";
import type { CommandObject } from "swagcommands";

const color = {
  type: CommandType.SLASH,
  description: "Choose a color",
  options: [{
    name: "shade",
    description: "A color name",
    type: ApplicationCommandOptionType.String,
    required: true,
    autocomplete: true,
  }],
  autocomplete: (_command, focusedOption) => {
    if (focusedOption !== "shade") return [];
    return ["blue", "blue gray", "violet", "silver"];
  },
  callback: ({ interaction }) =>
    `You picked ${interaction.options.getString("shade", true)}.`,
} satisfies CommandObject;

export default color;

The autocomplete callback receives the command object, focused option name, and autocomplete interaction. Return an array of strings (or a promise of one). SWAGcommands filters them by the current typed value, keeps the first 25 matches, and calls interaction.respond() with name/value pairs. Return [] for an option you don't handle.

Autocomplete is its own Discord interaction. It does not run normal command preconditions or use the normal response path. If a callback returns the wrong shape or throws, onError receives AutocompleteError; the framework attempts to send an empty list when it still can.

If a normal slash-capable command omits options but supplies expectedArgs, deployment derives string options from that syntax. minArgs marks the first options as required. Explicit options take precedence and are better for users, choices, numbers, and other rich Discord types.

Subcommand leaves can also define options and autocomplete. For grouped subcommands, the leaf's options describe the nested Discord subcommands. See Subcommands.