Skip to content

Build Your Own Discord Bot with discord.py

Learn how to write your own Discord bot with Python and discord.py, then deploy it on ZapX.

12 November 20263 min read11 Views0 helpful

Introduction

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.

Preparation

Before you start coding, you'll need a bot application on Discord:

  1. Open the Discord Developer Portal and click New Application. Choose a name for your bot.
  2. 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.
  3. 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).
  4. 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.

Write the Bot in Python

First, install discord.py in your development environment:

bash
pip install -U discord.py

Create a main.py file and set up the foundation for your bot:

python
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:

python
@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}!")

Deploy the Bot on ZapX Discord Bot Hosting

  1. Create a new Discord Bot service in the ZapX Customer Panel if you haven't already.
  2. During configuration, select Python 3.12 as the runtime.
  3. Upload your main.py file (and, if available, a requirements.txt file with additional packages) to the service directory using either the built-in file manager or file upload.
  1. 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_TOKEN environment variable.
5. Start the service. If everything is working correctly, your bot will appear in the log as Bot ist online als ... and show as "Online" on your Discord server.

Troubleshooting

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.

Conclusion

You've now written a working Discord bot with discord.py, added a custom slash command, and deployed it on your ZapX Discord Bot Hosting. From here, you can extend the bot with additional commands, moderation features, or integrations.

FAQ

Frequently asked questions

Create a bot application in the Discord Developer Portal. Create a New Application, get the token from the Bot tab using 'Reset Token', and generate an invite link for your server in the OAuth2 URL Generator using the 'bot' and 'applications.commands' scopes.
The token isn't stored in the code. It's read from the DISCORD_TOKEN environment variable ('os.environ["DISCORD_TOKEN"]'). In ZapX Discord Bot Hosting, enter it in the Settings tab under Bot Configuration -> Discord Bot Token.
Python 3.12.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.