Discord RPC client written in Python for integrating applications with Discord's Rich Presence system
npx @tessl/cli install tessl/pypi-pypresence@4.3.0A comprehensive Discord RPC client library for Python that enables applications to integrate with Discord's Rich Presence system. Pypresence provides both synchronous and asynchronous implementations for displaying rich status information including activity names, details, timestamps, and custom images in users' Discord profiles.
pip install pypresenceimport pypresenceCommon imports for specific functionality:
from pypresence import Presence, AioPresence
from pypresence import Client, AioClient
from pypresence import PyPresenceException, DiscordNotFoundfrom pypresence import Presence
import time
# Connect to Discord
RPC = Presence("your_client_id_here")
RPC.connect()
# Update presence
RPC.update(state="Playing Solo", details="In Main Menu")
# Keep alive
while True:
time.sleep(15)import asyncio
from pypresence import AioPresence
async def main():
RPC = AioPresence("your_client_id_here")
await RPC.connect()
await RPC.update(
state="In Game",
details="Level 5 - Forest",
large_image="game_logo",
large_text="My Amazing Game",
start=int(time.time())
)
# Keep alive
while True:
await asyncio.sleep(15)
asyncio.run(main())Pypresence provides a layered architecture:
The library handles cross-platform IPC communication automatically, discovering Discord's pipe/socket connections on Windows, Linux, and macOS.
Simple interface for updating Discord Rich Presence with activity information, timestamps, images, and party details.
class Presence(BaseClient):
def __init__(self, client_id: str, **kwargs): ...
def connect(self): ...
def update(self, pid: int = None, state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True, payload_override: dict = None): ...
def clear(self, pid: int = None): ...
def close(self): ...class AioPresence(BaseClient):
def __init__(self, client_id: str, **kwargs): ...
async def connect(self): ...
async def update(self, pid: int = None, state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True): ...
async def clear(self, pid: int = None): ...
def close(self): ...Full Discord RPC client with support for authentication, guild/channel operations, voice settings, and event subscriptions.
class Client(BaseClient):
def __init__(self, client_id: str, **kwargs): ...
def start(self): ...
def authorize(self, client_id: str, scopes: list): ...
def authenticate(self, token: str): ...
def set_activity(self, pid: int = None, state: str = None, details: str = None, **kwargs): ...
def get_guilds(self): ...
def get_voice_settings(self): ...
def register_event(self, event: str, func: callable, args: dict = None): ...
def close(self): ...class AioClient(BaseClient):
def __init__(self, client_id: str, **kwargs): ...
async def start(self): ...
async def authorize(self, client_id: str, scopes: list): ...
async def authenticate(self, token: str): ...
async def set_activity(self, pid: int = None, state: str = None, details: str = None, **kwargs): ...
async def get_guilds(self): ...
async def get_voice_settings(self): ...
async def register_event(self, event: str, func: callable, args: dict = None): ...
def close(self): ...Comprehensive exception hierarchy for handling Discord integration errors.
class PyPresenceException(Exception):
def __init__(self, message: str = None): ...
class DiscordNotFound(PyPresenceException): ...
class InvalidPipe(PyPresenceException): ...
class InvalidArgument(PyPresenceException): ...
class ServerError(PyPresenceException): ...
class DiscordError(PyPresenceException): ...
class InvalidID(DiscordError): ...
class PipeClosed(PyPresenceException): ...
class ResponseTimeout(PyPresenceException): ...
class ConnectionTimeout(PyPresenceException): ...class BaseClient:
"""Base client for Discord RPC communication"""
def __init__(self, client_id: str, loop=None, handler=None,
pipe=None, isasync: bool = False,
connection_timeout: int = 30, response_timeout: int = 10): ...
def update_event_loop(self, loop): ...
async def read_output(self): ...
def send_data(self, op: int, payload): ...
async def handshake(self): ...class Payload:
"""Discord RPC payload builder"""
def __init__(self, data: dict, clear_none: bool = True): ...
@staticmethod
def time() -> float: ...
@classmethod
def set_activity(cls, pid: int = None, state: str = None,
details: str = None, **kwargs) -> 'Payload': ...