Deploying a discord.py Bot on Pterodactyl
How to deploy Python discord.py, Pycord, and Disnake bots 24/7 on VisiHost's Pterodactyl panel with virtual environments and environment variables.
Deploying a Python Discord bot built with discord.py, Pycord, or Disnake on VisiHost is quick and isolated inside a dedicated Linux container. This guide walks you through setting up dependencies, configuring your bot token, and launching your server.
Prerequisites#
Before deploying, ensure you have:
- An active container from Free Bot Hosting or Paid Discord Bot Hosting.
- Python compatibility (VisiHost containers support Python 3.10, 3.11, and 3.12).
- Your bot token from the Discord Developer Portal.
Deployment Steps#
Step 1: Create a requirements.txt File#
List every library your bot depends on so the container can install them cleanly:
discord.py>=2.3.2
python-dotenv>=1.0.1
aiohttp>=3.9.5Security Reminder: Never upload your local
.envfile containing confidential bot tokens to public git repositories or shared folders. Set sensitive tokens through your container's Startup tab instead.
Step 2: Upload Files via File Manager or SFTP#
- Log into your dashboard at paid.visihost.in.
- Select your Python bot container.
- Open the Files tab.
- Upload your Python scripts (such as
bot.pyormain.py, along with any cogs or helper folders) and yourrequirements.txtfile.
You can also transfer files via SFTP using credentials found in the Settings tab.
Step 3: Configure Startup Command & Environment Variables#
- Navigate to the Startup tab in your container dashboard.
- Confirm the Python Main File matches your entry script (e.g.,
bot.pyormain.py). - Under Environment Variables, configure your token:
- Variable Name:
DISCORD_TOKEN - Value:
YourDiscordBotTokenHere
- Variable Name:
Read the token safely inside your Python code using os.getenv:
import os
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Online! Logged in as {bot.user} (ID: {bot.user.id})")
token = os.getenv("DISCORD_TOKEN")
if not token:
raise ValueError("DISCORD_TOKEN environment variable is not configured!")
bot.run(token)Step 4: Install Dependencies and Start#
- Open the Console tab.
- The Python container environment automatically executes
pip install -r requirements.txtinto an isolated virtual environment upon initial start. - Click the green Start button in the header.
- Review the console stream to confirm your bot connects to the Discord gateway.
Troubleshooting Common Python Issues#
1. ModuleNotFoundError: No module named 'package_name'#
- Cause: A library used by your bot code is missing from
requirements.txt. - Solution: Open the Files tab, add the package name to
requirements.txt, save the file, and restart the container from the Console tab.
2. LoginFailure: Improper token has been passed#
- Cause: The token entered in the Startup tab is invalid, has expired, or includes extraneous quotation marks or spaces.
- Solution: Re-copy the token from the Discord Developer Portal, paste it into the
DISCORD_TOKENfield in the Startup tab without quotes, and restart.
3. Loop / Thread Blocking#
- Cause: Running synchronous time-blocking functions (like
time.sleep()or synchronous database queries) freezes the Discord event loop, causing WebSocket heartbeats to fail. - Solution: Replace synchronous operations with asynchronous equivalents like
await asyncio.sleep()or run blocking operations insideasyncio.to_thread().
Frequently Asked Questions (FAQ)#
Why aren't my print statements appearing immediately in the console?#
Python buffers standard output (stdout) by default. To see live log output instantly, make sure your startup command includes the unbuffered flag: python3 -u main.py.
What Python versions are available?#
VisiHost Python containers support Python 3.10, 3.11, and 3.12. You can verify and select your preferred Python runtime version in the container Startup tab.
Can I install Pycord or Disnake instead of discord.py?#
Yes! Simply list py-cord or disnake in your requirements.txt file instead of discord.py. Note: do not list multiple conflicting Discord API wrappers in the same file.
