Skip to content

Commit 05b5ae3

Browse files
NeloBlivionpre-commit-ci[bot]plun1331Lulalaby
authored
fix: Improve Automod compatibility with Audit Logs + minor fixes (#2030)
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: plun1331 <[email protected]> Co-authored-by: Lala Sabathil <[email protected]>
1 parent 2544830 commit 05b5ae3

File tree

6 files changed

+163
-9
lines changed

6 files changed

+163
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ These changes are available on the `master` branch, but have not yet been releas
3737
- Added the `data` attribute to all
3838
[Raw Event payloads](https://docs.pycord.dev/en/master/api/models.html#events).
3939
([#2023](https://github.com/Pycord-Development/pycord/pull/2023))
40+
- Added and documented missing `AuditLogAction` enums.
41+
([#2030](https://github.com/Pycord-Development/pycord/pull/2030))
42+
- `AuditLogDiff` now supports AutoMod related models.
43+
([#2030](https://github.com/Pycord-Development/pycord/pull/2030))
4044
- Added `Interaction.respond` and `Interaction.edit` as shortcut responses.
4145
([#2026](https://github.com/Pycord-Development/pycord/pull/2026))
4246

4347
### Changed
4448

49+
- Suppressed FFMPEG output when recording voice channels.
50+
([#1993](https://github.com/Pycord-Development/pycord/pull/1993))
4551
- Changed file-upload size limit from 8 MB to 25 MB accordingly.
4652
([#2014](https://github.com/Pycord-Development/pycord/pull/2014))
4753
- `Interaction.channel` is received from the gateway, so it can now be `DMChannel` and
@@ -54,11 +60,6 @@ These changes are available on the `master` branch, but have not yet been releas
5460
- Removed `@client.once()` in favour of `@client.listen(once=True)`.
5561
([#1957](https://github.com/Pycord-Development/pycord/pull/1957))
5662

57-
### Changed
58-
59-
- Suppressed FFMPEG output when recording voice channels.
60-
([#1993](https://github.com/Pycord-Development/pycord/pull/1993))
61-
6263
### Fixed
6364

6465
- Fixed `AttributeError` caused by
@@ -78,7 +79,7 @@ These changes are available on the `master` branch, but have not yet been releas
7879
- Fixed `TypeError` being raised when passing `name` argument to bridge groups.
7980
([#2000](https://github.com/Pycord-Development/pycord/pull/2000))
8081
- Fixed `TypeError` in AutoModRule.
81-
- ([#2029](https://github.com/Pycord-Development/pycord/pull/2029))
82+
([#2029](https://github.com/Pycord-Development/pycord/pull/2029))
8283

8384
## [2.4.1] - 2023-03-20
8485

discord/audit_logs.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from . import enums, utils
3131
from .asset import Asset
32+
from .automod import AutoModAction, AutoModTriggerMetadata
3233
from .colour import Colour
3334
from .invite import Invite
3435
from .mixins import Hashable
@@ -57,6 +58,8 @@
5758
from .threads import Thread
5859
from .types.audit_log import AuditLogChange as AuditLogChangePayload
5960
from .types.audit_log import AuditLogEntry as AuditLogEntryPayload
61+
from .types.automod import AutoModAction as AutoModActionPayload
62+
from .types.automod import AutoModTriggerMetadata as AutoModTriggerMetadataPayload
6063
from .types.channel import PermissionOverwrite as PermissionOverwritePayload
6164
from .types.role import Role as RolePayload
6265
from .types.snowflake import Snowflake
@@ -83,6 +86,22 @@ def _transform_channel(
8386
return entry.guild.get_channel(int(data)) or Object(id=data)
8487

8588

89+
def _transform_channels(
90+
entry: AuditLogEntry, data: list[Snowflake] | None
91+
) -> list[abc.GuildChannel | Object] | None:
92+
if data is None:
93+
return None
94+
return [_transform_channel(entry, channel) for channel in data]
95+
96+
97+
def _transform_roles(
98+
entry: AuditLogEntry, data: list[Snowflake] | None
99+
) -> list[Role | Object] | None:
100+
if data is None:
101+
return None
102+
return [entry.guild.get_role(int(r)) or Object(id=r) for r in data]
103+
104+
86105
def _transform_member_id(
87106
entry: AuditLogEntry, data: Snowflake | None
88107
) -> Member | User | None:
@@ -172,6 +191,24 @@ def _transform_type(
172191
return enums.try_enum(enums.ChannelType, data)
173192

174193

194+
def _transform_actions(
195+
entry: AuditLogEntry, data: list[AutoModActionPayload] | None
196+
) -> AutoModAction | None:
197+
if data is None:
198+
return None
199+
else:
200+
return [AutoModAction.from_dict(d) for d in data]
201+
202+
203+
def _transform_trigger_metadata(
204+
entry: AuditLogEntry, data: list[AutoModActionPayload] | None
205+
) -> AutoModAction | None:
206+
if data is None:
207+
return None
208+
else:
209+
return AutoModTriggerMetadata.from_dict(data)
210+
211+
175212
class AuditLogDiff:
176213
def __len__(self) -> int:
177214
return len(self.__dict__)
@@ -240,6 +277,12 @@ class AuditLogChanges:
240277
),
241278
"command_id": ("command_id", _transform_snowflake),
242279
"image_hash": ("cover", _transform_scheduled_event_cover),
280+
"trigger_type": (None, _enum_transformer(enums.AutoModTriggerType)),
281+
"event_type": (None, _enum_transformer(enums.AutoModEventType)),
282+
"actions": (None, _transform_actions),
283+
"trigger_metadata": (None, _transform_trigger_metadata),
284+
"exempt_roles": (None, _transform_roles),
285+
"exempt_channels": (None, _transform_channels),
243286
}
244287

245288
def __init__(
@@ -255,13 +298,31 @@ def __init__(
255298
for elem in sorted(data, key=lambda i: i["key"]):
256299
attr = elem["key"]
257300

258-
# special cases for role add/remove
301+
# special cases for role/trigger_metadata add/remove
259302
if attr == "$add":
260303
self._handle_role(self.before, self.after, entry, elem["new_value"]) # type: ignore
261304
continue
262305
elif attr == "$remove":
263306
self._handle_role(self.after, self.before, entry, elem["new_value"]) # type: ignore
264307
continue
308+
elif attr in [
309+
"$add_keyword_filter",
310+
"$add_regex_patterns",
311+
"$add_allow_list",
312+
]:
313+
self._handle_trigger_metadata(
314+
self.before, self.after, entry, elem["new_value"], attr
315+
)
316+
continue
317+
elif attr in [
318+
"$remove_keyword_filter",
319+
"$remove_regex_patterns",
320+
"$remove_allow_list",
321+
]:
322+
self._handle_trigger_metadata(
323+
self.after, self.before, entry, elem["new_value"], attr
324+
)
325+
continue
265326

266327
try:
267328
key, transformer = self.TRANSFORMERS[attr]
@@ -355,6 +416,23 @@ def _handle_role(
355416

356417
setattr(second, "roles", data)
357418

419+
def _handle_trigger_metadata(
420+
self,
421+
first: AuditLogDiff,
422+
second: AuditLogDiff,
423+
entry: AuditLogEntry,
424+
elem: list[AutoModTriggerMetadataPayload],
425+
attr: str,
426+
) -> None:
427+
if not hasattr(first, "trigger_metadata"):
428+
setattr(first, "trigger_metadata", None)
429+
430+
key = attr.split("_", 1)[-1]
431+
data = {key: elem}
432+
tm = AutoModTriggerMetadata.from_dict(data)
433+
434+
setattr(second, "trigger_metadata", tm)
435+
358436

359437
class _AuditLogProxyMemberPrune:
360438
delete_member_days: int

discord/automod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def __repr__(self) -> str:
317317
inner.append(f"{attr}={value}")
318318
inner = " ".join(inner)
319319

320-
return f"<AutoModActionMetadata {inner}>"
320+
return f"<AutoModTriggerMetadata {inner}>"
321321

322322

323323
class AutoModRule(Hashable):

discord/enums.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ class AuditLogAction(Enum):
438438
auto_moderation_rule_update = 141
439439
auto_moderation_rule_delete = 142
440440
auto_moderation_block_message = 143
441+
auto_moderation_flag_to_channel = 144
442+
auto_moderation_user_communication_disabled = 145
441443

442444
@property
443445
def category(self) -> AuditLogActionCategory | None:
@@ -496,6 +498,8 @@ def category(self) -> AuditLogActionCategory | None:
496498
AuditLogAction.auto_moderation_rule_update: AuditLogActionCategory.update,
497499
AuditLogAction.auto_moderation_rule_delete: AuditLogActionCategory.delete,
498500
AuditLogAction.auto_moderation_block_message: None,
501+
AuditLogAction.auto_moderation_flag_to_channel: None,
502+
AuditLogAction.auto_moderation_user_communication_disabled: None,
499503
}
500504
return lookup[self]
501505

@@ -534,7 +538,7 @@ def target_type(self) -> str | None:
534538
return "thread"
535539
elif v < 122:
536540
return "application_command_permission"
537-
elif v < 144:
541+
elif v < 146:
538542
return "auto_moderation_rule"
539543

540544

discord/types/audit_log.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class AuditEntryInfo(TypedDict):
267267
id: Snowflake
268268
type: Literal["0", "1"]
269269
role_name: str
270+
application_id: Snowflake
271+
auto_moderation_rule_name: str
272+
auto_moderation_rule_trigger_type: str
270273

271274

272275
class AuditLogEntry(TypedDict):

docs/api/enums.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,74 @@ of :class:`enum.Enum`.
15121512

15131513
.. versionadded:: 2.0
15141514

1515+
.. attribute:: auto_moderation_rule_create
1516+
1517+
A guild auto moderation rule was created.
1518+
1519+
Possible attributes for :class:`AuditLogDiff`:
1520+
1521+
- :attr:`~AuditLogDiff.name`
1522+
- :attr:`~AuditLogDiff.enabled`
1523+
- :attr:`~AuditLogDiff.trigger_type`
1524+
- :attr:`~AuditLogDiff.event_type`
1525+
- :attr:`~AuditLogDiff.trigger_metadata`
1526+
- :attr:`~AuditLogDiff.actions`
1527+
- :attr:`~AuditLogDiff.exempt_roles`
1528+
- :attr:`~AuditLogDiff.exempt_channels`
1529+
1530+
.. versionadded:: 2.5
1531+
1532+
.. attribute:: auto_moderation_rule_update
1533+
1534+
A guild auto moderation rule was updated.
1535+
1536+
Possible attributes for :class:`AuditLogDiff`:
1537+
1538+
- :attr:`~AuditLogDiff.name`
1539+
- :attr:`~AuditLogDiff.enabled`
1540+
- :attr:`~AuditLogDiff.trigger_type`
1541+
- :attr:`~AuditLogDiff.trigger_metadata`
1542+
- :attr:`~AuditLogDiff.actions`
1543+
- :attr:`~AuditLogDiff.exempt_roles`
1544+
- :attr:`~AuditLogDiff.exempt_channels`
1545+
1546+
.. versionadded:: 2.5
1547+
1548+
.. attribute:: auto_moderation_rule_delete
1549+
1550+
A guild auto moderation rule was deleted.
1551+
1552+
Possible attributes for :class:`AuditLogDiff`:
1553+
1554+
- :attr:`~AuditLogDiff.name`
1555+
- :attr:`~AuditLogDiff.enabled`
1556+
- :attr:`~AuditLogDiff.trigger_type`
1557+
- :attr:`~AuditLogDiff.event_type`
1558+
- :attr:`~AuditLogDiff.trigger_metadata`
1559+
- :attr:`~AuditLogDiff.actions`
1560+
- :attr:`~AuditLogDiff.exempt_roles`
1561+
- :attr:`~AuditLogDiff.exempt_channels`
1562+
1563+
.. versionadded:: 2.5
1564+
1565+
.. attribute:: auto_moderation_block_message
1566+
1567+
A message was blocked by auto moderation.
1568+
1569+
.. versionadded:: 2.5
1570+
1571+
.. attribute:: auto_moderation_flag_to_channel
1572+
1573+
A message was flagged by auto moderation.
1574+
1575+
.. versionadded:: 2.5
1576+
1577+
.. attribute:: auto_moderation_user_communication_disabled
1578+
1579+
A member was timed out by auto moderation.
1580+
1581+
.. versionadded:: 2.5
1582+
15151583

15161584
.. class:: AuditLogActionCategory
15171585

0 commit comments

Comments
 (0)