From 0dc05ae5e27ea745b1fd0c9ed1d4991a84393699 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 12 Jan 2023 18:16:36 -0500 Subject: [PATCH] Add support for on_audit_log_entry_create event --- discord/audit_logs.py | 8 ++++++-- discord/role.py | 2 +- discord/state.py | 16 ++++++++++++++++ discord/types/gateway.py | 5 +++++ docs/api.rst | 28 ++++++++++++++++++++-------- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/discord/audit_logs.py b/discord/audit_logs.py index c5cbdc848..220622a13 100644 --- a/discord/audit_logs.py +++ b/discord/audit_logs.py @@ -508,6 +508,10 @@ class AuditLogEntry(Hashable): user: :class:`abc.User` The user who initiated this action. Usually a :class:`Member`\, unless gone then it's a :class:`User`. + user_id: :class:`int` + The user ID who initiated this action. + + .. versionadded:: 2.0 id: :class:`int` The entry ID. target: Any @@ -623,8 +627,8 @@ class AuditLogEntry(Hashable): # into meaningful data when requested self._changes = data.get('changes', []) - user_id = utils._get_as_snowflake(data, 'user_id') - self.user: Optional[Union[User, Member]] = self._get_member(user_id) + self.user_id = utils._get_as_snowflake(data, 'user_id') + self.user: Optional[Union[User, Member]] = self._get_member(self.user_id) self._target_id = utils._get_as_snowflake(data, 'target_id') def _get_member(self, user_id: Optional[int]) -> Union[Member, User, None]: diff --git a/discord/role.py b/discord/role.py index 774a853b4..14036a440 100644 --- a/discord/role.py +++ b/discord/role.py @@ -116,7 +116,7 @@ class RoleTags: def is_guild_connection(self) -> bool: """:class:`bool`: Whether the role is a guild's linked role. - .. versionadded:: 2.2 + .. versionadded:: 2.0 """ return self._guild_connections is None diff --git a/discord/state.py b/discord/state.py index abdd60b78..6fb1f8d93 100644 --- a/discord/state.py +++ b/discord/state.py @@ -94,6 +94,7 @@ from .entitlements import Entitlement, Gift from .guild_premium import PremiumGuildSubscriptionSlot from .library import LibraryApplication from .automod import AutoModRule, AutoModAction +from .audit_logs import AuditLogEntry if TYPE_CHECKING: from typing_extensions import Self @@ -1979,6 +1980,21 @@ class ConnectionState: guild.stickers = tuple(map(lambda d: self.store_sticker(guild, d), data['stickers'])) self.dispatch('guild_stickers_update', guild, before_stickers, guild.stickers) + def parse_guild_audit_log_entry_create(self, data: gw.GuildAuditLogEntryCreate) -> None: + guild = self._get_guild(int(data['guild_id'])) + if guild is None: + _log.debug('GUILD_AUDIT_LOG_ENTRY_CREATE referencing an unknown guild ID: %s. Discarding.', data['guild_id']) + return + + entry = AuditLogEntry( + users=self._users, # type: ignore + automod_rules={}, + data=data, + guild=guild, + ) + + self.dispatch('audit_log_entry_create', entry) + def parse_auto_moderation_rule_create(self, data: AutoModerationRule) -> None: guild = self._get_guild(int(data['guild_id'])) if guild is None: diff --git a/discord/types/gateway.py b/discord/types/gateway.py index 5a1128cb4..097dc9787 100644 --- a/discord/types/gateway.py +++ b/discord/types/gateway.py @@ -48,6 +48,7 @@ from .subscriptions import PremiumGuildSubscriptionSlot from .payments import Payment from .entitlements import Entitlement, GatewayGift from .library import LibraryApplication +from .audit_log import AuditLogEntry class UserPresenceUpdateEvent(TypedDict): @@ -479,3 +480,7 @@ class AutoModerationActionExecution(TypedDict): content: str matched_keyword: Optional[str] matched_content: Optional[str] + + +class GuildAuditLogEntryCreate(AuditLogEntry): + guild_id: Snowflake diff --git a/docs/api.rst b/docs/api.rst index 448884caa..6320f64e6 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -166,8 +166,6 @@ AutoMod Called when a :class:`AutoModRule` is created. You must have :attr:`~Permissions.manage_guild` to receive this. - This requires :attr:`Intents.auto_moderation_configuration` to be enabled. - .. versionadded:: 2.0 :param rule: The rule that was created. @@ -178,8 +176,6 @@ AutoMod Called when a :class:`AutoModRule` is updated. You must have :attr:`~Permissions.manage_guild` to receive this. - This requires :attr:`Intents.auto_moderation_configuration` to be enabled. - .. versionadded:: 2.0 :param rule: The rule that was updated. @@ -190,8 +186,6 @@ AutoMod Called when a :class:`AutoModRule` is deleted. You must have :attr:`~Permissions.manage_guild` to receive this. - This requires :attr:`Intents.auto_moderation_configuration` to be enabled. - .. versionadded:: 2.0 :param rule: The rule that was deleted. @@ -202,8 +196,6 @@ AutoMod Called when a :class:`AutoModAction` is created/performed. You must have :attr:`~Permissions.manage_guild` to receive this. - This requires :attr:`Intents.auto_moderation_execution` to be enabled. - .. versionadded:: 2.0 :param execution: The rule execution that was performed. @@ -768,6 +760,26 @@ Guilds :param after: A list of stickers after the update. :type after: Sequence[:class:`GuildSticker`] +.. function:: on_audit_log_entry_create(entry) + + Called when a :class:`Guild` gets a new audit log entry. + You must have :attr:`~Permissions.view_audit_log` to receive this. + + .. versionadded:: 2.0 + + .. warning:: + + Audit log entries received through the gateway are subject to data retrieval + from cache rather than REST. This means that some data might not be present + when you expect it to be. For example, the :attr:`AuditLogEntry.target` + attribute will usually be a :class:`discord.Object` and the + :attr:`AuditLogEntry.user` attribute will depend on user and member cache. + + To get the user ID of entry, :attr:`AuditLogEntry.user_id` can be used instead. + + :param entry: The audit log entry that was created. + :type entry: :class:`AuditLogEntry` + .. function:: on_invite_create(invite) Called when an :class:`Invite` is created.