Commands
A slash command is one definition: name, description, options, component, and layer. The same object produces the Discord registration JSON and the handler.
Declare a command
Section titled “Declare a command”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.stringSlashCommand.integerSlashCommand.booleanSlashCommand.numberSlashCommand.userSlashCommand.channelSlashCommand.roleSlashCommand.mentionableSlashCommand.attachment
Options are optional unless you pass required: true. Integer and number
options accept minValue and maxValue.
Attach a layer
Section titled “Attach a layer”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.
Register the command
Section titled “Register the command”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.
Load data with queries
Section titled “Load data with queries”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.