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

# How to keep your Discord bot online 24/7

> How to keep your Discord bot online 24/7 without leaving your PC on. Step by step for discord.js and discord.py, with the token in an environment variable.

Your bot works on your PC, but it goes offline when you close the terminal or shut down the computer. This guide moves the bot to Hyze Cloud, where it runs without depending on your machine. It covers bots built with discord.js (Node.js) and discord.py (Python).

> [!NOTE]
>
> A Discord bot does not need a port or a website. Hyze recognizes discord.js and discord.py from your dependencies and considers the bot live once the process stays up.

## Before you start

* The bot already starts on your computer with a single command, such as `node index.js` or `python bot.py`.
* You have the bot token. It lives in the [Discord Developer Portal](https://discord.com/developers/applications), under **Bot → Reset Token**.
* You have a Hyze Cloud account. Every sign-up gets a 7-day trial.

## 1. Prepare your code

Hyze reads your dependency manifest to find the runtime and the command that starts the bot. Lay out your files like this:

**discord.js (Node.js)**

```text theme={"system"}
my-bot/
├── index.js
├── package.json
└── package-lock.json
```

```json package.json theme={"system"}
{
  "name": "my-bot",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "discord.js": "^14.16.0"
  }
}
```

```js index.js theme={"system"}
const { Client, Events, GatewayIntentBits } = require("discord.js");

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, (c) => {
  console.log(`Online as ${c.user.tag}`);
});

client.login(process.env.DISCORD_TOKEN);
```

* With a `start` script, Hyze starts the bot with `npm run start`.
* Without it, Hyze looks for an entry file such as `index.js`, `main.js` or `bot.js`.
* Every library the bot imports must be listed in `dependencies` (or `devDependencies`).
* Include `package-lock.json`. It makes the installed versions match your PC.

> [!TIP]
>
> Using Bun? Include `bun.lock`. Hyze picks the Bun runtime, which runs `.ts` files directly. The Node.js runtime does not run TypeScript: compile first or use Bun.

**discord.py (Python)**

```text theme={"system"}
my-bot/
├── bot.py
└── requirements.txt
```

```text requirements.txt theme={"system"}
discord.py>=2.4
```

```python bot.py theme={"system"}
import os

import discord

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print(f"Online as {client.user}")


client.run(os.environ["DISCORD_TOKEN"])
```

* Hyze installs what is in `requirements.txt` and starts the bot with `python bot.py`.
* With no command set, Hyze looks for `main.py`, `app.py`, `index.py` or `bot.py` at the root, in that order.
* Forks such as `py-cord`, `nextcord` and `disnake` are also recognized as Discord bots.
* `print` output shows up in the logs right away: Hyze already sets `PYTHONUNBUFFERED=1`.

## 2. Keep the token out of your code

The token is the bot's password. Whoever has it controls the bot. So it never goes in your code or your repository: the bot reads it from the `DISCORD_TOKEN` environment variable.

To test on your PC, create a `.env` file and add it to `.gitignore`:

```bash .env theme={"system"}
DISCORD_TOKEN=paste-the-token-here
```

```bash Node.js 20.6+ theme={"system"}
node --env-file=.env index.js
```

```python Python (python-dotenv) theme={"system"}
# pip install python-dotenv
# At the top of bot.py. Without a .env file (as on Hyze), it does nothing.
from dotenv import load_dotenv

load_dotenv()
```

On Hyze, you add `DISCORD_TOKEN` to the app's **Environment variables** (step 3). The `.env` file does not need to be uploaded.

> [!WARNING]
>
> If the token has ever been pushed to GitHub or pasted in a chat, generate a new one under **Bot → Reset Token**. The old token stops working immediately.

## 3. Deploy from GitHub or a ZIP

**1. Create the app**

In the [dashboard](https://hyzecloud.com/en-us/dashboard), click **New project** and choose **Import from GitHub** or **ZIP file**.

* **GitHub**: connect your account and pick the repository and branch. With **Automatic deployment** on, every push publishes a new version.
* **ZIP**: files go at the root of the ZIP, not inside a folder. Leave out `node_modules`, `.venv` and `.env`.

**2. Check the configuration**

* **Memory**: 256 MB is the minimum per app and is usually enough for a small bot.
* **Public access**: leave it off. A bot does not serve HTTP.
* **Start command**: leave it on **Automatic**. Use **Custom** only if the bot starts with a different command, such as `node src/index.js`.
* **Automatic restart**: on by default. Keep it that way.

**3. Add the token**

Under **Environment variables**, add the `DISCORD_TOKEN` key with the bot token. If you already have a `.env` file, use **Import .env**.

**4. Deploy**

Click **Deploy application**. Hyze installs the dependencies, starts the bot and watches the process.

> [!TIP]
>
> Prefer automation? The same deploy works through the API. See [Deploy an app](https://docs.hyzecloud.app/en/guides/deploy-an-app.md) and [Deploy from GitHub](https://docs.hyzecloud.app/en/guides/deploy-from-github.md).

## 4. Check the logs

Open the app and go to **Observability → Console**. Logs stream in real time, just like your PC's terminal. Look for the `Online as ...` line: once it shows up, the bot is connected to Discord.

If the deploy failed before the bot started, the reason is in the **Deployments** tab, under **Logs** for that deploy.

Stop the copy of the bot on your PC. With the same token running in two places, the bot may answer every command twice.

## 5. What happens when the bot crashes

With **Automatic restart** on:

* **The process died from an error**: Hyze restarts the bot on its own.
* **The machine rebooted**: the bot comes back when the machine is up again.
* **The bot died more than 3 times in 10 minutes**: Hyze turns the app off and sends you a notification. Restarting in a loop does not fix anything: read the error in the **Console**, fix it and deploy again.
* **The bot went over its memory limit**: Hyze stops the app, without a restart loop, and lets you know. Raise the **Memory** in **Settings** and start the app again.

In discord.js, a rejected promise with no `catch` takes down the whole process. Register a handler so the error goes to the log without taking the bot with it:

```js index.js theme={"system"}
client.on(Events.Error, (error) => console.error(error));
process.on("unhandledRejection", (error) => console.error(error));
```

In discord.py, an error inside an event already goes to the log without taking the bot down.

Seeing an error with a strange name in the log? [Common errors when hosting a Discord bot](https://docs.hyzecloud.app/en/guides/discord-bot-erros-comuns.md) covers the cause and fix for the most frequent ones.

## Pricing

Every sign-up gets a 7-day trial. To keep the bot online after that, the Starter plan costs R\$ 5.99 per month, paid in Brazilian reais via Pix. Plan memory is shared across your apps, so several small bots fit in the same plan.

## Put your bot online

> **[Open the dashboard](https://hyzecloud.com/en-us/dashboard)**
>
> Create the app, add `DISCORD_TOKEN` and run your first deploy.

> **[Bot stops when I close the terminal](https://docs.hyzecloud.app/en/guides/discord-bot-desliga-terminal.md)**
>
> Why it happens and how the options compare.


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