discord-rpc
An Python wrapper for Discord RPC API. Allow you to make own custom RPC.
Install
- PyPI
pip install discord-rpcQuick example
import discordrpc
rpc = discordrpc.RPC(app_id=12345678910)
rpc.set_activity(
state="A super simple rpc",
details="simple RPC"
)
# Required if you only run Discord RPC on this file or current instance.
rpc.run()rpc.run() is only used if you are only running Discord RPC on the current file/instance. If there are other programs/tasks on the current instance, rpc.run() does not need to be used.
See documentation here.
More examples here.
Changelog
7.0
Breaking :
utils.timestampis now a function, not a variable. Updatetimestamptotimestamp()in your code.subscribe()andunsubscribe()now raiseDiscordNotOpenedwhen Discord is closed andexit_if_discord_close=True(the default), instead of returningNone. Catch it or passexit_if_discord_close=Falseto keep the old behavior.set_activity()now raisesActivityErrorwhenstateordetailsis outside 2 to 128 characters, andButtonErrorwhen a button has no label, a label over 32 characters, a URL over 512 characters, is a plain URL string, or is combined withjoin_secret,spectate_secret, ormatch_secret.- Event callbacks now run on their own thread instead of the IPC reader thread, so callbacks for events that arrive close together can run at the same time.
Application.publicis nowNonewhen Discord does not report it, instead ofFalse.
Added :
- Unix pipe connect now also tries the Flatpak Discord socket at
$XDG_RUNTIME_DIR/app/com.discordapp.Discord/discord-ipc-Nby @orowith2os in #71
Fixed :
disconnect()now clearsis_running, subscriptions, and callbacks even when the pipe was already disconnected- Unix pipe connect now falls back cleanly on any socket error, and closing no longer raises when Discord already closed the socket
run()no longer hangs forever when the heartbeat PING fails or Discord closes the connectionsubscribe()andunsubscribe()now attempt to reconnect when the pipe is disconnected, andsubscribe()after a reconnect subscribes again on Discord’s sideunsubscribe()now returnsFalse(instead ofNone) when the event is not subscribed- Unsubscribing from the last event no longer closes the connection and wipes the presence
- Windows: a second
set_activity(), therun()heartbeat,unsubscribe(), anddisconnect()no longer hang while the reader waits for data - Windows: connect now tries all 10 Discord pipes even when
exit_if_discord_close=True - An invalid application ID now raises
InvalidID - Calling
set_activity(),subscribe(), orunsubscribe()inside an event callback no longer times out - Calling
disconnect()from another thread, such as an event callback, now also stopsrun() - The handshake now gives up after 30 seconds with an
RPCException, instead of waiting forever clear()now returns the result ofset_activity()instead ofNonestatus_typenow accepts the plain int valuerpc.Userread before the handshake no longer stays empty after connectingprogress_bar()now raisesProgressbarErrorwhencurrentis negative ordurationis zero or negativeAssetManager()no longer raisesTypeErrorwhen constructed without an asset list- IPC reader now marks the pipe as disconnected on EOF or an empty
OP_CLOSEframe - Fixed
rpc-all-in-one.pyandrpc-with-timestamp.pyexamples
Changed :
date_to_timestamp()now usesdatetime.timestamp()instead oftime.mktime()get_app_info()andget_assets()now share a single fetch helperButtonErrorandProgressbarErrorno longer define redundant constructors
6.5
Added :
- Cross-platform event subscriptions with
Eventenum and@rpc.on()decorator by @SuperZombi in #66 AssetManagerwithRPC.assetsproperty and.get()/.namesfor fetching application assets by @SuperZombi in #60Assetobject support inset_activity(). Passrpc.assets.get("key")directly aslarge_image/small_imageutils.get_assets(app_id)to fetch assets without initializingRPCRPC.connectedread-only property by @SuperZombi in #63User.avatar_decorationandApplication.coverCDN URLs by @SuperZombi in #67InvalidEventandInvalidEventTypeexceptions,utils.required_url(), andexamples/rpc-events.py
Changed :
- IPC reads now preserve the opcode and route responses by nonce through a single background reader
- Windows imports are now loaded only on Windows, no more
msvcrtorwin32pipeneeded - URL validation now checks the scheme and host, and
set_activity()validates button URLs client-side - Bumped GitHub Actions versions by @SuperZombi in #62
- Docs and changelog links now point to
DOCS.mdandCHANGELOG.mdon GitHub
Fixed :
- Invalid method annotations in
_send()and_request()that prevented importing the package - Unread
PONGresponses no longer corrupt subsequent requests, and incomingPINGis answered withPONG - Handshake no longer requires a nonce
- Failed subscriptions no longer register callbacks locally
- Reader thread stops after the last unsubscribe and is cleaned up during disconnect
6.0
Added :
nameparameter toRPC.set_activity(), falls back to the application name by @SuperZombi in #58ping_everyparameter toRPC.run()for configurable heartbeat interval- External URL image support for
large_imageandsmall_image
Changed :
UserandAppare now lazy-loaded@cached_propertyby @SuperZombi in #58RPC.get_app_info()removed, use therpc.Appproperty directlyButton()renamed tobutton()for PEP8 compliance- Shared pipe logic extracted into
_BasePipeclass TRY_RECONNECTINGglobal converted to instance attributeself.try_reconnecting
Fixed :
- Socket timeout prevention: heartbeat is now sent every 15 seconds by default in
RPC.run() - Log handler duplication across multiple RPC instances, and
logging.basicConfig()removed SyntaxErroron some Python versions caused by global variable declaration orderUnixPipe._recv()data truncation on large payloadsself.connectednow reflects the real pipe connection result- Broken button arguments in
print-rpc-output.pyexample
5.6
- Fix
importlib.metadata.PackageNotFoundErrorby @deeffest in #56 - Add
Applicationinfo fetching (rpc.get_app_info()) by @SuperZombi & @Senophyx in #57 - Refactor: cleanup unused parameter comments and rename
ProgressBartoprogress_barby @Senophyx - Fix typing and license metadata on
pyproject.tomlby @Senophyx - Replace all-contributors with contrib.rocks by @Senophyx
- Update docs links and styling by @Senophyx
5.5
- PEP 621: Migrating from
setup.pytopyproject.tomlby in #0ffaca5 - feat: rewrite Button, added new ActivityType class, fix minor issues by @SuperZombi in #49
- Add new types and params by @SuperZombi in #50
- Add some missing types by @jannuary in #53
5.1
Updates :
- Added Activity Types (Listening, Watching, etc).
rpc.set_activity(act_type=0)- Added more detailed information in the output.
5.0
Critical updates & changes :
- Inputting id is no longer in
RPC.set_id. Replaced with inputing insideRPCclass.
Before:
rpc = discordrpc.RPC.set_id(app_id=1234567890)After:
rpc = discordrpc.RPC(app_id=1234567890)Updates :
Removing many functions. List:
RPC.set_idRPC._connectRPC._writeRPC._recvRPC._recv_exact- And many more…
Package
loggingis used again as output.Removing
timestampargument and addedts_startandts_endonset_activity()as a replacement for the timestamp argument.Added
debugparameter onRPCclass if you want to print more outputs.timestampvariable is moved todiscordrpc.utils. So if you want to import timestamp variable, usefrom discordrpc.utils import timestampbuttonondiscordrpc.buttonchanged toButton.added
date_to_timestampfunction ondiscordrpc.utils. with format%d/%m/%Y-%H:%M:%Sorday/month/year-hour:minute:secondExample :date_to_timestamp('14/06/2025-00:00:00')
4.0
Critical update :
- Changing
DiscordRPCfolder (when you import the package) todiscordrpc. Before :
import DiscordRPCAfter :
import discordrpcUpdates :
- No longer using
loggingpackage. GCARorget_current_application_runningremoved because it can’t be cross-platform (I spent days just looking for this solution lmao).setLoggerEnabled()function removed.Set_IDchanged toset_id.output()function changed toshow_outputvariable, Example.- Shorten the code in
presence.py(not that much though).
3.0
New & Updates :
- Added
GCARmethod. GCAR : Get Current Application Running. Basically, you can switch your rpc status (state/details) automatically to the application you’re running
2.0
New & Updates :