NEW 📢 Black Friday: our launch offer ends 30 Nov. Up to 70% off your first month on every game!See plans
Black FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53m

How to make a Discord bot with Python (discord.py)

Discord bot hostingUpdated Oct 29, 20265 min read

To make a Discord bot with Python, install discord.py, write a short bot.py that registers a slash command with app_commands.CommandTree, and run it with your bot token. This guide gives you the two files for a working /ping bot and runs them on a PlayNHost Python bot server, so it stays online when your PC is off.

You need a bot token first: How to create a Discord bot and get its token. In a hurry? Create a starter bot in the setup card writes these two files for you: How to create a starter Discord bot in one click.

The two files

requirements.txt

discord.py

bot.py

import os

import discord
from discord import app_commands

intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)


@tree.command(name="ping", description="Check that the bot answers")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message(f"Pong! {round(client.latency * 1000)} ms")


@client.event
async def on_ready():
    await tree.sync()
    print(f"Logged in as {client.user}")


client.run(os.environ["DISCORD_TOKEN"])

What it does: the bot logs in with the token the panel hands it as DISCORD_TOKEN, registers /ping with Discord when it is ready, and prints Logged in as .... It uses the default intents, so no privileged intent has to be switched on.

Run it on PlayNHost

  1. Open your server in the panel. The bot's card on Overview must say Python Bot. If not, change the type and keep your files: How to switch your bot to another language.
  2. In Files, upload bot.py and requirements.txt to the main folder.
  3. On the Bot page, pane Code, check Entry file is bot.py.
  4. On the Discord pane, paste your token and click Connect. Then invite the bot with Add to a Discord server.
  5. Press Start and open Console. The server installs discord.py, prints [PlayNHost] Starting your bot, then your bot prints Logged in as ....
  6. In Discord, type /ping.

The Console of a bot server showing [PlayNHost] Starting your bot followed by Logged in as MyBot.

/ping does not show up yet

A command synced with tree.sync() is global. It can take a little while to appear the first time, and restarting Discord (Ctrl+R) helps. While you test, sync to your own server, which is instant:

MY_GUILD = discord.Object(id=123456789012345678)  # your server's ID


@client.event
async def on_ready():
    tree.copy_global_to(guild=MY_GUILD)
    await tree.sync(guild=MY_GUILD)
    print(f"Logged in as {client.user}")

To copy your server's ID, switch on Developer Mode in Discord's Advanced settings, then right-click the server icon and choose Copy Server ID.

Prefix commands like !hello

A bot that reads message text needs the Message Content intent, in your code and in the Developer Portal:

import os

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)


@bot.command()
async def hello(ctx):
    await ctx.send(f"Hello {ctx.author.display_name}!")


@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")


bot.run(os.environ["DISCORD_TOKEN"])

On the Bot page, pane Discord, tick My code uses this next to Message Content so the panel warns you if it is off. See Fix "Used disallowed intents".

Questions people ask

Which discord.py version does this install?

The newest release: requirements.txt names it without a version and the server runs pip install -U whenever the file changes, and once a day. To stay on one major version, write discord.py>=2.4,<3.

Can I test it on my PC first?

Yes. Run pip install discord.py, set DISCORD_TOKEN in your terminal and run python bot.py. Stop it before you start it on the server, so only one copy runs.

Next steps

Already hosting your bot somewhere else? Move it to us

OUR LATEST BLOGS