How to Build a Discord Bot With discord.py From Zero
🔍 WiseChecker

How to Build a Discord Bot With discord.py From Zero

You want to create a Discord bot but have no coding experience. A Discord bot can automate tasks like moderating chat, playing music, or sending welcome messages. The discord.py library is the most popular Python framework for building bots. This guide walks you through installing Python, setting up discord.py, registering your bot with Discord, and writing your first working bot script.

You do not need prior programming knowledge. Each step is explained with plain language and exact commands. By the end, you will have a bot that responds to a simple command in your own server.

Key Takeaways: Building Your First Discord Bot

  • Python 3.8+ installation: Required before you can install discord.py with pip.
  • Discord Developer Portal > Applications > New Application: Register your bot and get its token.
  • Bot > Privileged Gateway Intents: Enable MESSAGE CONTENT INTENT so your bot can read messages.
  • OAuth2 > URL Generator > bot scope + Send Messages permission: Generate an invite URL to add the bot to your server.
  • client.run(‘YOUR_TOKEN’): The line that starts your bot and keeps it online.

What Is discord.py and Why Use It?

discord.py is a Python library that handles all the low-level networking with Discord’s API. Instead of writing raw HTTP requests, you use Python objects and events. The library supports commands, slash commands, voice connections, and background tasks.

The minimum Python version is 3.8. discord.py works on Windows, macOS, and Linux. You need a Discord account and a server where you have the Manage Server permission to add a bot.

Prerequisites Before You Start

You must install Python and a text editor. Visual Studio Code is recommended because it has a built-in terminal and Python support. Download Python from python.org and check the box that says “Add Python to PATH” during installation.

Steps to Build and Run Your First Discord Bot

Step 1: Install discord.py

  1. Open a terminal or command prompt
    On Windows, press Windows+R, type cmd, and press Enter. On macOS, open Terminal from Applications > Utilities.
  2. Run the pip install command
    Type pip install discord.py and press Enter. Wait for the installation to finish. If you get a “pip not found” error, use python -m pip install discord.py instead.

Step 2: Create a Bot on the Discord Developer Portal

  1. Go to the Discord Developer Portal
    Open your browser and go to https://discord.com/developers/applications. Log in with your Discord account.
  2. Click New Application
    Enter a name for your bot, such as “MyFirstBot”. Accept the terms and click Create.
  3. Go to the Bot page
    In the left sidebar, click Bot. Then click Add Bot and confirm by clicking Yes, do it!.
  4. Copy your bot token
    Under the bot username, click Reset Token. Copy the new token immediately. Keep this token secret. Anyone with the token can control your bot.
  5. Enable the Message Content Intent
    On the same Bot page, scroll to Privileged Gateway Intents. Turn on MESSAGE CONTENT INTENT. Click Save Changes.

Step 3: Invite the Bot to Your Server

  1. Go to OAuth2 > URL Generator
    In the left sidebar, click OAuth2, then URL Generator.
  2. Select the bot scope
    Under Scopes, check the box for bot.
  3. Select permissions
    Under Bot Permissions, check Send Messages and Read Message History. For a basic bot, these two permissions are enough.
  4. Copy the generated URL
    Scroll to the bottom of the page. Copy the URL in the Generated URL box. Open a new browser tab, paste the URL, and press Enter.
  5. Select your server and authorize
    Choose your server from the dropdown list. Click Continue, then Authorize. Complete the CAPTCHA if prompted.

Step 4: Write Your First Bot Script

  1. Create a new Python file
    Open Visual Studio Code or any text editor. Create a new file and save it as bot.py in an empty folder.
  2. Write the bot code
    Copy and paste the following code into bot.py:

    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'Logged in as {bot.user}' )

    @bot.command()
    async def hello(ctx):
    await ctx.send('Hello! I am a bot.')

    bot.run('YOUR_TOKEN_HERE')

  3. Replace the token
    In the last line, replace 'YOUR_TOKEN_HERE' with the token you copied from the Developer Portal. Keep the single quotes.

Step 5: Run the Bot

  1. Open a terminal in your bot folder
    In Visual Studio Code, go to Terminal > New Terminal. Or use the terminal outside and navigate to the folder where bot.py is saved.
  2. Run the bot script
    Type python bot.py and press Enter. You should see the message “Logged in as MyFirstBot#1234” appear in the terminal.
  3. Test the bot in Discord
    Go to any text channel in your server where the bot has access. Type !hello and press Enter. The bot should reply with “Hello! I am a bot.”

Common Mistakes and Things to Avoid

Bot Does Not Respond to Commands

The most common cause is missing the Message Content Intent. Go back to the Bot page in the Developer Portal and confirm MESSAGE CONTENT INTENT is enabled. Also verify that the bot has the Send Messages permission in the channel.

Token Not Working or Invalid

If you get a “Invalid token” error, the token was copied incorrectly. Reset the token on the Bot page and copy it again. Make sure there are no extra spaces or line breaks. The token should be a long string of characters inside single quotes.

Bot Goes Offline After Closing the Terminal

When you close the terminal window, the bot stops running. To keep the bot online 24/7, you need to host it on a cloud server. Services like Heroku, Railway, or a cheap VPS can run your bot script continuously.

Command Prefix Conflicts With Other Bots

If another bot in your server also uses the ! prefix, commands may conflict. Change your prefix to something unique like $ or ? in the commands.Bot(command_prefix='$') line.

discord.py vs Other Bot Libraries

Item discord.py discord.js
Language Python JavaScript / Node.js
Learning curve Low to medium Medium to high
Slash command support Yes, via discord.app_commands Yes, built-in
Voice support Yes, via FFmpeg Yes, via @discordjs/voice
Community size Large Very large

You now have a working Discord bot that responds to the !hello command. Next, try adding more commands like !ping to check latency or !say to repeat a message. The commands.Bot class also supports slash commands by using bot.tree.command. For advanced features, explore the discord.py documentation on event listeners and background tasks.