PyPresence Discord RPC Client

4.6.1 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pypresence.Presence` for synchronous Rich Presence updates. It connects to Discord, sets a custom presence with details, timestamps, images, and buttons, then maintains it for 30 seconds before disconnecting. Remember to replace `123456789012345678` with your actual Discord Application ID in production, or set the `PYPRESENCE_CLIENT_ID` environment variable.

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.")

view raw JSON →