Skip to content

Commit

Permalink
feat: Add data attribute to all raw event payloads (#2023)
Browse files Browse the repository at this point in the history
Signed-off-by: Lala Sabathil <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lala Sabathil <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2023
1 parent 3e534b7 commit 0f7b46e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ These changes are available on the `master` branch, but have not yet been releas
- Added support for
[voice messages](https://github.com/discord/discord-api-docs/pull/6082).
([#2016](https://github.com/Pycord-Development/pycord/pull/2016))
- Added the `data` attribute to all
[Raw Event payloads](https://docs.pycord.dev/en/master/api/models.html#events).
([#2023](https://github.com/Pycord-Development/pycord/pull/2023))

### Changed

Expand Down
90 changes: 75 additions & 15 deletions discord/raw_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ class RawMessageDeleteEvent(_RawReprMixin):
The message ID that got deleted.
cached_message: Optional[:class:`Message`]
The cached message, if found in the internal message cache.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#message-delete>`_.
.. versionadded:: 2.5
"""

__slots__ = ("message_id", "channel_id", "guild_id", "cached_message")
__slots__ = ("message_id", "channel_id", "guild_id", "cached_message", "data")

def __init__(self, data: MessageDeleteEvent) -> None:
self.message_id: int = int(data["id"])
Expand All @@ -109,6 +113,7 @@ def __init__(self, data: MessageDeleteEvent) -> None:
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: MessageDeleteEvent = data


class RawBulkMessageDeleteEvent(_RawReprMixin):
Expand All @@ -124,9 +129,13 @@ class RawBulkMessageDeleteEvent(_RawReprMixin):
The guild ID where the message got deleted, if applicable.
cached_messages: List[:class:`Message`]
The cached messages, if found in the internal message cache.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#message-delete-bulk>`_.
.. versionadded:: 2.5
"""

__slots__ = ("message_ids", "channel_id", "guild_id", "cached_messages")
__slots__ = ("message_ids", "channel_id", "guild_id", "cached_messages", "data")

def __init__(self, data: BulkMessageDeleteEvent) -> None:
self.message_ids: set[int] = {int(x) for x in data.get("ids", [])}
Expand All @@ -137,6 +146,7 @@ def __init__(self, data: BulkMessageDeleteEvent) -> None:
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: BulkMessageDeleteEvent = data


class RawMessageUpdateEvent(_RawReprMixin):
Expand All @@ -156,7 +166,7 @@ class RawMessageUpdateEvent(_RawReprMixin):
.. versionadded:: 1.7
data: :class:`dict`
The raw data given by the `gateway <https://discord.com/developers/docs/topics/gateway#message-update>`_
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway#message-update>`_
cached_message: Optional[:class:`Message`]
The cached message, if found in the internal message cache. Represents the message before
it is modified by the data in :attr:`RawMessageUpdateEvent.data`.
Expand Down Expand Up @@ -204,6 +214,10 @@ class RawReactionActionEvent(_RawReprMixin):
``REACTION_REMOVE`` for reaction removal.
.. versionadded:: 1.3
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#message-reaction-add>`_.
.. versionadded:: 2.5
"""

__slots__ = (
Expand All @@ -214,6 +228,7 @@ class RawReactionActionEvent(_RawReprMixin):
"emoji",
"event_type",
"member",
"data",
)

def __init__(
Expand All @@ -230,6 +245,7 @@ def __init__(
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: ReactionActionEvent = data


class RawReactionClearEvent(_RawReprMixin):
Expand All @@ -243,9 +259,13 @@ class RawReactionClearEvent(_RawReprMixin):
The channel ID where the reactions got cleared.
guild_id: Optional[:class:`int`]
The guild ID where the reactions got cleared.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#message-reaction-remove-all>`_.
.. versionadded:: 2.5
"""

__slots__ = ("message_id", "channel_id", "guild_id")
__slots__ = ("message_id", "channel_id", "guild_id", "data")

def __init__(self, data: ReactionClearEvent) -> None:
self.message_id: int = int(data["message_id"])
Expand All @@ -255,6 +275,7 @@ def __init__(self, data: ReactionClearEvent) -> None:
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: ReactionClearEvent = data


class RawReactionClearEmojiEvent(_RawReprMixin):
Expand All @@ -272,9 +293,13 @@ class RawReactionClearEmojiEvent(_RawReprMixin):
The guild ID where the reactions got cleared.
emoji: :class:`PartialEmoji`
The custom or unicode emoji being removed.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#message-reaction-remove-emoji>`_.
.. versionadded:: 2.5
"""

__slots__ = ("message_id", "channel_id", "guild_id", "emoji")
__slots__ = ("message_id", "channel_id", "guild_id", "emoji", "data")

def __init__(self, data: ReactionClearEmojiEvent, emoji: PartialEmoji) -> None:
self.emoji: PartialEmoji = emoji
Expand All @@ -285,6 +310,7 @@ def __init__(self, data: ReactionClearEmojiEvent, emoji: PartialEmoji) -> None:
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: ReactionClearEmojiEvent = data


class RawIntegrationDeleteEvent(_RawReprMixin):
Expand All @@ -300,9 +326,13 @@ class RawIntegrationDeleteEvent(_RawReprMixin):
The ID of the bot/OAuth2 application for this deleted integration.
guild_id: :class:`int`
The guild ID where the integration got deleted.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#integration-delete>`_.
.. versionadded:: 2.5
"""

__slots__ = ("integration_id", "application_id", "guild_id")
__slots__ = ("integration_id", "application_id", "guild_id", "data")

def __init__(self, data: IntegrationDeleteEvent) -> None:
self.integration_id: int = int(data["id"])
Expand All @@ -312,6 +342,7 @@ def __init__(self, data: IntegrationDeleteEvent) -> None:
self.application_id: int | None = int(data["application_id"])
except KeyError:
self.application_id: int | None = None
self.data: IntegrationDeleteEvent = data


class RawThreadUpdateEvent(_RawReprMixin):
Expand All @@ -330,7 +361,7 @@ class RawThreadUpdateEvent(_RawReprMixin):
parent_id: :class:`int`
The ID of the channel the thread belongs to.
data: :class:`dict`
The raw data given by the `gateway <https://discord.com/developers/docs/topics/gateway-events#thread-update>`_.
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#thread-update>`_.
thread: :class:`discord.Thread` | None
The thread, if it could be found in the internal cache.
"""
Expand Down Expand Up @@ -364,16 +395,21 @@ class RawThreadDeleteEvent(_RawReprMixin):
The ID of the channel the thread belonged to.
thread: Optional[:class:`discord.Thread`]
The thread that was deleted. This may be ``None`` if deleted thread is not found in internal cache.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#thread-delete>`_.
.. versionadded:: 2.5
"""

__slots__ = ("thread_id", "thread_type", "guild_id", "parent_id", "thread")
__slots__ = ("thread_id", "thread_type", "guild_id", "parent_id", "thread", "data")

def __init__(self, data: ThreadDeleteEvent) -> None:
self.thread_id: int = int(data["id"])
self.thread_type: ChannelType = try_enum(ChannelType, int(data["type"]))
self.guild_id: int = int(data["guild_id"])
self.parent_id: int = int(data["parent_id"])
self.thread: Thread | None = None
self.data: ThreadDeleteEvent = data


class RawTypingEvent(_RawReprMixin):
Expand All @@ -393,9 +429,13 @@ class RawTypingEvent(_RawReprMixin):
The guild ID where the typing originated from, if applicable.
member: Optional[:class:`Member`]
The member who started typing. Only available if the member started typing in a guild.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#typing-start>`_.
.. versionadded:: 2.5
"""

__slots__ = ("channel_id", "user_id", "when", "guild_id", "member")
__slots__ = ("channel_id", "user_id", "when", "guild_id", "member", "data")

def __init__(self, data: TypingEvent) -> None:
self.channel_id: int = int(data["channel_id"])
Expand All @@ -409,6 +449,7 @@ def __init__(self, data: TypingEvent) -> None:
self.guild_id: int | None = int(data["guild_id"])
except KeyError:
self.guild_id: int | None = None
self.data: TypingEvent = data


class RawMemberRemoveEvent(_RawReprMixin):
Expand All @@ -422,13 +463,18 @@ class RawMemberRemoveEvent(_RawReprMixin):
The user that left the guild.
guild_id: :class:`int`
The ID of the guild the user left.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#guild-member-remove>`_.
.. versionadded:: 2.5
"""

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

def __init__(self, data: MemberRemoveEvent, user: User):
self.user: User = user
self.guild_id: int = int(data["guild_id"])
self.data: MemberRemoveEvent = data


class RawScheduledEventSubscription(_RawReprMixin):
Expand All @@ -448,15 +494,20 @@ class RawScheduledEventSubscription(_RawReprMixin):
event_type: :class:`str`
Can be either ``USER_ADD`` or ``USER_REMOVE`` depending on
the event called.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-user-add>`_.
.. versionadded:: 2.5
"""

__slots__ = ("event_id", "guild", "user_id", "event_type")
__slots__ = ("event_id", "guild", "user_id", "event_type", "data")

def __init__(self, data: ScheduledEventSubscription, event_type: str):
self.event_id: int = int(data["guild_scheduled_event_id"])
self.user_id: int = int(data["user_id"])
self.guild: Guild | None = None
self.event_type: str = event_type
self.data: ScheduledEventSubscription = data


class AutoModActionExecutionEvent:
Expand Down Expand Up @@ -503,6 +554,10 @@ class AutoModActionExecutionEvent:
The word or phrase configured that was matched in the content.
matched_content: :class:`str`
The substring in the content that was matched.
data: :class:`dict`
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#auto-moderation-action-execution>`_.
.. versionadded:: 2.5
"""

__slots__ = (
Expand All @@ -522,6 +577,7 @@ class AutoModActionExecutionEvent:
"message",
"alert_system_message_id",
"alert_system_message",
"data",
)

def __init__(self, state: ConnectionState, data: AutoModActionExecution) -> None:
Expand Down Expand Up @@ -570,6 +626,7 @@ def __init__(self, state: ConnectionState, data: AutoModActionExecution) -> None
except KeyError:
self.alert_system_message_id: int | None = None
self.alert_system_message: Message | None = None
self.data: AutoModActionExecution = data

def __repr__(self) -> str:
return (
Expand All @@ -593,7 +650,9 @@ class RawThreadMembersUpdateEvent(_RawReprMixin):
member_count: :class:`int`
The approximate number of members in the thread. Maximum of 50.
data: :class:`dict`
The raw data given by the `gateway <https://discord.com/developers/docs/topics/gateway-events#thread-members-update>`_.
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#thread-members-update>`_.
.. versionadded:: 2.5
"""

__slots__ = ("thread_id", "guild_id", "member_count", "data")
Expand All @@ -602,7 +661,7 @@ def __init__(self, data: ThreadMembersUpdateEvent) -> None:
self.thread_id = int(data["id"])
self.guild_id = int(data["guild_id"])
self.member_count = int(data["member_count"])
self.data = data
self.data: ThreadMembersUpdateEvent = data


class RawAuditLogEntryEvent(_RawReprMixin):
Expand Down Expand Up @@ -632,7 +691,7 @@ class RawAuditLogEntryEvent(_RawReprMixin):
contains extra information. See :class:`AuditLogAction` for
which actions have this field filled out.
data: :class:`dict`
The raw data given by the `gateway <https://discord.com/developers/docs/topics/gateway-events#guild-audit-log-entry-create>`_.
The raw data sent by the `gateway <https://discord.com/developers/docs/topics/gateway-events#guild-audit-log-entry-create>`_.
"""

__slots__ = (
Expand All @@ -644,6 +703,7 @@ class RawAuditLogEntryEvent(_RawReprMixin):
"reason",
"extra",
"changes",
"data",
)

def __init__(self, data: AuditLogEntryEvent) -> None:
Expand All @@ -657,4 +717,4 @@ def __init__(self, data: AuditLogEntryEvent) -> None:
self.reason = data.get("reason")
self.extra = data.get("options")
self.changes = data.get("changes")
self.data = data
self.data: AuditLogEntryEvent = data

0 comments on commit 0f7b46e

Please sign in to comment.