Skip to content

Commands

A slash command is one definition: name, description, options, component, and layer. The same object produces the Discord registration JSON and the handler.

src/commands/search.ts
import { SlashCommand } from "@arona/platform-discord";
import { Search } from "../components/search";
import { SearchCharactersLive } from "../queries/characters";
/** `/search` opens the character browser. */
export const search = SlashCommand.provide(
SlashCommand.make("search", {
description: "Search Rick and Morty API",
options: {
query: SlashCommand.string({ description: "Search query" }),
},
component: Search,
}),
SearchCharactersLive,
);

Option helpers declare Discord metadata and the params type at the same time. The component receives the decoded options as props.

Available helpers:

  • SlashCommand.string
  • SlashCommand.integer
  • SlashCommand.boolean
  • SlashCommand.number
  • SlashCommand.user
  • SlashCommand.channel
  • SlashCommand.role
  • SlashCommand.mentionable
  • SlashCommand.attachment

Options are optional unless you pass required: true. Integer and number options accept minValue and maxValue.

SlashCommand.provide composes an application layer onto the command:

export const search = SlashCommand.provide(
SlashCommand.make("search", { description: "Search", component: Search }),
SearchCharacters.layer,
);

The layer is built once per message mount and released when the message root closes.

Pass commands to the client when you create it:

DiscordGatewayClient.layerEphemeral({
token,
clientId,
devGuildId,
intents: [Intent.Guilds],
commands: [search],
});

The client replaces the application’s command list on login. With devGuildId, the list is guild-scoped and updates at once. Without it, the list is global and Discord may take up to an hour to show it.

The Worker transport uses the same command object. registerWorkerCommands registers its .json contract.

Command params arrive as props. For data, use an Effect UI Query service, not a Promise loader:

class Characters extends Query.Service<Characters>()("app/Characters", {
args: { query: Schema.String },
success: Schema.Array(Character),
error: Schema.Never,
staleTime: "5 minutes",
}) {
static readonly layer = this.toLayer(
Effect.gen(function* () {
const http = yield* HttpClient.HttpClient;
return ({ query }) => http.get(`/characters?name=${query}`).pipe(/* decode */);
}),
);
}

Attach the query to the component and provide the service through the command layer:

const Search = Discord.component({
name: "Search",
props: Schema.Struct({ query: Schema.String }),
model: Schema.Struct({ selected: Schema.String }),
message: Message,
queries: {
characters: {
query: Characters,
args: ({ props }) => ({ query: props.query }),
},
},
}).make({
init: () => ({ selected: "" }),
update: {/* ... */},
view: (h, model) =>
AsyncResult.match(h.queries.characters, {
onInitial: () => Text(h, "Loading..."),
onFailure: () => Text(h, "Could not load characters."),
onSuccess: ({ value }) => Text(h, `${value.length} characters`),
}),
});

Query state lives beside the Model. A result re-renders the message without an extra command. The full working version is in examples/rick-and-morty.

  • Components for the Discord vocabulary and composition rules.
  • Sessions for message lifetime.