Pycord
Pycord is a modern, easy-to-use, and feature-rich Python wrapper for the Discord API. It enables developers to build Discord bots with support for slash commands, UI components, voice functionality, and more. The library is actively maintained with frequent minor updates and occasional major version releases to align with Discord API changes and introduce new features.
Common errors
-
discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal.
cause Your bot is attempting to use a privileged intent (e.g., Message Content, Members, or Presences) that has not been enabled in your Discord Developer Portal settings or not specified in your bot's code.fixGo to the Discord Developer Portal, select your application, navigate to the 'Bot' tab, and enable the necessary privileged intents under 'Privileged Gateway Intents'. Also, ensure these intents are passed to your `discord.Bot` or `discord.Client` instance (e.g., `intents.message_content = True`). -
discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access
cause Your bot lacks the necessary permissions to perform an action (e.g., sending messages in a channel, assigning roles, kicking members) in the Discord server. This can be due to bot role hierarchy or missing permissions in the developer portal/server.fixCheck your bot's role permissions in the Discord server and ensure its highest role is above the roles it's trying to manage. Also, verify that the bot has the required permissions enabled in the Discord Developer Portal and during its invite process (OAuth2 URL Generator). If MFA is required for the server, ensure your developer account has it enabled. -
message.content is empty / Bot not reading message content
cause The `message_content` privileged intent is not enabled, preventing your bot from accessing the content of messages sent by other users.fixEnable the 'Message Content Intent' in your bot's settings on the Discord Developer Portal, and ensure `intents.message_content = True` is set when initializing your `discord.Bot` or `discord.Client` instance. -
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
cause This error often occurs with slash commands when required options are defined after optional ones, or when the message content (e.g., in an embed) is malformed or empty, or exceeds Discord's character limits (2000 characters for a message).fixFor slash commands, ensure all required options are placed before any optional options. For messages, verify the content is not empty, does not exceed 2000 characters, and any embeds or components are correctly structured and populated.
Warnings
- breaking Pycord v2.7.0 dropped support for Python 3.8 and 3.9. Python 3.7 and below were dropped in v2.0. The library currently requires Python >=3.10 and <3.14.
- breaking Gateway Intents are now explicitly required when initializing `discord.Client` or `discord.Bot`. Additionally, `Intents.message_content`, `Intents.members`, and `Intents.presences` are 'privileged intents' and must be manually enabled in your bot's Discord Developer Portal settings.
- breaking Migrating from py-cord v1.x to v2.x involves significant breaking changes, including a new `discord.Bot` class for application commands, redesigned UI components, removal of user account support, and changes to webhook handling. Old `discord.py` code may require substantial updates.
- gotcha A `KeyError` could occur when fetching application information or checking ownership (`is_owner`) due to `read_only` team members not being considered owners. This was a recent fix.
Install
-
pip install -U py-cord -
pip install -U "py-cord[voice]"
Imports
- Bot
from discord import Bot
- Client
from discord import Client
- Intents
from discord import Intents
- ApplicationContext
from discord import ApplicationContext
Quickstart
import discord
import os
# Enable necessary intents. message_content is privileged for text commands.
intents = discord.Intents.default()
intents.message_content = True # Required for reading message content in commands or on_message events
bot = discord.Bot(intents=intents)
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
@bot.slash_command(name="hello", description="Say hello to the bot")
async def hello(ctx: discord.ApplicationContext, name: str = None):
"""Says hello to the user or a specified name."""
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
# Run the bot with the token from environment variables
# Make sure to set a 'TOKEN' environment variable with your bot's token
bot.run(os.environ.get('TOKEN'))