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

fix(auditlog): support threads in extra attribute #1009

Merged
merged 6 commits into from Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1009.bugfix.rst
@@ -0,0 +1 @@
Fix some instances where threads were not being returned in :attr:`AuditLogEntry.extra`.
40 changes: 26 additions & 14 deletions disnake/audit_logs.py
Expand Up @@ -484,7 +484,7 @@ class _AuditLogProxyMemberPrune:


class _AuditLogProxyMemberMoveOrMessageDelete:
channel: abc.GuildChannel
channel: Union[abc.GuildChannel, Thread]
count: int


Expand All @@ -493,16 +493,16 @@ class _AuditLogProxyMemberDisconnect:


class _AuditLogProxyPinAction:
channel: abc.GuildChannel
channel: Union[abc.GuildChannel, Thread]
message_id: int


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

Expand Down Expand Up @@ -590,16 +590,20 @@ 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
):
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:
Expand All @@ -611,7 +615,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)()
Expand All @@ -629,7 +635,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:
Expand All @@ -644,7 +652,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,
Expand All @@ -661,7 +671,7 @@ def _from_data(self, data: AuditLogEntryPayload) -> None:
# _AuditLogProxyMemberDisconnect,
# _AuditLogProxyPinAction,
# _AuditLogProxyStageInstanceAction,
# _AuditLogProxyAutoModBlockMessage,
# _AuditLogProxyAutoModAction,
# Member, User, None,
# Role,
# ]
Expand All @@ -681,10 +691,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
Expand Down
10 changes: 5 additions & 5 deletions docs/api/audit_logs.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down