Black Friday: first month free on quarterly plans · until 30 Nov See the plans Conditions
New here? Try a Minecraft server or Discord bot free for 24 hours. Try free
ServerSparkServerSpark

Knowledge base

Make a Discord bot in Python (step by step)

Build your first Discord bot with a slash command in Python and put it online 24/7.

NVMe nodesNightly backupsOnline in minutes

Updated 2 November 2026

Making your own Discord bot is easier than it sounds. In this guide you build a bot in Python with a first slash command, /ping, and then put it online 24/7. You need Python 3.10 or newer on your own computer.

1. Create the bot on Discord

  1. Go to the Discord Developer Portal, log in and click New Application. Give your bot a name.
  2. Go to Bot and click Reset Token. Copy the token. This is your bot's password: never share it.
  3. Go to OAuth2 → URL Generator. Under Scopes tick bot and applications.commands, and under Bot Permissions for example Send Messages.
  4. Open the link at the bottom in your browser and add the bot to your own server.

2. Write the code

Make a folder with three files.

requirements.txt:

discord.py
python-dotenv

.env (replace the text with your own token):

DISCORD_TOKEN=your-token-here

app.py:

import os

import discord
from discord import app_commands
from dotenv import load_dotenv

load_dotenv()  # reads DISCORD_TOKEN from the .env file

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


@tree.command(name="ping", description="Check if the bot is online")
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()  # registers the slash commands with Discord
    print(f"Logged in as {client.user}")


client.run(os.getenv("DISCORD_TOKEN"))

3. Test it on your own computer

  1. Install the packages: pip install -r requirements.txt
  2. Start the bot: python app.py
  3. See "Logged in as ..."? Type /ping in your Discord server. The command can take a few minutes to appear the first time.

Close the window or turn your computer off and the bot goes offline. That's what hosting is for.

4. Put it online 24/7

With ServerSpark bot hosting your bot runs day and night, for €0.69 a month (paid quarterly):

  1. Order Bot hosting (Python). Your server already contains this example bot: just put your token in .env under Files and skip to the last step. Using your own code instead?
  2. Upload app.py, requirements.txt and .env in SparkPanel under Files. The server starts app.py by default and installs the packages from requirements.txt by itself.
  3. Click Start in the Console. If the bot crashes, it's restarted automatically.

More on uploading, Git and the start file in Put your Discord bot online 24/7.

Next steps

  • Add more commands by copying and changing the @tree.command block.
  • Want to read messages (for example for a moderation bot)? Turn on the Message Content Intent under Bot in the Developer Portal, and set intents.message_content = True in your code.
  • Prefer JavaScript? Read Make a Discord bot with JavaScript.