Browse Source

Add support for on_audit_log_entry_create event

pull/10109/head
Rapptz 2 years ago
committed by dolfies
parent
commit
0dc05ae5e2
  1. 8
      discord/audit_logs.py
  2. 2
      discord/role.py
  3. 16
      discord/state.py
  4. 5
      discord/types/gateway.py
  5. 28
      docs/api.rst

8
discord/audit_logs.py

@ -508,6 +508,10 @@ class AuditLogEntry(Hashable):
user: :class:`abc.User` user: :class:`abc.User`
The user who initiated this action. Usually a :class:`Member`\, unless gone The user who initiated this action. Usually a :class:`Member`\, unless gone
then it's a :class:`User`. then it's a :class:`User`.
user_id: :class:`int`
The user ID who initiated this action.
.. versionadded:: 2.0
id: :class:`int` id: :class:`int`
The entry ID. The entry ID.
target: Any target: Any
@ -623,8 +627,8 @@ class AuditLogEntry(Hashable):
# into meaningful data when requested # into meaningful data when requested
self._changes = data.get('changes', []) self._changes = data.get('changes', [])
user_id = utils._get_as_snowflake(data, 'user_id') self.user_id = utils._get_as_snowflake(data, 'user_id')
self.user: Optional[Union[User, Member]] = self._get_member(user_id) self.user: Optional[Union[User, Member]] = self._get_member(self.user_id)
self._target_id = utils._get_as_snowflake(data, 'target_id') self._target_id = utils._get_as_snowflake(data, 'target_id')
def _get_member(self, user_id: Optional[int]) -> Union[Member, User, None]: def _get_member(self, user_id: Optional[int]) -> Union[Member, User, None]:

2
discord/role.py

@ -116,7 +116,7 @@ class RoleTags:
def is_guild_connection(self) -> bool: def is_guild_connection(self) -> bool:
""":class:`bool`: Whether the role is a guild's linked role. """:class:`bool`: Whether the role is a guild's linked role.
.. versionadded:: 2.2 .. versionadded:: 2.0
""" """
return self._guild_connections is None return self._guild_connections is None

16
discord/state.py

@ -94,6 +94,7 @@ from .entitlements import Entitlement, Gift
from .guild_premium import PremiumGuildSubscriptionSlot from .guild_premium import PremiumGuildSubscriptionSlot
from .library import LibraryApplication from .library import LibraryApplication
from .automod import AutoModRule, AutoModAction from .automod import AutoModRule, AutoModAction
from .audit_logs import AuditLogEntry
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Self from typing_extensions import Self
@ -1979,6 +1980,21 @@ class ConnectionState:
guild.stickers = tuple(map(lambda d: self.store_sticker(guild, d), data['stickers'])) guild.stickers = tuple(map(lambda d: self.store_sticker(guild, d), data['stickers']))
self.dispatch('guild_stickers_update', guild, before_stickers, guild.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: def parse_auto_moderation_rule_create(self, data: AutoModerationRule) -> None:
guild = self._get_guild(int(data['guild_id'])) guild = self._get_guild(int(data['guild_id']))
if guild is None: if guild is None:

5
discord/types/gateway.py

@ -48,6 +48,7 @@ from .subscriptions import PremiumGuildSubscriptionSlot
from .payments import Payment from .payments import Payment
from .entitlements import Entitlement, GatewayGift from .entitlements import Entitlement, GatewayGift
from .library import LibraryApplication from .library import LibraryApplication
from .audit_log import AuditLogEntry
class UserPresenceUpdateEvent(TypedDict): class UserPresenceUpdateEvent(TypedDict):
@ -479,3 +480,7 @@ class AutoModerationActionExecution(TypedDict):
content: str content: str
matched_keyword: Optional[str] matched_keyword: Optional[str]
matched_content: Optional[str] matched_content: Optional[str]
class GuildAuditLogEntryCreate(AuditLogEntry):
guild_id: Snowflake

28
docs/api.rst

@ -166,8 +166,6 @@ AutoMod
Called when a :class:`AutoModRule` is created. Called when a :class:`AutoModRule` is created.
You must have :attr:`~Permissions.manage_guild` to receive this. You must have :attr:`~Permissions.manage_guild` to receive this.
This requires :attr:`Intents.auto_moderation_configuration` to be enabled.
.. versionadded:: 2.0 .. versionadded:: 2.0
:param rule: The rule that was created. :param rule: The rule that was created.
@ -178,8 +176,6 @@ AutoMod
Called when a :class:`AutoModRule` is updated. Called when a :class:`AutoModRule` is updated.
You must have :attr:`~Permissions.manage_guild` to receive this. You must have :attr:`~Permissions.manage_guild` to receive this.
This requires :attr:`Intents.auto_moderation_configuration` to be enabled.
.. versionadded:: 2.0 .. versionadded:: 2.0
:param rule: The rule that was updated. :param rule: The rule that was updated.
@ -190,8 +186,6 @@ AutoMod
Called when a :class:`AutoModRule` is deleted. Called when a :class:`AutoModRule` is deleted.
You must have :attr:`~Permissions.manage_guild` to receive this. You must have :attr:`~Permissions.manage_guild` to receive this.
This requires :attr:`Intents.auto_moderation_configuration` to be enabled.
.. versionadded:: 2.0 .. versionadded:: 2.0
:param rule: The rule that was deleted. :param rule: The rule that was deleted.
@ -202,8 +196,6 @@ AutoMod
Called when a :class:`AutoModAction` is created/performed. Called when a :class:`AutoModAction` is created/performed.
You must have :attr:`~Permissions.manage_guild` to receive this. You must have :attr:`~Permissions.manage_guild` to receive this.
This requires :attr:`Intents.auto_moderation_execution` to be enabled.
.. versionadded:: 2.0 .. versionadded:: 2.0
:param execution: The rule execution that was performed. :param execution: The rule execution that was performed.
@ -768,6 +760,26 @@ Guilds
:param after: A list of stickers after the update. :param after: A list of stickers after the update.
:type after: Sequence[:class:`GuildSticker`] :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) .. function:: on_invite_create(invite)
Called when an :class:`Invite` is created. Called when an :class:`Invite` is created.

Loading…
Cancel
Save