Skip to content

Commit d49162b

Browse files
authored
feat: Client Presence enum support. (#547)
1 parent 4421264 commit d49162b

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

interactions/api/models/presence.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import time
2+
from enum import IntEnum
23

4+
from ..models import StatusType
35
from .misc import DictSerializerMixin, Snowflake
46

57

@@ -77,12 +79,29 @@ def __init__(self, **kwargs):
7779
super().__init__(**kwargs)
7880

7981

82+
class PresenceActivityType(IntEnum):
83+
"""
84+
A class object representing all supported Discord activity types.
85+
"""
86+
87+
GAME = 0
88+
STREAMING = 1
89+
LISTENING = 2
90+
WATCHING = 3
91+
CUSTOM = 4
92+
COMPETING = 5
93+
94+
8095
class PresenceActivity(DictSerializerMixin):
8196
"""
8297
A class object representing the current activity data of a presence.
8398
99+
.. note::
100+
When using this model to instantiate alongside the client, if you provide a type 1 ( or PresenceActivityType.STREAMING ), then
101+
the ``url`` attribute is necessary.
102+
84103
:ivar str name: The activity name
85-
:ivar str type: The activity type
104+
:ivar Union[int, PresenceActivityType] type: The activity type
86105
:ivar Optional[str] url?: stream url (if type is 1)
87106
:ivar Snowflake created_at: Unix timestamp of when the activity was created to the User's session
88107
:ivar Optional[PresenceTimestamp] timestamps?: Unix timestamps for start and/or end of the game
@@ -128,6 +147,9 @@ class PresenceActivity(DictSerializerMixin):
128147

129148
def __init__(self, **kwargs):
130149
super().__init__(**kwargs)
150+
self._json["type"] = (
151+
self.type.value if isinstance(self.type, PresenceActivityType) else self.type
152+
)
131153
self.application_id = (
132154
Snowflake(self.application_id) if self._json.get("application_id") else None
133155
)
@@ -143,24 +165,43 @@ def __init__(self, **kwargs):
143165
self.assets = PresenceAssets(**self.assets) if self._json.get("assets") else None
144166
self.secrets = PresenceSecrets(**self.secrets) if self._json.get("secrets") else None
145167

168+
@property
169+
def gateway_json(self) -> dict:
170+
"""
171+
This returns the JSON representing the ClientPresence sending via the Gateway.
172+
173+
.. note::
174+
This is NOT used for standard presence activity reading by other users, i.e. User activity reading.
175+
You can use the `_json` attribute instead.
176+
"""
177+
res = {"name": self.name, "type": self.type}
178+
if self.url:
179+
res["url"] = self.url
180+
return res
181+
146182

147183
class ClientPresence(DictSerializerMixin):
148184
"""
149185
An object that symbolizes the presence of the current client's session upon creation.
150186
151187
:ivar Optional[int] since?: Unix time in milliseconds of when the client went idle. None if it is not idle.
152188
:ivar Optional[List[PresenceActivity]] activities: Array of activity objects.
153-
:ivar str status: The client's new status.
189+
:ivar Union[str, StatusType] status: The client's new status.
154190
:ivar bool afk: Whether the client is afk or not.
155191
"""
156192

157193
__slots__ = ("_json", "since", "activities", "status", "afk")
158194

159195
def __init__(self, **kwargs):
160196
super().__init__(**kwargs)
197+
self._json["status"] = (
198+
self.status.value if isinstance(self.status, StatusType) else self.status
199+
)
161200
self.activities = (
162201
[
163-
PresenceActivity(**(activity if isinstance(activity, dict) else activity._json))
202+
PresenceActivity(
203+
**(activity if isinstance(activity, dict) else activity.gateway_json)
204+
)
164205
for activity in self._json.get("activities")
165206
]
166207
if self._json.get("activities")
@@ -172,3 +213,5 @@ def __init__(self, **kwargs):
172213
# If since is not provided by the developer...
173214
self.since = int(time.time() * 1000)
174215
self._json["since"] = self.since
216+
if not self._json.get("afk"):
217+
self._json["afk"] = False

interactions/api/models/presence.pyi

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from typing import List, Optional
1+
from enum import IntEnum
2+
from typing import List, Optional, Union
23

34
from .message import Emoji
45
from .misc import DictSerializerMixin, Snowflake
6+
from ..models import StatusType
7+
58

69
class PresenceParty(DictSerializerMixin):
710
_json: dict
@@ -36,10 +39,18 @@ class PresenceTimestamp(DictSerializerMixin):
3639
end: Optional[int]
3740
def __init__(self, **kwargs): ...
3841

42+
class PresenceActivityType(IntEnum):
43+
GAME: int
44+
STREAMING: int
45+
LISTENING: int
46+
WATCHING: int
47+
CUSTOM: int
48+
COMPETING: int
49+
3950
class PresenceActivity(DictSerializerMixin):
4051
_json: dict
4152
name: str
42-
type: int
53+
type: Union[int, PresenceActivityType]
4354
url: Optional[str]
4455
created_at: Snowflake
4556
timestamps: Optional[PresenceTimestamp]
@@ -54,11 +65,13 @@ class PresenceActivity(DictSerializerMixin):
5465
flags: Optional[int]
5566
buttons: Optional[List[PresenceButtons]]
5667
def __init__(self, **kwargs): ...
68+
@property
69+
def gateway_json(self) -> dict: ...
5770

5871
class ClientPresence(DictSerializerMixin):
5972
_json: dict
6073
since: Optional[int]
6174
activities: Optional[List[PresenceActivity]]
62-
status: str
75+
status: Union[str, StatusType]
6376
afk: bool
6477
def __init__(self, **kwargs): ...

0 commit comments

Comments
 (0)