Autocompletes

Autocompletes provides your bot the ability to give your users a helpful interface which shows what values are valid / provide the best result / relevant to the command.

It is a powerful tool that is capable of providing massive quality of life improvements as it is able to remove the guesswork or shine light on possible or better answers that might have otherwise been completely skipped.

Autocompletes are only available in String, Number, and Integer options, and can use the defineOption Definition Function for extended type support.

TIP

For more advanced uses, visit the Setup Method guide.

Username Autocomplete

import { defineOption, defineSubcommand } from 'chooksie'

async function db() {
  const fakeDb = await import('../some-fake-db')
  return { fakeDb }
}

const getUser = defineSubcommand({
  name: 'get',
  description: 'Get an account\'s details.',
  type: 'SUB_COMMAND',
  setup: db,
  async execute(ctx) {
    const username = ctx.interaction.options.getString('username', true)
    const userDetails = await this.fakeDb.get(username)
    await ctx.interaction.reply(userDetails)
  },
  options: [
    defineOption({
      name: 'username',
      description: 'The account\'s username.',
      type: 'STRING',
      required: true,
      setup: db,
      async autocomplete(ctx) {
        const input = ctx.interaction.options.getFocused()
        const users = await this.fakeDb.getAll({ username: input })

        await ctx.interaction.respond(users.map(user => ({
          name: user.username,
          value: users.username,
        })))
      },
    }),
  ],
})


















 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


import { defineOption, defineSubcommand } from 'chooksie'

async function db() {
  const fakeDb = await import('../some-fake-db')
  return { fakeDb }
}

const getUser = defineSubcommand({
  name: 'get',
  description: 'Get an account\'s details.',
  type: 'SUB_COMMAND',
  setup: db,
  async execute(ctx) {
    const username = ctx.interaction.options.getString('username', true)
    const userDetails = await this.fakeDb.get(username)
    await ctx.interaction.reply(userDetails)
  },
  options: [
    defineOption({
      name: 'username',
      description: 'The account\'s username.',
      type: 'STRING',
      required: true,
      setup: db,
      async autocomplete(ctx) {
        const input = ctx.interaction.options.getFocused()
        const users = await this.fakeDb.getAll({ username: input })

        await ctx.interaction.respond(users.map(user => ({
          name: user.username,
          value: users.username,
        })))
      },
    }),
  ],
})


















 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


const { defineOption, defineSubcommand } = require('chooksie')

function db() {
  const fakeDb = require('../some-fake-db')
  return { fakeDb }
}

const getUser = defineSubcommand({
  name: 'get',
  description: 'Get an account\'s details.',
  type: 'SUB_COMMAND',
  setup: db,
  async execute(ctx) {
    const username = ctx.interaction.options.getString('username', true)
    const userDetails = await this.fakeDb.get(username)
    await ctx.interaction.reply(userDetails)
  },
  options: [
    defineOption({
      name: 'username',
      description: 'The account\'s username.',
      type: 'STRING',
      required: true,
      setup: db,
      async autocomplete(ctx) {
        const input = ctx.interaction.options.getFocused()
        const users = await this.fakeDb.getAll({ username: input })

        await ctx.interaction.respond(users.map(user => ({
          name: user.username,
          value: users.username,
        })))
      },
    }),
  ],
})