YENİ 📢 İlk ayda %30'a varan indirim, sınırlı süre!

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

Discord bot hostingUpdated Oct 6, 20263 min read

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

  1. 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.
  2. In Files, upload bot.py and requirements.txt.
  3. In Settings, Game Settings, set Main file to bot.py and paste your token into Discord bot token.
  4. Open Console and start the server. The server installs the packages in requirements.txt on each start.
  5. When the console shows Logged in as ..., type /ping in 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)) then await 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.

OYUN SUNUCUNUZU OLUŞTURUN
BAŞLAMAYA HAZIR MISINIZ?

Yeni oyun sunucunuzda %30’a varan tasarruf sağlayın.
Premium performans, anında aktivasyon ve rakipsiz güvenilirlik. Hepsi daha iyi bir fiyatla.

EN YENİ BLOG YAZILARIMIZ