Skip to content

Commit

Permalink
Add activity type support
Browse files Browse the repository at this point in the history
  • Loading branch information
MCMi460 committed Jul 28, 2024
1 parent 5b4cb5a commit d6709d3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
18 changes: 18 additions & 0 deletions examples/rich-presence-music.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pypresence import Presence, ActivityType
import time

client_id = '717091213148160041' # Fake ID, put your real one here
RPC = Presence(client_id) # Initialize the client class
RPC.connect() # Start the handshake loop

RPC.update(
activity_type = ActivityType.LISTENING, # Set the activity to listening
details=input("Your favorite song: "),
state=input("The artist who made it: "),
end=int(input("The length of the song (in seconds): ")) + time.time(),
# At time of writing this, timestamps don't show for listening statuses!
# ...so this field is pointless lol
) # Get the user's favorite song!

while True: # The presence will stay on as long as the program is running
time.sleep(15) # Can only update rich presence every 15 seconds
1 change: 1 addition & 0 deletions pypresence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .baseclient import BaseClient
from .client import Client, AioClient
from .exceptions import *
from .types import ActivityType
from .presence import Presence, AioPresence


Expand Down
13 changes: 8 additions & 5 deletions pypresence/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .baseclient import BaseClient
from .exceptions import *
from .payloads import Payload
from .types import ActivityType


class Client(BaseClient):
Expand Down Expand Up @@ -121,6 +122,7 @@ def select_text_channel(self, channel_id: str):
return self.loop.run_until_complete(self.read_output())

def set_activity(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
Expand All @@ -129,8 +131,8 @@ def set_activity(self, pid: int = os.getpid(),
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True):
payload = Payload.set_activity(pid=pid, state=state, details=details, start=start, end=end,
large_image=large_image, large_text=large_text, small_image=small_image,
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details, start=start,
end=end, large_image=large_image, large_text=large_text, small_image=small_image,
small_text=small_text, party_id=party_id, party_size=party_size, join=join,
spectate=spectate, match=match, buttons=buttons, instance=instance,
activity=True)
Expand Down Expand Up @@ -305,6 +307,7 @@ async def select_text_channel(self, channel_id: str):
return await self.read_output()

async def set_activity(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
Expand All @@ -313,9 +316,9 @@ async def set_activity(self, pid: int = os.getpid(),
join: str = None, spectate: str = None,
buttons: list = None,
match: str = None, instance: bool = True):
payload = Payload.set_activity(pid, state, details, start, end, large_image, large_text,
small_image, small_text, party_id, party_size, join, spectate,
match, buttons, instance, activity=True)
payload = Payload.set_activity(pid, activity_type, state, details, start, end, large_image,
large_text, small_image, small_text, party_id, party_size,
join, spectate, match, buttons, instance, activity=True)
self.send_data(1, payload)
return await self.read_output()

Expand Down
8 changes: 8 additions & 0 deletions pypresence/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Union

from .utils import remove_none
from .types import ActivityType


class Payload:
Expand All @@ -22,6 +23,7 @@ def time():

@classmethod
def set_activity(cls, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
Expand All @@ -38,12 +40,18 @@ def set_activity(cls, pid: int = os.getpid(),
start = int(start)
if end:
end = int(end)
if activity_type:
if isinstance(activity_type, ActivityType):
activity_type = activity_type.value
else:
activity_type = int(activity_type)

if activity is None:
act_details = None
clear = True
else:
act_details = {
"type": activity_type,
"state": state,
"details": details,
"timestamps": {
Expand Down
11 changes: 7 additions & 4 deletions pypresence/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .baseclient import BaseClient
from .payloads import Payload
from .utils import remove_none, get_event_loop
from .types import ActivityType


class Presence(BaseClient):
Expand All @@ -14,6 +15,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def update(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
Expand All @@ -24,8 +26,8 @@ def update(self, pid: int = os.getpid(),
instance: bool = True, payload_override: dict = None):

if payload_override is None:
payload = Payload.set_activity(pid=pid, state=state, details=details, start=start, end=end,
large_image=large_image, large_text=large_text,
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details,
start=start, end=end, large_image=large_image, large_text=large_text,
small_image=small_image, small_text=small_text, party_id=party_id,
party_size=party_size, join=join, spectate=spectate,
match=match, buttons=buttons, instance=instance, activity=True)
Expand Down Expand Up @@ -56,6 +58,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs, isasync=True)

async def update(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
Expand All @@ -64,8 +67,8 @@ async def update(self, pid: int = os.getpid(),
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True):
payload = Payload.set_activity(pid=pid, state=state, details=details, start=start, end=end,
large_image=large_image, large_text=large_text,
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details,
start=start, end=end, large_image=large_image, large_text=large_text,
small_image=small_image, small_text=small_text, party_id=party_id,
party_size=party_size, join=join, spectate=spectate,
match=match, buttons=buttons, instance=instance, activity=True)
Expand Down
11 changes: 11 additions & 0 deletions pypresence/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import enum

# https://discord.com/developers/docs/game-sdk/activities#data-models-activitytype-enum
# ""type" must be one of 0, 2, 3, 5" -- Discord only implemented these four
class ActivityType(enum.Enum):
PLAYING = 0
#STREAMING = 1
LISTENING = 2
WATCHING = 3
#CUSTOM = 4
COMPETING = 5

0 comments on commit d6709d3

Please sign in to comment.