Hosting a Discord bot
Get your bot ready to move
A bot that runs on your own computer runs the same way on a server. Before you upload it, check three things:
- The token is not in your code. Keep it in a
.envfile or a config file next to your code, and read it from there. That way you can share or publish the code without leaking the token. - Your packages are listed. Node.js bots need a
package.json. Python bots need arequirements.txt. The server installs from these on every start. - You know which file starts the bot. Usually
index.jsfor Node.js ormain.pyfor Python.
Anyone with your token can control your bot. If it was ever pasted somewhere public or
pushed to a public Git repository, reset it in the Discord Developer Portal under
Bot, then put the new one in your .env.
Upload your files
Open your bot server in the panel and go to the File Manager. Upload
your code, your package.json or requirements.txt, and your
.env.
Don't upload node_modules or a Python virtual environment folder. They are big,
they were built for your computer, and the server makes its own on start.
For a lot of files, connect over SFTP with a program like FileZilla or WinSCP. The address, port and username are on the Settings tab, and the password is your panel password.
Tell the server which file to run
On the Startup tab, set the start file:
| Language | Setting | Default |
|---|---|---|
| Node.js | Main file | index.js |
| Python | App py file | main.py |
Press Start and watch the Console. You'll see your packages install, then your bot's own output, such as a "logged in" message. Your bot should show as online in Discord.
To change the Node.js or Python version, pick a different image from the Docker Image list on the same tab and restart.
Deploy from GitHub instead
If your code is in a Git repository, the server can pull it for you instead of you uploading files. On the Startup tab:
- Set Git Repo Address to your repository's HTTPS address.
- Set the branch if you don't use the default one. It is Install Branch for Node.js and Git Branch for Python.
- For a private repository, set Git Username and Git Access Token. Use a GitHub personal access token with read access to that repository, not your password.
- Set User Uploaded Files to
0, then reinstall the server from the Settings tab so it clones the repository. - Set Auto Update to
1to pull the latest code on every restart.
A reinstall with User Uploaded Files set to 0 empties the folder before it
clones. Download anything you want to keep first, such as a database file. Keep your
.env out of the repository and upload it again after the reinstall.
Intents and permissions
If your bot logs in but can't read messages or see members, the cause is almost always privileged intents. In the Discord Developer Portal, open your application, go to Bot, and turn on the ones your bot asks for in its code:
- Message Content Intent for bots that read what people type, such as prefix commands.
- Server Members Intent for welcome messages, member counts and role tools.
- Presence Intent only if the bot needs to see who is online or playing.
A bot that only uses slash commands usually needs none of these.
Saving data
Files your bot writes stay on the server between restarts. A JSON file or an SQLite database
file works well for most bots. Keep them in a folder like data/ so they are easy
to back up, and use the panel's Backups tab before big changes.
You don't need tricks like a small web server to "keep the bot awake". Those exist for free hosts that put bots to sleep. Your server keeps running until you stop it.
Troubleshooting
"Cannot find module" or "No module named"
The package isn't in your package.json or requirements.txt.
Add it there and restart. For a quick test you can also list packages in the
Additional Node packages or Additional Python packages setting.
"An invalid token was provided"
The bot can't find its token, or the token was reset. Check the .env
file was uploaded, its name starts with a dot, and the variable name matches what
your code reads.
The bot starts, then stops straight away
Your code finished or crashed. The last lines in the console show the error. A bot that ends with no error usually never called its login function.