Discord Bot in 50 Lines of Python
I'll show you how to build a working Discord bot that responds to commands
in less than 5 minutes.
What You Need
pip install discord.py python-dotenv
The Code
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
@bot.event
async def on_ready():
print(f"{bot.user} is online!")
@bot.command(name="ping")
async def ping(ctx):
await ctx.send(f"Pong! {round(bot.latency * 1000)}ms")
@bot.command(name="hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.name}!")
bot.run(TOKEN)
Steps to Deploy
- Create a Discord app at discord.com/developers
- Generate a token
- Invite bot to your server
- Run the code
- Type
!pingin Discord
That's it. Now extend with your own commands.