Registry / communication / discord.py

discord.py

JSON →
library2.3.2pypypiunverified

discord.py is a modern, easy-to-use, feature-rich, and async-ready API wrapper for Discord written in Python. It simplifies interaction with the Discord API, enabling developers to create bots with various functionalities, from simple message responders to complex moderation tools. The library is actively maintained, with new releases typically following Discord API updates, and currently requires Python 3.8 or higher. [1, 2, 7]

communicationhttp-networkingweb-framework
pip install -U discord.py
Install & Compatibility
Where this runs
tested against v2.7.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.950 runs
installs and imports cleanly · install 0.0s · import 0.839s · 43MB
glibc
py 3.103.950 runs
installs and imports cleanly · install 4.6s · import 0.757s · 44MB
42MB installed
● package 42MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Client
import discord client = discord.Client(...)
from discord import Client
While functional, importing the top-level 'discord' module and accessing Client as 'discord.Client' is the idiomatic and generally recommended approach to avoid potential name collisions and keep the namespace cleaner.
Intents
import discord intents = discord.Intents.default()
from discord import Intents
Intents are usually accessed via the main 'discord' module. It also requires explicit enabling of privileged intents both in code and in the Discord Developer Portal. [14, 16]
Bot
from discord.ext import commands bot = commands.Bot(...)
import discord bot = discord.Bot(...)
The Bot class, which provides command handling, resides in the `discord.ext.commands` submodule, not directly under `discord`.
View
from discord.ui import View
from discord import View
UI components like Views (for buttons, select menus) are located in the `discord.ui` submodule. [30]

This quickstart demonstrates a basic Discord bot using `discord.ext.commands.Bot`. It includes essential setup like defining intents (crucial for `discord.py` v2.0+), handling the `on_ready` event, and a simple 'ping' command. Remember to enable the 'Message Content Intent' in your bot's settings on the Discord Developer Portal for the bot to read message content and process text commands. [3, 7, 16]

import discord from discord.ext import commands import os # Configure intents: message_content is a privileged intent intents = discord.Intents.default() intents.message_content = True # Required for text commands that read message content [7] # Initialize the bot with a command prefix and intents bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): print(f'Logged in as {bot.user} (ID: {bot.user.id})') print('------') @bot.command() async def ping(ctx): """Responds with pong!""" await ctx.send('pong!') @bot.event async def on_message(message): # Ignore messages from the bot itself if message.author == bot.user: return # Process commands (needed if you also have on_message event handler) await bot.process_commands(message) # Get the bot token from an environment variable for security BOT_TOKEN = os.environ.get('DISCORD_BOT_TOKEN', 'YOUR_BOT_TOKEN_HERE') if BOT_TOKEN == 'YOUR_BOT_TOKEN_HERE': print("WARNING: Replace 'YOUR_BOT_TOKEN_HERE' with your actual bot token or set DISCORD_BOT_TOKEN env var.") # Run the bot bot.run(BOT_TOKEN)
Debug
Known issues
breakingThe `discord` package on PyPI is a mirror and not the official library. Installing `discord` instead of `discord.py` can lead to outdated versions, missing features, or even malicious code. [PyPI summary, 21]
fix
Always install `discord.py` using `pip install -U discord.py`.
affects: All versions
breakingVersion 2.0+ of `discord.py` dropped support for Python versions below 3.8. Ensure your environment meets this requirement. [7, 14, 23]
fix
Upgrade your Python installation to 3.8 or higher.
affects: 2.0.0 and above
breakingIntents are now mandatory for `Client` and `Bot` initialization. The `message_content` intent became privileged in v2.0, meaning you must explicitly enable it in your code and on the Discord Developer Portal for your bot to receive message content and process text commands. User account automation is against Discord ToS, and support for user tokens was removed. [7, 14, 19, 23, 27]
fix
Pass an `intents` object to `discord.Client` or `commands.Bot` (e.g., `intents = discord.Intents.default(); intents.message_content = True`). Also, enable the necessary privileged intents (e.g., Message Content Intent, Server Members Intent) in your bot's settings on the Discord Developer Portal.
affects: 2.0.0 and above
breakingMany methods like `edit()` no longer update objects in-place; they now return a new, modified instance. Old attributes like `User.avatar_url` or `Guild.icon_url` have been replaced or changed to `Asset` objects. [7, 14, 19, 22]
fix
Capture the return value of `edit()` methods. For avatars/icons, use `user.display_avatar` for the pre-2.0 behavior or access `user.avatar.url` for the raw URL.
affects: 2.0.0 and above
gotchaDo not name your Python script `discord.py` or any name that conflicts with the library's internal modules. This can cause `ImportError` or `ModuleNotFoundError` due to Python's import system looking at your local file instead of the installed library. [3, 24]
fix
Rename your script file to something unique, like `bot.py` or `main.py`.
affects: All versions
Upgrade
Version history
2.7.1latest on PyPI
Audit
Dependencies
PythonrequiredPython 3.8 or higher is required for discord.py v2.0+.
PyNaCloptionalRequired for voice functionality. Install with `discord.py[voice]`.
Agent activity
45 hits · last 30 days
node
12
mj12bot
3
ahrefsbot
3
oai-searchbot
2
amazonbot
1
bytedance
1
Resources