Skip to content

Add autocomplete callback support#54

Open
Sim-hu wants to merge 1 commit intojchristgit:masterfrom
Sim-hu:add-autocomplete-callback
Open

Add autocomplete callback support#54
Sim-hu wants to merge 1 commit intojchristgit:masterfrom
Sim-hu:add-autocomplete-callback

Conversation

@Sim-hu
Copy link
Copy Markdown

@Sim-hu Sim-hu commented Mar 27, 2026

Summary

Add an optional autocomplete/1 callback to the Nosedrum.ApplicationCommand behaviour, enabling command modules to handle autocomplete interactions separately from regular command invocations.

Motivation

Discord sends autocomplete interactions (type 4) when a user is typing in an option with autocomplete: true. Currently, nosedrum routes these to the same command/1 callback as regular command invocations, forcing command modules to manually inspect the interaction type and branch accordingly.

This is a pain point for anyone using autocomplete — the command logic and autocomplete logic serve different purposes and should be separate callbacks.

Changes

  • lib/nosedrum/application_command.ex — add autocomplete/1 as an optional callback with documentation and example
  • lib/nosedrum/storage/dispatcher.ex — split handle_interaction to detect autocomplete interactions (type 4) and route to autocomplete/1 if implemented, falling back to command/1 for backwards compatibility

Usage

defmodule MyApp.Commands.Search do
  @behaviour Nosedrum.ApplicationCommand

  @impl true
  def type, do: :slash

  @impl true
  def description, do: "Search for something"

  @impl true
  def options do
    [%{type: :string, name: "query", description: "Search query", required: true, autocomplete: true}]
  end

  @impl true
  def autocomplete(interaction) do
    focused = Enum.find(interaction.data.options, & &1.focused)
    results = MyApp.search(focused.value)
    [type: :application_command_autocomplete_result, choices: Enum.map(results, &%{name: &1, value: &1})]
  end

  @impl true
  def command(interaction) do
    [%{name: "query", value: query}] = interaction.data.options
    [content: "Results for: #{query}"]
  end
end

Backwards Compatibility

Fully backwards compatible — autocomplete/1 is an optional callback. If not implemented, autocomplete interactions fall back to command/1 as before.

Testing

  • mix compile --warnings-as-errors passes
  • mix test --no-start — 79 tests, 0 failures

Add an optional `autocomplete/1` callback to ApplicationCommand
behaviour so that command modules can handle autocomplete
interactions separately from regular command invocations.

When an autocomplete interaction (type 4) is received, the
dispatcher checks if the command module implements `autocomplete/1`
and calls it instead of `command/1`. If not implemented, falls
back to `command/1` for backwards compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant