Skip to main content

prefix()

Define a text-based prefix command that users trigger by typing a prefix followed by the command name.

Signature

function prefix(command: Command): Command

Parameters

command
Command
required

Return Value

Returns the same Command object (identity function for type safety).

Example

const sayCmd = prefix({
  name: 'say',
  description: 'Echo a message',
  run: async (ctx) => {
    const text = ctx.args.join(' ')
    await ctx.reply(text || 'You didn\'t say anything!')
  }
})

createBot({
  prefix: '!',
  commands: [sayCmd]
})

// User types: !say Hello world
// Bot replies: Hello world

slash()

Define a slash command that appears in Discord’s command menu.

Signature

function slash(command: SlashCommand): SlashCommand

Parameters

command
SlashCommand
required

Return Value

Returns the same SlashCommand object (identity function for type safety).

Simple Command Example

const rollCmd = slash({
  name: 'roll',
  description: 'Roll a dice',
  options: [{
    name: 'sides',
    description: 'Number of sides',
    type: 'integer',
    required: false
  }],
  run: async (ctx) => {
    const sides = ctx.options.sides as number ?? 6
    const result = Math.floor(Math.random() * sides) + 1
    await ctx.reply(`You rolled a ${result}!`)
  }
})

Subcommand Example

const modCmd = slash({
  name: 'mod',
  description: 'Moderation commands',
  subcommands: [
    {
      name: 'kick',
      description: 'Kick a user',
      options: [{
        name: 'user',
        description: 'User to kick',
        type: 'string',
        required: true
      }],
      run: async (ctx) => {
        const user = ctx.options.user
        await ctx.reply(`Kicked ${user}`)
      }
    },
    {
      name: 'ban',
      description: 'Ban a user',
      options: [{
        name: 'user',
        description: 'User to ban',
        type: 'string',
        required: true
      }],
      run: async (ctx) => {
        const user = ctx.options.user
        await ctx.reply(`Banned ${user}`)
      }
    }
  ]
})

SlashCommandOption

Properties

name
string
required
Option name (lowercase, no spaces)
description
string
required
Description shown in Discord’s UI
type
'string' | 'integer' | 'number' | 'boolean' | 'subcommand' | 'subcommand_group'
The type of value this option accepts (defaults to ‘string’)
required
boolean
Whether this option must be provided (defaults to false)
options
SlashCommandOption[]
Nested options (for subcommands and subcommand groups)

Notes

  • Command names must be lowercase with no spaces
  • Slash commands are automatically registered when passed to createBot()
  • You cannot mix subcommands with options or run in the same command
  • Options are automatically flattened and available via ctx.options

Build docs developers (and LLMs) love