Create a Discord bot
A Discord bot is an application, a token, and a gateway connection. Arona adds the command registration and the message runtime.
Create the application
Section titled “Create the application”- Open the Discord Developer Portal.
- Select New Application and name it.
- Open General Information and copy the Application ID.
- Open Bot and select Reset Token. Copy the token now; Discord shows it once.
- Open Installation. Add the
botandapplications.commandsscopes. Give the bot Send Messages, Attach Files, and Read Message History. - Copy the install link and add the bot to your server.
Slash commands use interactions. They need no privileged intent. Arona asks for
Intent.Guilds only.
Configure the environment
Section titled “Configure the environment”Create .env in the project root:
DISCORD_APP_TOKEN=your-bot-tokenDISCORD_APP_ID=your-application-idDISCORD_DEV_GUILD_ID=your-server-idDISCORD_DEV_GUILD_ID is optional. When set, commands register in that server
at once. When empty, commands register globally, which can take up to an hour.
Run the bot
Section titled “Run the bot”pnpm devThe process logs in, reloads the command list, and starts the gateway. The
scaffolded default template is copied from this example, which wires
telemetry and the devtools layer:
import { Telemetry } from "@arona/core";import { layerFromEnv } from "@arona/devtools";import { DiscordGatewayClient, Intent } from "@arona/platform-discord";import { NodeRuntime } from "@effect/platform-node";import { Config, Effect, Layer } from "effect";import { counter } from "./counter";
const program = Effect.gen(function* () { const config = yield* Config.all({ token: Config.Redacted("DISCORD_APP_TOKEN"), clientId: Config.String("DISCORD_APP_ID"), devGuildId: Config.option(Config.String("DISCORD_DEV_GUILD_ID")), });
return yield* Effect.gen(function* () { const client = yield* DiscordGatewayClient;
const user = yield* client.login; yield* Effect.log(`🚀 Logged in as ${user.username}`); return yield* Effect.never; }).pipe( Effect.provide( DiscordGatewayClient.layerEphemeral({ token: config.token, clientId: config.clientId, devGuildId: config.devGuildId, intents: [Intent.Guilds], commands: [counter], }), ), );});
const telemetry = process.env.NODE_ENV === "production" ? Telemetry.layerFromEnv : Telemetry.layer({ console: true });
NodeRuntime.runMain(program.pipe(Effect.provide(Layer.merge(telemetry, layerFromEnv()))));A command is one definition. Options declare the params type and the Discord metadata at the same time:
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,);SlashCommand.provide attaches the layer the component needs. The component
receives the decoded options as props.
Sessions
Section titled “Sessions”DiscordGatewayClient.layerEphemeral keeps messages in memory. Use it for
demos and short-lived bots.
DiscordGatewayClient.layer needs a MessageSessionStore. Messages then
survive a restart, and stale interactions are rejected. See
Resumable messages for the stores.
- Create a Slack bot with the same component model.
- Read Commands for options, queries, and layers.