Skip to content

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.

  1. Open the Discord Developer Portal.
  2. Select New Application and name it.
  3. Open General Information and copy the Application ID.
  4. Open Bot and select Reset Token. Copy the token now; Discord shows it once.
  5. Open Installation. Add the bot and applications.commands scopes. Give the bot Send Messages, Attach Files, and Read Message History.
  6. 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.

Create .env in the project root:

Terminal window
DISCORD_APP_TOKEN=your-bot-token
DISCORD_APP_ID=your-application-id
DISCORD_DEV_GUILD_ID=your-server-id

DISCORD_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.

Terminal window
pnpm dev

The 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:

examples/default/src/index.ts
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:

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,
);

SlashCommand.provide attaches the layer the component needs. The component receives the decoded options as props.

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.