Skip to main content

Overview

Command types define the structure for both prefix commands and slash commands in Flora.

Command

Type for prefix (text-based) commands.
type Command = {
  name: string
  description?: string
  run: (ctx: MessageContext & { args: string[] }) => Promise<void> | void
}

Properties

name
string
required
The command name (without prefix). Users type <prefix><name> to invoke.
description
string
Optional human-readable description of what the command does
run
function
required
Handler function that executes when the command is invokedParameters:
  • ctx: MessageContext - The message context
  • ctx.args: string[] - Array of space-separated arguments after the command
Example:
// User types: !echo hello world
// ctx.args = ['hello', 'world']

Example

const echoCmd: Command = {
  name: 'echo',
  description: 'Repeat a message',
  run: async (ctx) => {
    const text = ctx.args.join(' ')
    await ctx.reply(text || 'Nothing to echo!')
  }
}

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

SlashCommand

Type for Discord slash commands.
type SlashCommand = {
  name: string
  description: string
  options?: SlashCommandOption[]
  subcommands?: SlashSubcommand[]
  run?: (ctx: InteractionContext) => Promise<void> | void
}

Properties

name
string
required
Command name (lowercase, no spaces, 1-32 characters)
description
string
required
Description shown in Discord’s UI (1-100 characters)
options
SlashCommandOption[]
Array of command parameters. Mutually exclusive with subcommands.
subcommands
SlashSubcommand[]
Array of subcommands. Mutually exclusive with options and run.
run
function
Handler function for the command. Not used if subcommands is provided.

Example: Simple Command

const pingCmd: SlashCommand = {
  name: 'ping',
  description: 'Check bot latency',
  run: async (ctx) => {
    await ctx.reply('Pong! 🏓')
  }
}

Example: Command with Options

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

Example: Command with Subcommands

const modCmd: SlashCommand = {
  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) => {
        await ctx.reply(`Kicked ${ctx.options.user}`)
      }
    },
    {
      name: 'ban',
      description: 'Ban a user',
      options: [{
        name: 'user',
        description: 'User to ban',
        type: 'string',
        required: true
      }],
      run: async (ctx) => {
        await ctx.reply(`Banned ${ctx.options.user}`)
      }
    }
  ]
}

SlashCommandOption

Defines a parameter for a slash command.
type SlashCommandOption = {
  name: string
  description: string
  type?: 'string' | 'integer' | 'number' | 'boolean' | 'subcommand' | 'subcommand_group'
  required?: boolean
  options?: SlashCommandOption[]
}

Properties

name
string
required
Option name (lowercase, no spaces, 1-32 characters)
description
string
required
Description shown in Discord’s UI (1-100 characters)
type
string
The value type this option acceptsAvailable types:
  • 'string' - Text input (default)
  • 'integer' - Whole number
  • 'number' - Decimal number
  • 'boolean' - True/false
  • 'subcommand' - Nested subcommand
  • 'subcommand_group' - Group of subcommands
required
boolean
default:"false"
Whether this option must be provided
options
SlashCommandOption[]
Nested options (for subcommands and subcommand groups)

Example

const options: SlashCommandOption[] = [
  {
    name: 'message',
    description: 'Message to send',
    type: 'string',
    required: true
  },
  {
    name: 'count',
    description: 'Number of times to send',
    type: 'integer',
    required: false
  },
  {
    name: 'silent',
    description: 'Send silently',
    type: 'boolean',
    required: false
  }
]

SlashSubcommand

Defines a subcommand within a slash command.
type SlashSubcommand = {
  name: string
  description: string
  options?: SlashCommandOption[]
  run: (ctx: InteractionContext) => Promise<void> | void
}

Properties

name
string
required
Subcommand name (lowercase, no spaces)
description
string
required
Description shown in Discord’s UI
options
SlashCommandOption[]
Parameters for this subcommand
run
function
required
Handler function for this subcommand

Example

const subcommands: SlashSubcommand[] = [
  {
    name: 'add',
    description: 'Add a role',
    options: [{
      name: 'role',
      description: 'Role to add',
      type: 'string',
      required: true
    }],
    run: async (ctx) => {
      const role = ctx.options.role as string
      await ctx.reply(`Added role: ${role}`)
    }
  },
  {
    name: 'remove',
    description: 'Remove a role',
    options: [{
      name: 'role',
      description: 'Role to remove',
      type: 'string',
      required: true
    }],
    run: async (ctx) => {
      const role = ctx.options.role as string
      await ctx.reply(`Removed role: ${role}`)
    }
  }
]

const roleCmd: SlashCommand = {
  name: 'role',
  description: 'Manage roles',
  subcommands
}

FlattenedSlashCommand

Internal representation of a slash command for Discord registration.
type FlattenedSlashCommand = {
  name: string
  description: string
  options?: SlashCommandOption[]
}
This type is used internally by createBot() when registering commands with Discord. You typically don’t need to use this directly.

CreateOptions

Configuration object for createBot().
type CreateOptions = {
  prefix?: string
  commands?: Command[]
  prefixCommands?: Command[]
  slashCommands?: SlashCommand[]
}

Properties

prefix
string
default:"!"
The prefix for text commands
commands
Command[]
Array of prefix commands (alias for prefixCommands)
prefixCommands
Command[]
Array of prefix commands
slashCommands
SlashCommand[]
Array of slash commands to register

Example

const options: CreateOptions = {
  prefix: '!',
  commands: [echoCmd, pingCmd],
  slashCommands: [rollCmd, modCmd]
}

createBot(options)

Notes

  • Command names must be lowercase with no spaces
  • Slash command descriptions are required; prefix command descriptions are optional
  • You cannot mix subcommands with options/run in the same slash command
  • Options in ctx.options are automatically flattened and type-cast
  • Required options must come before optional options in the array

Build docs developers (and LLMs) love