ImperialHost.cc · Guides · Web hosting and bots
Run a Discord bot in Python or Node.js
A bot in Python or Node.js runs on web hosting around the clock, even when your computer is off. You create it on Discord, upload the code and keep the token in a .env file.
Before you start
- Web hosting with us with Python or Node.js. You pick it when you order.
- A Discord account and a server where you may add bots.
- The bot's code: in Python for example with the discord.py library, in Node.js with discord.js.
1. Create the bot on Discord
- Open the Discord Developer Portal at discord.com/developers/applications, click New Application, enter a name and confirm.
- On the left open the Bot page, click Reset Token and copy the token. It is shown only once.
- If the bot reads the text of messages (for example commands that start with an exclamation mark), turn on Message Content Intent on the same page. A bot with only slash commands doesn't need it.
- On the Installation page, in Default Install Settings, add the scopes
botandapplications.commandsunder Guild Install and pick the bot's permissions. Open the link from Install Link (Discord Provided Link) in your browser and add the bot to your server.
The token is the bot's password. Never send it to anyone, not even to us, and don't put it in code you upload, for example to GitHub. If it leaks, create a new one on the Bot page with Reset Token right away.
2. Get the code ready
- Python: the main file is
bot.py, and you list the libraries inrequirements.txt, one per line, for examplediscord.pyandpython-dotenv. - In
bot.pyload the token from the.envfile: at the topimport os,from dotenv import load_dotenvandload_dotenv(). Then start the bot, for example withclient.run(os.getenv("DISCORD_TOKEN")). - Node.js: the main file is
index.js, and the libraries are inpackage.jsonunderdependencies. Running, for example,npm install discord.json your computer puts them there. - Put
process.loadEnvFile()at the start ofindex.jsand sign the bot in, for example withclient.login(process.env.DISCORD_TOKEN). You don't need any extra package for this.
3. Upload the bot and start it
- In the client area open Services, pick your service and click Open game panel.
- On the Files tab delete the sample
bot.py(for Node.jsindex.jsandpackage.json): the three dots at the end of the row, Delete. Then click Upload and upload your files. Don't upload thenode_modulesfolder. - Click New File, type the line
DISCORD_TOKEN=your_tokenand click Create File. Type.envin File Name and confirm with Create File. - Is your main file named differently, for example
main.py? On the Startup tab enter it in BOT / APP FILE, for Node.js in MAIN FILE. - On the Console tab click Restart (the circular arrow at the top near the server's name). The server first installs the libraries, then starts the bot. On Discord the bot shows as online.
Troubleshooting
The bot says the token is invalid
Check that .env holds the whole token without spaces. After Reset Token only the new token works: put it in .env and restart the bot.
The bot is online but ignores messages
A bot that reads the text of messages needs Message Content Intent. Turn it on in the Developer Portal on the Bot page and allow it in the code too: in discord.py intents.message_content = True, in discord.js GatewayIntentBits.MessageContent. Then restart the bot.
The console says a module is missing
Python prints ModuleNotFoundError, Node.js Cannot find module. Add the library to requirements.txt or to dependencies in package.json and restart the bot. It is installed at start.
The panel keeps showing that the bot is starting
The top near the name shows Starting until the panel finds a word such as ready in the console log. Print it once the bot has signed in, for example print("Bot ready", flush=True) or console.log("Bot ready"). The bot runs normally in the meantime.
Still stuck?
Message us on Discord. The AI assistant Aurel answers first, and you can call an admin. Or open a ticket in the client area.