Sign inCreate server
Apps & bots

How to Host a Python Discord Bot (discord.py) 24/7

Host your discord.py bot 24/7: deploy from Git or upload files, store the token as a secret variable, invite the bot and verify it live in the panel.

6 min read#discord bot python#discord.py#host python discord bot#bot de discord python#alojar bot de discord 24/7#deploy discord bot#python bot hosting

To host a Python Discord bot (discord.py) 24/7 on SolumeCore, put your project on a Python app plan, save the bot token as an encrypted variable, start the app and invite the bot to your server. The panel's Discord bot assistant verifies the token with Discord and builds the invite link for you, so your bot keeps running even when your computer is off.

Before you start

  • A SolumeCore app plan for Python. The available plans are on the programming page.
  • A bot project with a main file (the examples use main.py) and a requirements.txt.
  • A Discord account, to create the bot application in Discord's developer portal.

Step 1: Prepare your bot code

Read the token from an environment variable instead of writing it in the file. This sample bot answers a /ping slash command, which is a quick way to confirm everything works:

python
# main.py
import os

import discord
from discord import app_commands


class MyBot(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        await self.tree.sync()


bot = MyBot()


@bot.tree.command(name="ping", description="Check that the bot is alive")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("Pong!")


@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}", flush=True)


bot.run(os.environ["DISCORD_TOKEN"])

The matching requirements.txt:

text
discord.py>=2.3
✦
Tip

Install discord.py, not the older discord package. In your code you still write import discord.

If your bot reads message text, enable intents.message_content = True in the code and turn on Message Content Intent in the developer portal as well (step 3).

Step 2: Get your code onto the app

A Git repository is the most dependable route: you set the build and start commands yourself, and each update is one click.

  1. Open your app in the panel, go to the Code tab and open Deploy.
  2. Press Connect repository. Pick Public repository (HTTPS link) or Private repository (SSH link plus the read-only deploy key the wizard shows you).
  3. On the Commands step, set Build command to the Python suggestion the panel offers, pip install -U --prefix .local -r requirements.txt, and Start command to python -u main.py.
  4. Press Save and deploy.

The -u flag makes Python print each line immediately, so your messages show up in the console right away instead of arriving in bursts. The complete Git walkthrough, with private repositories and automatic deploys, is in Deploy an app from GitHub or GitLab.

Not using Git? Open Files, use Upload > Upload files (or SFTP) and place your project in the main folder. Without a repository the app starts with the default command of its Python environment; review what it runs under Settings > Startup. A requirements.txt in the main folder is installed when the app starts.

i
Note

The first start may fail with a missing-token error (KeyError: 'DISCORD_TOKEN'). That is normal: you save the token in the next step.

Step 3: Create the bot and save the token

  1. Under Code > Deploy, find the Discord bot assistant card in the quick start area and press Open assistant. You can also open it from Code > Variables > Import / export > Discord bot assistant.
  2. In Create the bot, open the Discord Developer Portal, press New Application and name your bot. On the Bot tab press Reset Token and copy the token.
  3. If the bot reads message content, turn on Message Content Intent on the same Bot tab.
  4. Paste the token into Bot token. It is stored encrypted as DISCORD_TOKEN, the variable the sample reads with os.environ["DISCORD_TOKEN"].
!
Warning

Treat the token like a password: keep it out of your code, your repository and any screenshot. If it ever leaks, press Reset Token in the portal and save the new one.

Step 4: Connect, invite and verify

  1. In the Connect step, wait for your bot's name and ID to appear. Fix any red check: press Restart if the app doesn't have the token yet, or Start if it is stopped, then Check again.
  2. In the Invite step, choose Basic, Moderation, Music or Administrator and press Invite to my server. Authorize the bot on your Discord server and press Done.
  3. Open Console > Live console. You should see Logged in as ..., and your bot appears online in Discord.
  4. Type /ping in your server. The bot answers Pong!. Slash commands can take a moment to show up the first time.

Choose the Python version

Your app runs the Python version selected under Code > Packages > Language version. Pick a current Python 3 version and restart the app to apply it. The Packages screen also lists the libraries in your requirements.txt and lets you add, update or remove them; if your app deploys from Git, make the change in your repository so the next deploy doesn't overwrite it.

Troubleshooting

  • ModuleNotFoundError: No module named 'discord': the libraries weren't installed. Check that requirements.txt is in the main folder and that Build command installs it, then deploy again.
  • KeyError: 'DISCORD_TOKEN': the app hasn't read the token. Press Restart to apply under Code > Variables, or check the variable name matches your code.
  • PrivilegedIntentsRequired: turn on the matching intent on the Bot tab of the developer portal and restart.
  • No output in the console: use python -u main.py as the start command, or flush=True in your print calls.
  • Still offline: see Why your Discord bot shows offline.

FAQ

How do I run a discord.py bot 24/7 without leaving my PC on?

Deploy it to a Python app on SolumeCore. The app runs on our servers around the clock, restarts when you ask it to and lets you watch its console from the panel.

Where do I store the bot token in a Python app?

Under Code > Variables, or through the Discord bot assistant, as DISCORD_TOKEN. Read it with os.environ["DISCORD_TOKEN"]; never hard-code it.

Can I use a .env file with python-dotenv?

You can, but environment variables are safer. If you need a .env file, the Variables screen has a switch to write one for you, with a warning: anyone with file access to the app can read it.

How do I update my bot after changing the code?

Push to your repository and press Deploy now under Code > Deploy, or turn on Deploy automatically on every push so it happens on its own.

Want your bot online around the clock? See the Python and Node.js app plans.

Was this article helpful?
How to Host a Python Discord Bot (discord.py) 24/7