communicate with discord-rpc in python

To communicate with Discord's Rich Presence API (discord-rpc) in Python, you'll need to install the pypresence library using pip.

main.py
pip install pypresence
23 chars
2 lines

Once you have pypresence installed, you can import it and create a Client object. Then, you can use various methods to communicate with the API and update your Discord presence.

main.py
import pypresence
import time

# Initialize the Client
RPC = pypresence.Presence(client_id)

# Connect to the API
RPC.connect()

# Set your presence
RPC.update(state="Playing", details="My Cool Game", start=time.time())

# You can update your presence later on as well
RPC.update(details="Now Playing: Level 2", end=time.time())
329 chars
15 lines

The update() method takes various parameters such as state, details, start, end, large_image, and small_image. These parameters define your presence details such as the game you are playing, your current status, and even the images you want to display.

Don't forget to close the connection with the API when you are done using the close() method.

main.py
RPC.close()
12 chars
2 lines

With these basic steps, you can use discord-rpc to enable your users to show off their current activities on Discord.

gistlibby LogSnag