# Node Discord bot

A Discord bot answers uptime questions, rolls familiar dice, and gives playful `8ball` replies inside a server you manage. Tokay keeps its outbound Discord connection running as a Background Worker with logs beside the Service.

This is the useful shape for a bot that waits on a provider connection instead of serving a web page. You bring a Discord bot token, and Tokay keeps the process alive and makes connection failures visible.

## Facts

- Runtime: Node.js
- Framework: discord.js
- Service types: Background Worker
- Resources: None
- Required secrets: `DISCORD_TOKEN`

## What it does

Once **Logs** shows `Campfire ready`, the bot registers three slash commands in your Discord server.

Use `/uptime` to see when the current process started. Ask `/8ball` a question for a random campfire answer, or choose a die from d4 through d100 with `/roll`.

When the Worker restarts, Discord reconnects and the commands register again. The next uptime response reflects the new process.

## Deploy it

Bring this folder to Tokay any way you like. Paste or upload it in the dashboard, push it with git, or hand it to your AI agent along with [our agent instructions](https://app.tokay.io/llms.txt). Tokay figures out the rest.

Working in Claude, ChatGPT, VS Code, or another MCP client? Connect the [Tokay MCP server](https://tokay.io/docs/platform-mcp) and ask your agent to deploy this example. It signs in through your browser, so there is no token to paste.

New to Tokay? The [getting started guide](https://tokay.io/getting-started) walks you through your first deploy.

## Set up Discord first

Create the bot in Discord before you deploy it. You will receive one token that lets this app connect to your server. Keep that token out of this folder and out of chat.

1. Open the [Discord Developer Portal](https://discord.com/developers/applications) and choose **New Application**.
2. Name the app, create it, then open **Bot** in the left sidebar.
3. Under **Token**, choose **Reset Token** and copy the value. Discord only shows it once.
4. Open **Installation**. Under **Install Link**, keep **Discord Provided Link**.
5. Under **Default Install Settings** for Guild Install, add the `applications.commands` and `bot` scopes. Give the bot **Send Messages** permission.
6. Copy the install link, open it, choose **Add to server**, and select a test server you manage.

These commands do not need privileged intents. If a Discord screen looks different, the [official Discord guide](https://docs.discord.com/developers/quick-start/getting-started) covers the current token and installation flow.

## Add the Discord token

Open **Environment** and find `DISCORD_TOKEN` under **From your code**. Choose **Set shared value**, paste the bot token, and save it.

When the row says **Good to go**, follow the deployment tracker and choose **Go Live** if that action appears. The Background Worker has no public page. **Logs** will show when the bot is connected.

## Try it

1. Wait for `Campfire ready` in **Logs**, then run `/roll die: d20` in Discord.
2. Run `/uptime` and note the reported start time.
3. Restart the Worker and ask again. The command returns after reconnection with a new start time.

If the token is wrong, **Logs** shows a direct Discord login error. Tokay restarts the Background Worker and makes the repeated failure visible as an incident. Replace `DISCORD_TOKEN` with a valid value and the bot will reconnect.

## How it works

You do not need a public callback URL because `discord.js` opens one outbound gateway connection. The ready event registers the commands for every installed server.

Tokay sends shutdown signals during a deployment, and the bot closes its Discord client cleanly. Reconnects, errors, and command registration remain visible in **Logs** when you need to trace a problem.

## Secrets

| Name | Required | Purpose |
| --- | --- | --- |
| `DISCORD_TOKEN` | Yes | Authenticates this bot with Discord. Save it under the Project's encrypted Secrets. |

## Grow from here

Try the [statement generator](/examples/node-express-bullmq-redis-statement-generator) when you want an always running worker that pulls saved jobs from Redis.

## Source files

- [bot.js](https://tokay.io/examples/src/node-discord-bot/bot.js)
- [package.json](https://tokay.io/examples/src/node-discord-bot/package.json)

## Download and source

- [Download the complete project](https://tokay.io/examples/downloads/node-discord-bot.zip)
- Last updated: 2026-07-31
- Example revision: `52c7b7f`
- [View this revision on GitHub](https://github.com/tokayio/tokay-examples/tree/52c7b7ffd52e157375b08f1f11ff4fbe6c498642/node-discord-bot)
- [Host a Discord bot for free](https://tokay.io/use-cases/host-discord-bot)
