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
In this guide, you'll learn how to build a simple bot using Python, Replit, and Discord Developer Portal.
π§° Requirements
- A Replit account
- A Discord account
- Basic knowledge of Python syntax
π οΈ Setup Instructions
1. Create the Python environment
- Go to Replit
- Click Create Repl
- Choose Python template
2. Enable Developer Mode in Discord
- Open Discord β User Settings β Advanced
- Enable Developer Mode
3. Create a Discord Bot Application
- Go to Discord Developer Portal
- Click "New Application"
- Name your bot (e.g.,
HelloBot) - Go to Bot tab β Click Add Bot
- Click Reset Token β Copy the token (Youβll paste this into code)
4. Invite Your Bot to a Server
- Go to OAuth2 β URL Generator
- Check
botandapplications.commands - Under Bot Permissions, check
Administrator - Copy the generated URL and open it in a browser to invite your bot
5. Enable Message Intent
- In the Bot tab, scroll to Privileged Gateway Intents
- Enable Message Content 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, orbyein any server your bot is part of and see it reply!