> ## Documentation Index
> Fetch the complete documentation index at: https://www.chapterjs.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Your Discord application

> Create the application of your bot, get its token and the ID of your test server, and enable what your files need

A bot is a Discord application with a bot account. You create it in the [Developer Portal](https://discord.com/developers/applications), and `chapterjs dev` needs two things from it: the token of the bot and the ID of the server you test in.

> [!NOTE]
>
> **Create two applications from the start: one for development, one for production.** Name them after your bot, with "dev" in the name of the first (`my-bot dev`, `my-bot`). The token of the dev one goes in the `.env` of your computer, the token of the other one on the machine that will host your bot. A command for everyone belongs to the bot, not to a server, so with one bot for both your test server shows every command you are changing twice, and the presence, the privileged intents and the token of production are shared with what you try. Why in detail: [A second bot for development](https://www.chapterjs.org/run/start.md#a-second-bot-for-development).

**1. Create the application**

Open the [Developer Portal](https://discord.com/developers/applications), click **New Application** and give it a name: it is the name people see. Do it twice, once for the dev bot and once for the production bot.

Then open the **Bot** page of the application. This is where the bot account of your application lives: its username, its avatar and its token.

**2. Copy the token**

On the **Bot** page of the **dev** application, click **Reset Token** and copy the token it shows. Discord shows a token once: if you lose it, reset it again, which makes the old one stop working everywhere it was used.

Put it in the `.env` file of your project:

```bash .env theme={"theme":{"light":"github-light","dark":"vesper"}}
BOT_TOKEN=
DEV_GUILD_ID=
```

> [!WARNING]
>
> The token is the password of your bot: whoever has it controls the bot. Never share it, never put it in your code, and never commit `.env`. A project made with `create chapterjs` already ignores it in git.

**3. Copy the ID of your test server**

Your test server is where `chapterjs dev` runs your bot. Any server you manage works: create one for your bot if you don't have one.

Server IDs are hidden until you enable **Developer Mode** in Discord: **Settings** → **Advanced** → **Developer Mode**. Then right-click your server and choose **Copy Server ID**. The ID is a number of 17 to 20 digits. Put it in `DEV_GUILD_ID`.

**4. Add the bot to your test server**

Nothing to find: run your bot, and `chapterjs dev` prints the link when the bot is not in the server yet.

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
⚠ Your bot is not in your dev server yet (DEV_GUILD_ID=123456789012345678).
  Open this link to add it:
  https://discord.com/oauth2/authorize?client_id=...&scope=bot+applications.commands&permissions=8&guild_id=123456789012345678&disable_guild_select=true
  If the link shows another server, DEV_GUILD_ID is not the ID of a server you manage.
  Waiting for the bot to be added...
```

Open the link, confirm, and the command goes on by itself:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
✓ The bot joined My Server
```

The link adds the bot to that server only, with every permission (`permissions=8` is Administrator): a test server is a playground. It also asks for the `applications.commands` scope, which lets the bot have slash commands there.

That is all: run `chapterjs dev` and your bot is online in your test server. The rest of this page is about the three options of the **Bot** page that some files need.

> [!NOTE]
>
> What `chapterjs dev` reads from `.env`, and in which order, is in the [CLI reference](https://www.chapterjs.org/reference/cli.md#env).

## Privileged intents

Discord only sends a bot what it asked for, and three things have to be enabled by hand, under **Privileged Gateway Intents** on the **Bot** page of your application: **Presence Intent**, **Server Members Intent** and **Message Content Intent**. You never list them in your code: ChapterJS works out what your files need, and asks you to enable an option only when a file needs it.

| Option to enable | Needed by the files of |
| - | - |
| **Message Content Intent** | `messageCreate`, `messageUpdate` |
| **Server Members Intent** | `memberJoin`, `memberLeave`, `memberUpdate`, `threadMemberJoin`, `threadMemberLeave` |
| **Presence Intent** | `presenceUpdate` |

When a file needs an option that is not enabled, `chapterjs dev` says which files and gives the link to the page, then waits:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
⚠ Your files need an option that is not enabled for your bot:
  - Message Content Intent, needed by src/events/ping.ts
  Enable it under "Privileged Gateway Intents", then save:
  https://discord.com/developers/applications/123456789012345678/bot
  Waiting for you to enable it...
```

Enable the option, click **Save Changes**, and the command goes on:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
✓ Privileged intents enabled
```

With several options missing, the message lists them all, and the files that need each one:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
⚠ Your files need options that are not enabled for your bot:
  - Server Members Intent, needed by src/events/welcome.ts
  - Message Content Intent, needed by src/events/ping.ts, src/events/edits.ts
  Enable them under "Privileged Gateway Intents", then save:
  https://discord.com/developers/applications/123456789012345678/bot
  Waiting for you to enable them...
```

The same check runs when a file you save while the bot runs needs a new option: the bot reconnects by itself once the option is enabled. See [chapterjs dev](https://www.chapterjs.org/run/dev.md).

> [!NOTE]
>
> `chapterjs dev` only waits when it runs in a terminal. Started by another program, and in production with [`chapterjs start`](https://www.chapterjs.org/run/start), the same message is an error (`✗`) and the command stops.

> [!NOTE]
>
> A bot in 100 servers or more has to be verified by Discord to keep its privileged intents. Until then, the options can be enabled freely on the **Bot** page.

## When something is wrong with a value

Each value of `.env` is checked before anything is tried, and every problem is printed at once, with how to fix it:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
✗ .env: BOT_TOKEN is empty.
  Copy the token of your bot from https://discord.com/developers/applications → your application → Bot → Reset Token.
✗ .env: DEV_GUILD_ID is "my-server", which is not a server ID (an ID only has digits).
  In Discord, right-click your test server → Copy Server ID.
```

A token Discord does not recognize is refused at the first request:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
✗ Discord refused the bot token. Copy it again from the Developer Portal (your application → Bot → Reset Token) into BOT_TOKEN in your .env file.
```

Every message is listed in [Troubleshooting](https://www.chapterjs.org/run/troubleshooting.md).

> [card] [Project structure](https://www.chapterjs.org/setup/project-structure.md)
>
> What each folder and file of your project is for.

> [card] [chapterjs dev](https://www.chapterjs.org/run/dev.md)
>
> Run your bot on your test server while you write it.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.