A Discord bot that responds to commands, greets new members, returns random jokes, and shows server stats — running 24/7 from your own machine.
Register your bot application and get a token.
# Steps to create your bot:
# 1. Go to https://discord.com/developers/applications
# 2. Click "New Application" and give it a name
# 3. Go to the "Bot" tab on the left
# 4. Click "Add Bot" then "Reset Token" — copy this token (keep it secret!)
# 5. Under "Privileged Gateway Intents", enable:
# - Presence Intent
# - Server Members Intent
# - Message Content Intent
# 6. Go to "OAuth2 > URL Generator"
# - Scopes: bot
# - Bot Permissions: Send Messages, Read Messages, Read Message History
# 7. Copy the generated URL and open it to invite the bot to your server
Write the basic bot script that connects to Discord and logs in.
# bot.py
import discord
TOKEN = 'YOUR_BOT_TOKEN_HERE' # paste your token
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user} (ID: {client.user.id})')
print('Bot is online!')
client.run(TOKEN)
Logged in as MyBot#1234 (ID: 123456789012345678) Bot is online!
Make the bot reply when someone types a command starting with !
import random
JOKES = [
"Why do programmers prefer dark mode? Because light attracts bugs!",
"How many programmers does it take to change a light bulb? None — that's a hardware problem.",
"Why do Java developers wear glasses? Because they don't C#.",
]
@client.event
async def on_message(message):
if message.author == client.user:
return # ignore messages from the bot itself
if message.content.lower() == '!hello':
await message.channel.send(f'Hello, {message.author.name}!')
elif message.content.lower() == '!joke':
await message.channel.send(random.choice(JOKES))
elif message.content.lower() == '!ping':
latency = round(client.latency * 1000)
await message.channel.send(f'Pong! Latency: {latency}ms')
User: !hello Bot: Hello, John! User: !joke Bot: Why do programmers prefer dark mode? Because light attracts bugs! User: !ping Bot: Pong! Latency: 42ms
Listen for the on_member_join event and send a welcome message.
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.text_channels, name='general')
if channel:
await channel.send(
f'Welcome to the server, {member.mention}! '
f'We now have {member.guild.member_count} members.'
)
Return server information when someone types !stats.
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.lower() == '!hello':
await message.channel.send(f'Hello, {message.author.name}!')
elif message.content.lower() == '!joke':
await message.channel.send(random.choice(JOKES))
elif message.content.lower() == '!ping':
latency = round(client.latency * 1000)
await message.channel.send(f'Pong! Latency: {latency}ms')
elif message.content.lower() == '!stats':
guild = message.guild
embed = discord.Embed(title=f'{guild.name} Stats', color=0x5865F2)
embed.add_field(name='Members', value=guild.member_count, inline=True)
embed.add_field(name='Channels', value=len(guild.channels), inline=True)
embed.add_field(name='Roles', value=len(guild.roles), inline=True)
embed.set_thumbnail(url=guild.icon.url if guild.icon else None)
await message.channel.send(embed=embed)
User: !stats
Bot: [Embed card showing]
Server Name Stats
Members: 47 Channels: 12 Roles: 8Your bot is live on Discord. Keep the terminal open to keep it running. For 24/7 uptime without leaving your PC on, deploy it to a free service like Railway or Render — both support Python bots on their free tier.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.