Discord Bot in 50 Lines of Python

python dev.to

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Steps to Deploy

  1. Create a Discord app at discord.com/developers
  2. Generate a token
  3. Invite bot to your server
  4. Run the code
  5. Type !ping in Discord

That's it. Now extend with your own commands.

https://github.com/ezequiellich44-cmd/Web3

Source: dev.to

arrow_back Back to Tutorials