Host a Discord bot 24/7
A Discord bot is a long-running process that keeps one connection open to Discord. It needs a machine that never sleeps and a way to restart it when it crashes; on an EnvHaven workspace an agent can write it and keep it running in the same place.
Updated November 23, 2026
Most Discord bots connect to Discord’s gateway and hold that connection open, listening for events. That shapes where they can live: anything that sleeps when idle, stops after a timeout or only runs on incoming HTTP requests drops the connection, and the bot goes offline.
What the bot needs
- An always-on process. The bot runs all the time and holds its connection open.
- A restart when it crashes. Discord disconnects happen; a restart loop or a supervisor brings the process back.
- The token kept out of the code. The bot reads it from an environment variable.
- Modest resources. A small bot fits in a few hundred megabytes, so the machine matters more for reliability than for size.
Running it on a workspace
On an EnvHaven workspace the whole loop happens in one place: an agent writes the bot, runs it, and it stays running.
- Save the bot token as
DISCORD_TOKENin the workspace’s Vars pane. It stays write-only, and every new shell gets it. - Choose an agent from the Launch menu, signing in the first time, and ask it for the bot: “Write a discord.py bot that answers /ping, reads the token from DISCORD_TOKEN, and restarts itself if it crashes.”
- Let the agent start the bot in a terminal window of its own, wrapped in a loop that restarts it after a crash. If you start it yourself, use a terminal window opened after step 1, so the token is there.
- Close the tab. The workspace has no idle shutdown, so the bot stays connected while you are away.
A process started in the terminal does not come back by itself after a workspace restart or update, so start the bot again after one. If the bot also serves a web dashboard or receives webhooks, bind that server to 0.0.0.0:3000 and it is live at the workspace’s preview address with HTTPS.
On a server you manage yourself
On any VPS the same bot runs under tmux, or as a systemd service with Restart=always, which also brings it back after a reboot. Keep the token in a .env file that is not committed. The SSH guide covers both ways to run it. What a workspace adds is the setup: the runtime, the agent that writes and fixes the bot, and the public address.