A Discord bot running on your computer disconnects as soon as you switch it off, and your members see its status turn grey. On a VPS, it stays connected day and night, restarts by itself after an error and no longer depends on your home connection. This guide goes from creating the bot to running it permanently, in Python with discord.py, with the discord.js equivalent.
1. Create the bot in the Developer Portal
- Open the Discord Developer Portal and create an application.
- In the Bot tab, generate the token ("Reset Token") and copy it. It will not be shown again: treat it like a password.
- Still in Bot, only enable privileged intents if you need them. Message Content is only needed to read message text; a slash-command bot does not need it.
- In OAuth2, generate an invite link with the
botandapplications.commandsscopes and only the permissions you need. Open it to add the bot to your server.
2. Prepare the VPS
On a Debian or Ubuntu VPS, logged in as root, install Python and create a dedicated user: the bot should never run as root.
apt update && apt install -y python3 python3-venv
adduser --disabled-password --gecos "" bot
su - bot
mkdir mybot && cd mybot
python3 -m venv venv
venv/bin/pip install discord.py
3. Your first bot
Create bot.py. This bot answers the /ping slash command:
import os
import discord
from discord import app_commands
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name="ping", description="Check that the bot responds")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message("pong")
@client.event
async def on_ready():
await tree.sync()
print(f"Logged in as {client.user}")
client.run(os.environ["DISCORD_TOKEN"])
Test it once by hand:
DISCORD_TOKEN="YOUR_TOKEN" venv/bin/python bot.py
The command can take a few minutes to appear in Discord after the first sync. Then stop the bot with Ctrl+C.
4. Keep it running with systemd
Back as root, store the token in a file only root can read:
install -m 600 /dev/null /etc/mybot.env
echo 'DISCORD_TOKEN=YOUR_TOKEN' > /etc/mybot.env
Create /etc/systemd/system/mybot.service:
[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target
[Service]
User=bot
WorkingDirectory=/home/bot/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/home/bot/mybot/venv/bin/python bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now mybot
journalctl -u mybot -f # live log
The bot now restarts after an error and after a VPS reboot. After changing the code: systemctl restart mybot.
5. With discord.js
The method is the same with Node.js: install a recent LTS version of Node.js, then run npm install discord.js in the bot folder. In the service, replace the ExecStart line with ExecStart=/usr/bin/node index.js. PM2 is a common alternative to systemd for Node.js projects.
6. Ports, intents and common errors
- No port to open: the bot connects to Discord itself. Keep the firewall closed unless you add a website or a dashboard.
- Privileged intents error at startup: the intent requested in the code is not enabled in the portal, or the other way round.
- The bot cannot see message text: that is the Message Content intent. Prefer slash commands, which do not need it.
- Beyond 2,500 servers, Discord requires sharding: discord.py handles it with
AutoShardedClient.
7. Which VPS for your bot
| Type of bot | RAM |
|---|---|
| Moderation, commands, tickets on a few servers | 1 to 2 GB |
| Database, web dashboard, several bots | 2 to 4 GB |
| Music with ffmpeg or Lavalink | 4 GB and up |
| Bot connected to a local AI model (Ollama) | 8 to 16 GB |
One VPS can host several bots, with one service and one token per bot. See our Discord bot VPS page, or the Ollama VPS if your bot uses a local AI model.
Frequently asked questions
Do I need a VPS to host a Discord bot?
Not for development: your computer is enough. For a bot that is always online, you need a machine that is always on and connected, which is what a VPS provides.
How much RAM does a Discord bot need?
1 to 2 GB is enough for a moderation or command bot. Allow 4 GB and up for a music bot, and more for several bots on the same server.
Do I need to open a port on the VPS?
No. The bot opens the connection to Discord itself. A port is only needed if you add a website, an API or a dashboard.
Why does my slash command not appear?
Check that the invite link included the applications.commands scope and that the sync ran at startup. A global command can take a few minutes to appear.










