Why Build a Discord Bot?

Discord bots are among the most popular Python projects for good reason — they're immediately useful, highly visible, and genuinely fun to build. Whether you want to moderate a server, play music, run games, or automate announcements, Python makes it accessible to developers of all skill levels.

What You'll Need

  • A Discord account and a server where you have admin rights
  • Python 3.10+ installed
  • The discord.py library (pip install discord.py)
  • A bot token from the Discord Developer Portal

Step 1: Register Your Bot on Discord

  1. Go to discord.com/developers/applications
  2. Click New Application and give it a name
  3. Navigate to the Bot tab and click Add Bot
  4. Under Token, click Reset Token and copy it — keep this secret!
  5. Under OAuth2 → URL Generator, select bot and applications.commands scopes, then choose permissions and copy the generated URL to invite the bot to your server

Step 2: Your First Bot Script

Create bot.py and add this starter code:

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} (ID: {bot.user.id})")

@bot.command()
async def hello(ctx):
    await ctx.send(f"Hey {ctx.author.mention}! 👋")

bot.run("YOUR_BOT_TOKEN_HERE")

Run it with python bot.py. In Discord, type !hello and your bot should greet you.

Step 3: Adding Slash Commands

Modern Discord bots use slash commands for better discoverability. Here's how to add one:

from discord import app_commands

@bot.tree.command(name="ping", description="Check if the bot is alive")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("Pong! 🏓")

@bot.event
async def on_ready():
    await bot.tree.sync()
    print(f"{bot.user} is online!")

Step 4: Organizing with Cogs

As your bot grows, use Cogs to keep your code organized. Each Cog is a class that groups related commands:

# cogs/moderation.py
from discord.ext import commands

class Moderation(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member: discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.send(f"{member} has been kicked.")

async def setup(bot):
    await bot.add_cog(Moderation(bot))

Load it in your main file: await bot.load_extension("cogs.moderation")

Key Concepts Recap

ConceptWhat It Does
IntentsControl what events your bot can receive
CommandsPrefix-based actions (e.g., !help)
Slash CommandsNative Discord UI commands via /
EventsRespond to things like messages, joins, reactions
CogsModular groupings of commands and listeners

Security Tips

  • Never hardcode your token — use environment variables or a .env file with python-dotenv
  • Always validate permissions before executing sensitive commands
  • Add rate limiting to prevent spam abuse

With these fundamentals in place, you can expand your Discord bot to do virtually anything — from running mini-games to integrating AI responses with OpenAI's API.