Discord.py

2.7.1 · active · verified Thu Apr 09

Discord.py is a modern, easy to use, feature-rich, and async ready API wrapper for Discord, written in Python. The current version is 2.7.1, and it is actively maintained with regular updates to support Discord API changes and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a minimal Discord bot that responds to a '$hello' message. It highlights the explicit requirement for `discord.Intents` and enabling the `message_content` privileged intent, which is crucial for bots interacting with message content in v2.0 and newer.

import os
import discord

intents = discord.Intents.default()
intents.message_content = True # Required for on_message to access message.content

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

# Replace 'YOUR_BOT_TOKEN' with your actual bot token, ideally from environment variables.
# Ensure DISCORD_TOKEN is set in your environment for production.
client.run(os.environ.get('DISCORD_TOKEN', 'YOUR_BOT_TOKEN'))

view raw JSON →