Skip to content

Commit

Permalink
Go!
Browse files Browse the repository at this point in the history
  • Loading branch information
elenakrittik committed Jan 17, 2024
1 parent d782f54 commit 7d8b504
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/1152.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add raw equivalent of :func:`on_presence_update` - :func:`on_raw_presence_update`.
3 changes: 3 additions & 0 deletions disnake/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,9 @@ class Event(Enum):
"""Called when a message has a specific reaction removed from it.
Represents the :func:`on_reaction_clear_emoji` event.
"""
raw_presence_update = "raw_presence_update"
"""Called when a user's presence changes regardless of the state of the internal member cache.
Represents the :func:`on_raw_presence_update` event."""
raw_reaction_add = "raw_reaction_add"
"""Called when a message has a reaction added regardless of the state of the internal message cache.
Represents the :func:`on_raw_reaction_add` event.
Expand Down
29 changes: 29 additions & 0 deletions disnake/raw_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
MessageReactionRemoveEmojiEvent,
MessageReactionRemoveEvent,
MessageUpdateEvent,
PresenceUpdateEvent,
ThreadDeleteEvent,
TypingStartEvent,
)
Expand All @@ -43,6 +44,7 @@
"RawThreadMemberRemoveEvent",
"RawTypingEvent",
"RawGuildMemberRemoveEvent",
"RawPresenceUpdateEvent",
)


Expand Down Expand Up @@ -446,3 +448,30 @@ class RawGuildMemberRemoveEvent(_RawReprMixin):
def __init__(self, user: Union[User, Member], guild_id: int) -> None:
self.user: Union[User, Member] = user
self.guild_id: int = guild_id


class RawPresenceUpdateEvent(_RawReprMixin):
"""Represents the event payload for an :func:`on_raw_presence_update` event.
.. versionadded:: 2.10
Attributes
----------
user_id: :class:`int`
The ID of the user whose presence was updated.
guild_id: :class:`int`
The ID of the guild where the user's presence changed.
data: :class:`dict`
The raw data given by the :ddocs:`gateway <topics/gateway-events#presence-update>`.
"""

__slots__ = (
"user_id",
"guild_id",
"data",
)

def __init__(self, data: PresenceUpdateEvent) -> None:
self.user_id = int(data["user"]["id"])
self.guild_id = int(data["guild_id"])
self.data = data
7 changes: 4 additions & 3 deletions disnake/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
RawIntegrationDeleteEvent,
RawMessageDeleteEvent,
RawMessageUpdateEvent,
RawPresenceUpdateEvent,
RawReactionActionEvent,
RawReactionClearEmojiEvent,
RawReactionClearEvent,
Expand Down Expand Up @@ -973,13 +974,13 @@ def parse_presence_update(self, data: gateway.PresenceUpdateEvent) -> None:
_log.debug("PRESENCE_UPDATE referencing an unknown guild ID: %s. Discarding.", guild_id)
return

raw = RawPresenceUpdateEvent(data)
self.dispatch("raw_presence_update", raw)

user = data["user"]
member_id = int(user["id"])
member = guild.get_member(member_id)
if member is None:
_log.debug(
"PRESENCE_UPDATE referencing an unknown member ID: %s. Discarding", member_id
)
return

old_member = Member._copy(member)
Expand Down
17 changes: 17 additions & 0 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,23 @@ Members
:param after: The updated member's updated info.
:type after: :class:`Member`

.. function:: on_raw_presence_update(payload)

Called when a :class:`User` updates their presence.
Unlike :func:`on_presence_update`, this is called regardless of the member cache.

This is called when one or more of the following things change:

- status
- activity

This requires :attr:`Intents.presences` and :attr:`Intents.members` to be enabled.

.. versionadded:: 2.10

:param payload: The raw event payload data.
:type payload: :class:`RawPresenceUpdateEvent`

.. function:: on_user_update(before, after)

Called when a :class:`User` is updated.
Expand Down
9 changes: 9 additions & 0 deletions docs/api/members.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ RawGuildMemberRemoveEvent
.. autoclass:: RawGuildMemberRemoveEvent()
:members:

RawPresenceUpdateEvent
~~~~~~~~~~~~~~~~~~~~~~

.. attributetable:: RawGuildMemberRemoveEvent

.. autoclass:: RawPresenceUpdateEvent()
:members:

Events
------

Expand All @@ -45,4 +53,5 @@ Events
- :func:`on_member_ban(guild, user) <disnake.on_member_ban>`
- :func:`on_member_unban(guild, user) <disnake.on_member_unban>`
- :func:`on_presence_update(before, after) <disnake.on_presence_update>`
- :func:`on_raw_presence_update(payload) <disnake.on_raw_presence_update>`
- :func:`on_user_update(before, after) <disnake.on_user_update>`
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ keywords = ["disnake", "discord", "discord api"]
license = { text = "MIT" }
dependencies = [
"aiohttp>=3.7.0,<4.0",
"esbonio>=0.16.4",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 7d8b504

Please sign in to comment.