From acc1ec894d6771d48cee74f5027c0ab921054db0 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Mon, 17 Apr 2023 01:25:55 +0200 Subject: [PATCH 1/5] fix(auditlog): support threads in `extra` attribute --- disnake/audit_logs.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/disnake/audit_logs.py b/disnake/audit_logs.py index c265e3a4bd..a3684dadd7 100644 --- a/disnake/audit_logs.py +++ b/disnake/audit_logs.py @@ -484,7 +484,7 @@ class _AuditLogProxyMemberPrune: class _AuditLogProxyMemberMoveOrMessageDelete: - channel: abc.GuildChannel + channel: Union[abc.GuildChannel, Thread] count: int @@ -493,7 +493,7 @@ class _AuditLogProxyMemberDisconnect: class _AuditLogProxyPinAction: - channel: abc.GuildChannel + channel: Union[abc.GuildChannel, Thread] message_id: int @@ -501,8 +501,8 @@ class _AuditLogProxyStageInstanceAction: channel: abc.GuildChannel -class _AuditLogProxyAutoModBlockMessage: - channel: abc.GuildChannel +class _AuditLogProxyAutoModAction: + channel: Optional[Union[abc.GuildChannel, Thread]] rule_name: str rule_trigger_type: enums.AutoModTriggerType @@ -599,7 +599,9 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: ): elems = { "count": int(extra["count"]), - "channel": self._get_channel(utils._get_as_snowflake(extra, "channel_id")), + "channel": self._get_channel_or_thread( + utils._get_as_snowflake(extra, "channel_id") + ), } self.extra = type("_AuditLogProxy", (), elems)() elif self.action is enums.AuditLogAction.member_disconnect: @@ -611,7 +613,9 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: elif self.action.name.endswith("pin"): # the pin actions have a dict with some information elems = { - "channel": self._get_channel(utils._get_as_snowflake(extra, "channel_id")), + "channel": self._get_channel_or_thread( + utils._get_as_snowflake(extra, "channel_id") + ), "message_id": int(extra["message_id"]), } self.extra = type("_AuditLogProxy", (), elems)() @@ -629,7 +633,9 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: self.extra = role elif self.action.name.startswith("stage_instance"): elems = { - "channel": self._get_channel(utils._get_as_snowflake(extra, "channel_id")), + "channel": self._get_channel_or_thread( + utils._get_as_snowflake(extra, "channel_id") + ) } self.extra = type("_AuditLogProxy", (), elems)() elif self.action is enums.AuditLogAction.application_command_permission_update: @@ -644,7 +650,9 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: enums.AuditLogAction.automod_timeout, ): elems = { - "channel": (self._get_channel(utils._get_as_snowflake(extra, "channel_id"))), + "channel": ( + self._get_channel_or_thread(utils._get_as_snowflake(extra, "channel_id")) + ), "rule_name": extra["auto_moderation_rule_name"], "rule_trigger_type": enums.try_enum( enums.AutoModTriggerType, @@ -661,7 +669,7 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: # _AuditLogProxyMemberDisconnect, # _AuditLogProxyPinAction, # _AuditLogProxyStageInstanceAction, - # _AuditLogProxyAutoModBlockMessage, + # _AuditLogProxyAutoModAction, # Member, User, None, # Role, # ] @@ -681,10 +689,12 @@ def _get_member(self, user_id: Optional[int]) -> Union[Member, User, Object, Non return None return self.guild.get_member(user_id) or self._users.get(user_id) or Object(id=user_id) - def _get_channel(self, channel_id: Optional[int]) -> Union[abc.GuildChannel, Object, None]: + def _get_channel_or_thread( + self, channel_id: Optional[int] + ) -> Union[abc.GuildChannel, Thread, Object, None]: if not channel_id: return None - return self.guild.get_channel(channel_id) or Object(channel_id) + return self.guild.get_channel_or_thread(channel_id) or Object(channel_id) def _get_integration_by_application_id( self, application_id: int From 5b590f467d4da902b08436602d4b2fdd0c05e62b Mon Sep 17 00:00:00 2001 From: shiftinv Date: Mon, 17 Apr 2023 01:26:31 +0200 Subject: [PATCH 2/5] fix(auditlog): future-proof `member_prune` extras --- disnake/audit_logs.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/disnake/audit_logs.py b/disnake/audit_logs.py index a3684dadd7..d1e2760bb9 100644 --- a/disnake/audit_logs.py +++ b/disnake/audit_logs.py @@ -590,9 +590,11 @@ def _from_data(self, data: AuditLogEntryPayload) -> None: if isinstance(self.action, enums.AuditLogAction) and extra: if self.action is enums.AuditLogAction.member_prune: # member prune has two keys with useful information - self.extra = type( - "_AuditLogProxy", (), {k: int(v) for k, v in extra.items()} # type: ignore - )() + elems = { + "delete_member_days": utils._get_as_snowflake(extra, "delete_member_days"), + "members_removed": utils._get_as_snowflake(extra, "members_removed"), + } + self.extra = type("_AuditLogProxy", (), elems)() elif ( self.action is enums.AuditLogAction.member_move or self.action is enums.AuditLogAction.message_delete From cc4b298bd9aa15029ad1e01b7fcd3a3ba7292116 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Mon, 17 Apr 2023 01:31:28 +0200 Subject: [PATCH 3/5] docs: update `extra` field docs --- docs/api/audit_logs.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/api/audit_logs.rst b/docs/api/audit_logs.rst index 2ca44cff20..cee3eeb418 100644 --- a/docs/api/audit_logs.rst +++ b/docs/api/audit_logs.rst @@ -996,7 +996,7 @@ AuditLogAction When this is the action, the type of :attr:`~AuditLogEntry.extra` is set to an unspecified proxy object with two attributes: - - ``channel``: A :class:`TextChannel` or :class:`Object` with the channel ID where the members were moved. + - ``channel``: A :class:`abc.GuildChannel` or :class:`Object` with the channel ID where the members were moved. - ``count``: An integer specifying how many members were moved. .. versionadded:: 1.3 @@ -1230,7 +1230,7 @@ AuditLogAction set to an unspecified proxy object with two attributes: - ``count``: An integer specifying how many messages were deleted. - - ``channel``: A :class:`TextChannel` or :class:`Object` with the channel ID where the message got deleted. + - ``channel``: A :class:`abc.GuildChannel`, :class:`Thread` or :class:`Object` with the channel ID where the message got deleted. .. attribute:: message_bulk_delete @@ -1257,7 +1257,7 @@ AuditLogAction When this is the action, the type of :attr:`~AuditLogEntry.extra` is set to an unspecified proxy object with two attributes: - - ``channel``: A :class:`TextChannel` or :class:`Object` with the channel ID where the message was pinned. + - ``channel``: A :class:`abc.GuildChannel`, :class:`Thread` or :class:`Object` with the channel ID where the message was pinned. - ``message_id``: the ID of the message which was pinned. .. versionadded:: 1.3 @@ -1273,7 +1273,7 @@ AuditLogAction When this is the action, the type of :attr:`~AuditLogEntry.extra` is set to an unspecified proxy object with two attributes: - - ``channel``: A :class:`TextChannel` or :class:`Object` with the channel ID where the message was unpinned. + - ``channel``: A :class:`abc.GuildChannel`, :class:`Thread` or :class:`Object` with the channel ID where the message was unpinned. - ``message_id``: the ID of the message which was unpinned. .. versionadded:: 1.3 @@ -1649,7 +1649,7 @@ AuditLogAction When this is the action, the type of :attr:`~AuditLogEntry.extra` is set to an unspecified proxy object with these attributes: - - ``channel``: A :class:`~abc.GuildChannel`, :class:`Thread` or :class:`Object` with the channel ID where the message got blocked. May also be ``None``. + - ``channel``: A :class:`abc.GuildChannel`, :class:`Thread` or :class:`Object` with the channel ID where the message got blocked. May also be ``None``. - ``rule_name``: A :class:`str` with the name of the rule that matched. - ``rule_trigger_type``: An :class:`AutoModTriggerType` value with the trigger type of the rule. From 639a926b5c876ddc0741074c1ef398722e1076f8 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Mon, 17 Apr 2023 01:32:54 +0200 Subject: [PATCH 4/5] docs: add changelog entry --- changelog/1009.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/1009.bugfix.rst diff --git a/changelog/1009.bugfix.rst b/changelog/1009.bugfix.rst new file mode 100644 index 0000000000..666a94a2fd --- /dev/null +++ b/changelog/1009.bugfix.rst @@ -0,0 +1 @@ +Fix threads not being returned in :attr:`AuditLogEntry.extra` in some cases. From f8c78be2c310de2aa3ef88be999fd25b428f11eb Mon Sep 17 00:00:00 2001 From: shiftinv <8530778+shiftinv@users.noreply.github.com> Date: Mon, 12 Jun 2023 14:40:17 +0200 Subject: [PATCH 5/5] docs(changelog): improve phrasing Co-authored-by: arl Signed-off-by: shiftinv <8530778+shiftinv@users.noreply.github.com> --- changelog/1009.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/1009.bugfix.rst b/changelog/1009.bugfix.rst index 666a94a2fd..91ef65702a 100644 --- a/changelog/1009.bugfix.rst +++ b/changelog/1009.bugfix.rst @@ -1 +1 @@ -Fix threads not being returned in :attr:`AuditLogEntry.extra` in some cases. +Fix some instances where threads were not being returned in :attr:`AuditLogEntry.extra`.