• /
    • Build a Basic Discord Bot

Build a Basic Discord Bot

A beginner-friendly guide to building your first Discord bot using Python and Replit.

Last modified 2025-06-03

Mohammed Fahad

Table of content
  1. 🧰 Requirements
  2. πŸ› οΈ Setup Instructions
  3. πŸ€– Code Walkthrough
  4. πŸ“¦ Full Code
  5. πŸ§ͺ Try it Out

In this guide, you'll learn how to build a simple bot using Python, Replit, and Discord Developer Portal.


🧰 Requirements


πŸ› οΈ Setup Instructions

1. Create the Python environment

2. Enable Developer Mode in Discord

3. Create a Discord Bot Application

4. Invite Your Bot to a Server

5. Enable Message Intent


πŸ€– Code Walkthrough

Here’s the full code, explained step by step:

1. Import modules

import discord
import random

2. Add your token

TOKEN = "PASTE_YOUR_TOKEN_HERE"

3. Set up intents and client

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

4. Event: Bot is ready

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

5. Event: Bot responds to messages

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    content = message.content.lower()
    print(f"{message.author.name} said: '{message.content}'")

    if content.startswith("!hello"):
        await message.channel.send("Hello!")
    elif content.startswith("!roll"):
        number = random.randint(1, 6)
        await message.channel.send(f"🎲 You rolled a {number}!")
    elif content.startswith("bye"):
        await message.channel.send(f"πŸ‘‹ Goodbye {message.author.name}!")

6. Start the bot

client.run(TOKEN)

πŸ“¦ Full Code

import discord
import random

TOKEN = "PASTE_YOUR_TOKEN_HERE"

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    content = message.content.lower()
    print(f"{message.author.name} said: '{message.content}'")

    if content.startswith("!hello"):
        await message.channel.send("Hello!")
    elif content.startswith("!roll"):
        number = random.randint(1, 6)
        await message.channel.send(f"🎲 You rolled a {number}!")
    elif content.startswith("bye"):
        await message.channel.send(f"πŸ‘‹ Goodbye {message.author.name}!")

client.run(TOKEN)

πŸ§ͺ Try it Out

Type !hello, !roll, or bye in any server your bot is part of and see it reply!