Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add raw equivalent of on_presence_update - on_raw_presence_update. #1154

Merged
merged 9 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions disnake/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@ def presences(self):
This corresponds to the following events:

- :func:`on_presence_update`
- :func:`on_raw_presence_update`

This also corresponds to the following attributes and classes in terms of cache:

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 = int(data["user"]["id"])
self.guild_id: int = int(data["guild_id"])
self.data: PresenceUpdateEvent = 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
1 change: 1 addition & 0 deletions docs/api/activities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ Events
------

- :func:`on_presence_update(before, after) <disnake.on_presence_update>`
- :func:`on_raw_presence_update(payload) <disnake.on_raw_presence_update>`
12 changes: 12 additions & 0 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,18 @@ Members
:param after: The updated member's updated info.
:type after: :class:`Member`

.. function:: on_raw_presence_update(payload)

Called when a member updates their presence.
Unlike :func:`on_presence_update`, this is called regardless of the member cache.
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

This requires :attr:`Intents.presences` 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:: RawPresenceUpdateEvent

.. 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>`
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
- :func:`on_user_update(before, after) <disnake.on_user_update>`
Loading