aslain.dev
0%
← Tüm makaleler

A Discord Bot in Python: A Getting-Started Guide with discord.py

You don't have to write a Discord bot only in Node.js — Python and the discord.py library are a powerful, very readable option too. Building a Discord bot in Python is especially comfortable for data-processing and automation-heavy bots. In this guide we'll build a bot from scratch with discord.py 2.x and write a command that connects to a game database.

Requirements

  • Python 3.10+.
  • pip install discord.py aiomysql.
  • A bot + token in the Developer Portal; invite it with the bot and applications.commands scopes.

Your first bot: a /ping command

In discord.py 2.x, slash commands are managed through the command tree. bot.tree.sync() uploads the commands to Discord:

import discord
from discord.ext import commands

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

@bot.event
async def on_ready():
    await bot.tree.sync()          # slash komutlarini Discord'a yukle
    print(f"Giris yapildi: {bot.user}")

@bot.tree.command(name="ping", description="Pong ile yanitlar")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("Pong! 🏓")

bot.run("TOKEN")

The intents decide which events the bot receives. Privileged intents like the member list or message content must be enabled in the Developer Portal.

Connecting to a game database

An async bot should query the database asynchronously too, so it doesn't block other commands. With aiomysql, a command that fetches a player's level:

import aiomysql

pool = await aiomysql.create_pool(
    host="localhost", user="bot", password="***", db="player"
)

async with pool.acquire() as conn:
    async with conn.cursor() as cur:
        await cur.execute(
            "SELECT level FROM player WHERE name = %s", (name,)
        )
        row = await cur.fetchone()

await interaction.response.send_message(f"{name} seviye {row[0]}")

Here %s is a parameter placeholder; never write user input straight into the query — this prevents SQL injection.

How it differs from Node.js

discord.py reads cleanly with its synchronous-looking async/await and gives direct access to Python's rich data ecosystem (pandas, requests, ML libraries). Node.js shines in event-driven, highly concurrent work. The right choice depends on what your bot does.

Keeping the bot running

You can run the bot 24/7 on a VPS with a systemd service or pm2's Python support. Auto-restart on crash and log keeping are essential.

Frequently asked questions

Is discord.py still maintained?

Yes. It's actively developed with the 2.x line and fully supports slash commands (app_commands).

My slash commands don't show up — why?

Usually bot.tree.sync() wasn't called, or you're waiting for global commands to propagate. Syncing to a single server in development gives instant results.

Can I use both Python and Node.js in the same bot?

A single bot is written in one language, but you can write different bots in different languages and have them share the same database.

Want a Python-based bot? I build bots that process game data and run automations — get in touch.

Devamı için