In this guide, you'll build your own Discord bot from scratch using Python and the discord.py library. You'll register the bot application in the Discord Developer Portal, write a working foundation with a custom slash command, and then deploy the finished bot on your ZapX Discord Bot Hosting. Basic knowledge of Python is helpful, but not required.
Before you start coding, you'll need a bot application on Discord:
- Open the Discord Developer Portal and click New Application. Choose a name for your bot.
- Select the Bot tab in the left-hand menu. Here, you can find your bot's Token using Reset Token. Copy the token and keep it safe—it grants full access to your bot and must never be shared in your code or publicly.
- On the same page, under Privileged Gateway Intents, enable Message Content Intent if your bot needs to respond to message content (for example, for text-based commands).
- Go to the OAuth2 → URL Generator tab. Select the bot scope and also applications.commands (for slash commands), then select the permissions your bot needs below. Open the generated link to invite the bot to your own Discord server.
First, install discord.py in your development environment:
pip install -U discord.py
Create a main.py file and set up the foundation for your bot:
import os
import discord
from discord import app_commands
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@client.event
async def on_ready():
await tree.sync()
print(f"Bot ist online als {client.user}")
@tree.command(name="hallo", description="Begrüßt dich mit einer kurzen Nachricht")
async def hallo(interaction: discord.Interaction):
await interaction.response.send_message(f"Hallo {interaction.user.mention}!")
client.run(os.environ["DISCORD_TOKEN"])
The token is intentionally not stored as a fixed string in the code. Instead, it's read from the DISCORD_TOKEN environment variable (os.environ["DISCORD_TOKEN"]). This keeps the token out of your code and is the basis for the deployment step below.
To also respond to new server members—for example, with a welcome message—add another event handler:
@client.event
async def on_member_join(member: discord.Member):
channel = discord.utils.get(member.guild.text_channels, name="willkommen")
if channel:
await channel.send(f"Willkommen auf dem Server, {member.mention}!")
- Create a new Discord Bot service in the ZapX Customer Panel if you haven't already.
- During configuration, select Python 3.12 as the runtime.
- Upload your
main.pyfile (and, if available, arequirements.txtfile with additional packages) to the service directory using either the built-in file manager or file upload.
- In the Settings tab, enter the bot token you created earlier under Bot Configuration → Discord Bot Token, then save the settings. This keeps your token separate from the code, so you won't need to update it when uploading files again. Your code can still access this value as the
DISCORD_TOKENenvironment variable.
Bot ist online als ... and show as "Online" on your Discord server.
The bot stays offline: Check that the token is correctly set in the DISCORD_TOKEN environment variable and look for an error message in the service log.
Slash commands aren't appearing: After tree.sync(), globally registered commands can take up to an hour to appear everywhere on Discord. To test them, sync the commands directly to a single server instead so they're available immediately.
"Missing Access" or similar permission errors: The bot doesn't have the required permissions on the server. Check the bot's role permissions and make sure you selected the appropriate permissions in the OAuth2 URL Generator when inviting it.