This guide builds a Python bot with one slash command, /ping. You need a Discord application and a bot token first: How to create a Discord bot and get its token.
The files
requirements.txt
`` discord.py>=2.4 ``
bot.py
```python 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="Replies with pong") async def ping(interaction: discord.Interaction): await interaction.response.send_message("pong")
@client.event async def on_ready(): await tree.sync() print(f"Logged in as {client.user}")
client.run(os.environ["DISCORD_TOKEN"]) ```
The bot uses the default intents, so no privileged intent has to be switched on.
Run it on PlayNHost
- Open your server in the panel. Click Edit Server Type on Overview and choose Python Bot. Do this on a new server: changing the type reinstalls it. See How to change your server type.
- In Files, upload
bot.pyandrequirements.txt. - In Settings, Game Settings, set Main file to
bot.pyand paste your token into Discord bot token. - Open Console and start the server. The server installs the packages in
requirements.txton each start. - When the console shows
Logged in as ..., type/pingin Discord.
Good to know
- The server runs a current Python 3 release. If a package needs a different version, tell support which one.
- Commands registered with
tree.sync()are global and can take a while to appear. While testing, sync to one server instead:tree.copy_global_to(guild=discord.Object(id=YOUR_SERVER_ID))thenawait tree.sync(guild=...). - Read errors in Console. A missing package means a line is missing from
requirements.txt.
More: How to host a Python Discord bot, How to use a database with your Discord bot.





