Step 1: Setting Up Your Python Discord Bot

How to Set Up Your First Python Discord Bot: A Beginner’s Guide

Have you ever been in a Discord server and thought, “Wow, this bot is cool—I wish I could build something like that!”? Maybe you’ve wanted a bot to greet new members, moderate chats, or even play music. The good news? Creating your own Discord bot with Python is easier than you think—even if you’re a beginner.

In this guide, we’ll walk through setting up your first Discord bot step by step, from registering it on Discord’s Developer Portal to writing a simple “Hello, World!” script. By the end, you’ll have a working bot and the foundation to add more advanced features. Let’s dive in!


Step 1: Create Your Bot on Discord’s Developer Portal

Before writing code, you need to register your bot with Discord:

  1. Go to the Discord Developer Portal and log in.
  2. Click “New Application”, name your bot (e.g., “MyCoolBot”), and agree to the terms.
  3. Navigate to the “Bot” tab on the left sidebar and click “Add Bot”. Confirm when prompted.
  4. Under the “Token” section, click “Copy” to save your bot’s secret token. ⚠️ Keep this private! (If leaked, anyone can control your bot.)

Pro Tip: Enable the “Presence Intent” and “Server Members Intent” if your bot will track user activity or roles.


Step 2: Install Python and discord.py

Next, set up your coding environment:

  1. Install Python (if you haven’t already) from python.org.
  2. Open your terminal or command prompt and install the discord.py library:

    pip install discord.py
    
  3. Verify the installation by running python --version and pip show discord.py.


Step 3: Write Your First Bot Script

Let’s create a bot that responds to a command. Open a text editor (like VS Code) and save this as bot.py:

import discord

# Set up the bot client
client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print(f'Logged in as {client.user}!')

@client.event
async def on_message(message):
    if message.author == client.user:  # Ignore the bot’s own messages
        return

    if message.content.startswith('!hello'):
        await message.channel.send('Hello, World!')

# Replace 'YOUR_TOKEN' with the bot token you copied earlier
client.run('YOUR_TOKEN')

What this code does:

  • The bot logs in and prints a confirmation message.
  • When a user types !hello, it replies with “Hello, World!”.

Step 4: Invite Your Bot to a Server

Your bot needs permission to join a server:

  1. Go back to the Developer Portal > OAuth2 > URL Generator.
  2. Under Scopes, select “bot”.
  3. Under Permissions, check “Send Messages” (and others as needed).
  4. Copy the generated URL, paste it into your browser, and pick a server to invite the bot to.

Note: You’ll need “Manage Server” permissions in the server you’re adding it to.


Step 5: Run Your Bot!

In your terminal, navigate to the folder containing bot.py and run:

python bot.py

If everything works, you’ll see Logged in as [YourBotName]! in the terminal. Test it by typing !hello in your Discord server.


What’s Next? Level Up Your Bot

Now that your bot is alive, try adding:

  • Custom commands (e.g., !joke, !weather).
  • Moderation tools (kick/ban users, filter spam).
  • Fun features (memes, quizzes, or music).

The discord.py documentation is a great resource for advanced features.


Final Thought

Building a Discord bot is a fun way to learn Python and automate tasks. What’s the first feature you’d add to your bot? A meme generator? A poll system? Share your ideas below!

Happy coding! 🤖


Call to Action:

  • Stuck? Ask for help in the discord.py server.
  • Want more? Comment with your bot’s purpose—we might feature it in a follow-up guide!
Why Every Discord Server Needs a Python Bot