PyPresence Discord RPC Client
PyPresence is a Discord RPC client library for Python, enabling developers to integrate their applications with Discord's Rich Presence feature. It supports both synchronous and asynchronous operations for updating a user's status. The current version is 4.6.1, and it typically has releases for bug fixes and new Discord RPC features.
Common errors
-
pypresence.exceptions.PipeError: Could not find Discord IPC pipe!
cause The Discord desktop client is not running, or pypresence cannot locate the IPC pipe.fixStart the Discord desktop client application. If it's already running, try restarting your Python script or Discord. Ensure you have the latest pypresence version for improved pipe detection. -
pypresence.exceptions.InvalidID: The Client ID you provided is invalid.
cause The Discord Application ID provided to `Presence()` or `AioPresence()` is not a valid 18-19 digit number, or doesn't correspond to an existing Discord application.fixDouble-check your `CLIENT_ID` in the Discord Developer Portal (https://discord.com/developers/applications). Copy it directly and ensure it's passed as a string or integer. -
TypeError: 'str' object cannot be interpreted as an integer
cause The `CLIENT_ID` was passed as a string where an integer was expected, particularly in older versions or specific parts of the library's internal workings.fixEnsure your `CLIENT_ID` is converted to an integer: `Presence(int(CLIENT_ID))` or `AioPresence(int(CLIENT_ID))`. While newer versions might handle string conversion, explicitly converting is safer. -
KeyError: 'details'
cause An invalid key was passed to the `update()` method, or the data structure for presence is incorrect according to Discord's API.fixRefer to the pypresence documentation or Discord Rich Presence API for valid keys and their expected types. Common keys include `state`, `details`, `start`, `end`, `large_image`, `large_text`, `small_image`, `small_text`, `buttons`, etc.
Warnings
- gotcha Discord client must be running. PyPresence connects to Discord via an IPC (Inter-Process Communication) pipe. If the Discord desktop client is not running, PyPresence will fail to connect and raise a `PipeError`.
- gotcha Incorrect Client ID or application not public. Your Discord Application ID must be correct and belong to an application you own or have access to. For Rich Presence to be visible to others, the application's 'Public' toggle must be enabled in the Discord Developer Portal.
- breaking Changes in IPC pipe detection and closing logic in v4.5.x. Earlier versions might have had issues with reliable connection or proper closing, especially on Windows, leading to 'Pipe Not Found' errors or resources not being released.
- gotcha Using `Presence` (sync) in an async context or `AioPresence` (async) in a sync context. While `Presence` is synchronous, `AioPresence` requires an `asyncio` event loop. Mixing them incorrectly can lead to `RuntimeError: Event loop is already running` or `RuntimeError: no current event loop`.
Install
-
pip install pypresence
Imports
- Presence
from pypresence import Presence
- AioPresence
from pypresence import AioPresence
- InvalidID
from pypresence.exceptions import InvalidID
- PipeError
from pypresence.exceptions import PipeError
Quickstart
import time
import os
from pypresence import Presence
# Get CLIENT_ID from environment or set a placeholder
# Replace with your actual Discord Application ID
CLIENT_ID = os.environ.get('PYPRESENCE_CLIENT_ID', '123456789012345678')
# Ensure Discord client is running before attempting to connect
# Presence requires an integer ID, so convert if necessary.
print(f"Attempting to connect with Client ID: {CLIENT_ID}")
RPC = Presence(CLIENT_ID)
try:
RPC.connect()
print("Connected to Discord RPC")
RPC.update(state="Playing a game!",
details="Currently in a match",
start=int(time.time()),
large_image="your_large_image_key",
large_text="Large Image Text",
small_image="your_small_image_key",
small_text="Small Image Text",
buttons=[{"label": "Join My Game", "url": "https://example.com/join"}])
print("Presence updated. Will stay active for 30 seconds...")
time.sleep(30) # Keep presence active for 30 seconds
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure Discord is running and your Client ID is correct.")
finally:
RPC.close()
print("Disconnected from Discord RPC.")