Usage with TypeScript

While the majority of the framework was designed with type safety in mind without explicit use of TypeScript features through the use of Definition Functions, it is still fully possible to use Chooksie with full type safety while having 0 runtime imports.

  • All Definition Functions have their own corresponding interface, all defined without the define prefix (ie. defineSlashCommand will have an interface named SlashCommand).

  • All Options can use the Option type, but Specialized Options like Subcommand and those that utilize the Setup functionality should use their dedicated interfaces respectively.

Basic Examples

Basic usage includes everything that doesn't use the setup feature, since these do not need any special type treatment.

Slash Commands

import type { SlashCommand } from 'chooksie'

const pingCommand: SlashCommand = {
  name: 'ping',
  description: 'Pong!',
  async execute(ctx) {
    await ctx.interaction.reply('Pong!')
  },
}

export default pingCommand
 

 








Subcommand Groups (Composed)

import type { Option, SlashSubcommand, Subcommand, SubcommandGroup } from 'chooksie'

const stringOption: Option = {
  name: 'text',
  description: 'The text to transform.',
  type: 'STRING',
  required: true,
}

const upper: Subcommand = {
  name: 'upper',
  description: 'Transform a text to be all uppercase.',
  type: 'SUB_COMMAND',
  async execute(ctx) {
    const targetText = ctx.interaction.options.getString('text', true)
    await ctx.interaction.reply(targetText.toUpperCase())
  },
  options: [stringOption],
}

const lower: Subcommand = {
  name: 'lower',
  description: 'Transform a text to be all lowercase.',
  type: 'SUB_COMMAND',
  async execute(ctx) {
    const targetText = ctx.interaction.options.getString('text', true)
    await ctx.interaction.reply(targetText.toLowerCase())
  },
  options: [stringOption],
}

const changeCase: SubcommandGroup = {
  name: 'case',
  description: 'Change a string\'s case.',
  type: 'SUB_COMMAND_GROUP',
  options: [
    upper,
    lower,
  ],
}

const stringCommand: SlashSubcommand = {
  name: 'string',
  description: 'Perform string manipulations.',
  options: [changeCase],
}

export default stringCommand
 

 






 










 










 









 






Examples with Setup

Using inline setups is where Definition Functions really do their best in inferring types, as doing it manually would require you to define the return value manually, or infer them from external functions.

TIP

Chooksie provides a type util called InferSetupType to infer the type of external functions.

Inline Setup

import type { SlashCommand } from 'chooksie'

const pingCommand: SlashCommand<{ message: () => string }> = {
  name: 'ping',
  description: 'Pong!',
  setup() {
    const getMessage = () => 'Pong!'
    return { message: getMessage }
  },
  async execute(ctx) {
    await ctx.interaction.reply(this.message())
  },
}

export default pingCommand
 

 




 







Reusable Setup

import type { InferSetupType, SlashCommand } from 'chooksie'

function setup() {
  const getMessage = () => 'Pong!'
  return { message: getMessage }
}

const pingCommand: SlashCommand<InferSetupType<typeof setup>> = {
  name: 'ping',
  description: 'Pong!',
  setup,
  async execute(ctx) {
    await ctx.interaction.reply(this.message())
  },
}

export default pingCommand
 

 
 
 
 

 









List of Interfaces

TIP

To see a full list of available types, visit the Type Referenceopen in new window or trigger autocomplete in your editor when importing chooksie.

Contexts


  • Context
  • EventContext
  • CommandContext

Modules


  • Event
  • SlashCommand
  • SlashSubcommand
  • UserCommand
  • MessageCommand

Specialized Options


  • Subcommand
  • StringOption
  • NumberOption

All Options


  • SubcommandGroup
  • ChannelOption
  • BoolOption
  • UserOption
  • RoleOption
  • MentionableOption
  • Choice

Misc

  • OnLoad