Skip to content

Commit d0d7451

Browse files
plun1331amachnLulalaby
authored
Implement Automod Features (#1316)
Co-authored-by: baronkobama <[email protected]> Co-authored-by: Lala Sabathil <[email protected]>
1 parent ef301fc commit d0d7451

File tree

14 files changed

+967
-3
lines changed

14 files changed

+967
-3
lines changed

discord/automod.py

Lines changed: 446 additions & 0 deletions
Large diffs are not rendered by default.

discord/enums.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class MessageType(Enum):
232232
thread_starter_message = 21
233233
guild_invite_reminder = 22
234234
context_menu_command = 23
235+
auto_moderation_action = 24
235236

236237

237238
class VoiceRegion(Enum):
@@ -381,6 +382,10 @@ class AuditLogAction(Enum):
381382
thread_update = 111
382383
thread_delete = 112
383384
application_command_permission_update = 121
385+
auto_moderation_rule_create = 140
386+
auto_moderation_rule_update = 141
387+
auto_moderation_rule_delete = 142
388+
auto_moderation_block_message = 143
384389

385390
@property
386391
def category(self) -> Optional[AuditLogActionCategory]:
@@ -433,6 +438,10 @@ def category(self) -> Optional[AuditLogActionCategory]:
433438
AuditLogAction.thread_update: AuditLogActionCategory.update,
434439
AuditLogAction.thread_delete: AuditLogActionCategory.delete,
435440
AuditLogAction.application_command_permission_update: AuditLogActionCategory.update,
441+
AuditLogAction.auto_moderation_rule_create: AuditLogActionCategory.create,
442+
AuditLogAction.auto_moderation_rule_update: AuditLogActionCategory.update,
443+
AuditLogAction.auto_moderation_rule_delete: AuditLogActionCategory.delete,
444+
AuditLogAction.auto_moderation_block_message: None,
436445
}
437446
return lookup[self]
438447

@@ -471,6 +480,8 @@ def target_type(self) -> Optional[str]:
471480
return "thread"
472481
elif v < 122:
473482
return "application_command_permission"
483+
elif v < 144:
484+
return "auto_moderation_rule"
474485

475486

476487
class UserFlags(Enum):
@@ -764,6 +775,29 @@ class ScheduledEventLocationType(Enum):
764775
voice = 2
765776
external = 3
766777

778+
779+
class AutoModTriggerType(Enum):
780+
keyword = 1
781+
harmful_link = 2
782+
spam = 3
783+
keyword_preset = 4
784+
785+
786+
class AutoModEventType(Enum):
787+
message_send = 1
788+
789+
790+
class AutoModActionType(Enum):
791+
block_message = 1
792+
send_alert_message = 2
793+
timeout = 3
794+
795+
796+
class AutoModKeywordPresetType(Enum):
797+
profanity = 1
798+
sexual_content = 2
799+
slurs = 3
800+
767801

768802
T = TypeVar("T")
769803

discord/flags.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,28 @@ def scheduled_events(self):
970970
- :meth:`Guild.get_scheduled_event`
971971
"""
972972
return 1 << 16
973+
974+
@flag_value
975+
def auto_moderation_configuration(self):
976+
""":class:`bool`: Whether guild auto moderation configuration events are enabled.
977+
978+
This corresponds to the following events:
979+
980+
- :func:`on_auto_moderation_rule_create`
981+
- :func:`on_auto_moderation_rule_update`
982+
- :func:`on_auto_moderation_rule_delete`
983+
"""
984+
return 1 << 20
985+
986+
@flag_value
987+
def auto_moderation_execution(self):
988+
""":class:`bool`: Whether guild auto moderation execution events are enabled.
989+
990+
This corresponds to the following events:
991+
992+
- :func:`on_auto_moderation_action_execution`
993+
"""
994+
return 1 << 21
973995

974996

975997
@fill_with_flags()

discord/guild.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@
4444
)
4545

4646
from . import abc, utils
47+
from .automod import AutoModAction, AutoModRule, AutoModTriggerMetadata
4748
from .asset import Asset
4849
from .channel import *
4950
from .channel import _guild_channel_factory, _threaded_guild_channel_factory
5051
from .colour import Colour
5152
from .emoji import Emoji
5253
from .enums import (
5354
AuditLogAction,
55+
AutoModEventType,
56+
AutoModTriggerType,
5457
ChannelType,
5558
ContentFilter,
5659
NotificationLevel,
@@ -203,6 +206,7 @@ class Guild(Hashable):
203206
204207
- ``ANIMATED_BANNER``: Guild can upload an animated banner.
205208
- ``ANIMATED_ICON``: Guild can upload an animated icon.
209+
- ``AUTO_MODERATION``: Guild has enabled the auto moderation system.
206210
- ``BANNER``: Guild can upload and use a banner. (i.e. :attr:`.banner`)
207211
- ``CHANNEL_BANNER``: Guild can upload and use a channel banners.
208212
- ``COMMERCE``: Guild can sell things using store channels, which have now been removed.
@@ -545,7 +549,6 @@ def _from_data(self, guild: GuildPayload) -> None:
545549

546550
for obj in guild.get("voice_states", []):
547551
self._update_voice_state(obj, int(obj["channel_id"]))
548-
549552
# TODO: refactor/remove?
550553
def _sync(self, data: GuildPayload) -> None:
551554
try:
@@ -3520,3 +3523,110 @@ async def create_scheduled_event(
35203523
def scheduled_events(self) -> List[ScheduledEvent]:
35213524
"""List[:class:`.ScheduledEvent`]: A list of scheduled events in this guild."""
35223525
return list(self._scheduled_events.values())
3526+
3527+
async def fetch_auto_moderation_rules(self) -> List[AutoModRule]:
3528+
"""|coro|
3529+
3530+
Retrieves a list of auto moderation rules for this guild.
3531+
3532+
Raises
3533+
-------
3534+
HTTPException
3535+
Getting the auto moderation rules failed.
3536+
Forbidden
3537+
You do not have the Manage Guild permission.
3538+
3539+
Returns
3540+
--------
3541+
List[:class:`AutoModRule`]
3542+
The auto moderation rules for this guild.
3543+
"""
3544+
data = await self._state.http.get_auto_moderation_rules(self.id)
3545+
return [AutoModRule(state=self._state, data=rule) for rule in data]
3546+
3547+
async def fetch_auto_moderation_rule(self, id: int) -> AutoModRule:
3548+
"""|coro|
3549+
3550+
Retrieves a :class:`AutoModRule` from rule ID.
3551+
3552+
Raises
3553+
-------
3554+
HTTPException
3555+
Getting the auto moderation rule failed.
3556+
Forbidden
3557+
You do not have the Manage Guild permission.
3558+
3559+
Returns
3560+
--------
3561+
:class:`AutoModRule`
3562+
The requested auto moderation rule.
3563+
"""
3564+
data = await self._state.http.get_auto_moderation_rule(self.id, id)
3565+
return AutoModRule(state=self._state, data=data)
3566+
3567+
async def create_auto_moderation_rule(
3568+
self,
3569+
*,
3570+
name: str,
3571+
event_type: AutoModEventType,
3572+
trigger_type: AutoModTriggerType,
3573+
trigger_metadata: AutoModTriggerMetadata,
3574+
actions: List[AutoModAction],
3575+
enabled: bool = False,
3576+
exempt_roles: List[Snowflake] = None,
3577+
exempt_channels: List[Snowflake] = None,
3578+
reason: Optional[str] = None,
3579+
) -> AutoModRule:
3580+
"""
3581+
Creates an auto moderation rule.
3582+
3583+
Parameters
3584+
-----------
3585+
name: :class:`str`
3586+
The name of the auto moderation rule.
3587+
event_type: :class:`AutoModEventType`
3588+
The type of event that triggers the rule.
3589+
trigger_type: :class:`AutoModTriggerType`
3590+
The rule's trigger type.
3591+
trigger_metadata: :class:`AutoModTriggerMetadata`
3592+
The rule's trigger metadata.
3593+
actions: List[:class:`AutoModAction`]
3594+
The actions to take when the rule is triggered.
3595+
enabled: :class:`bool`
3596+
Whether the rule is enabled.
3597+
exempt_roles: List[:class:`Snowflake`]
3598+
A list of roles that are exempt from the rule.
3599+
exempt_channels: List[:class:`Snowflake`]
3600+
A list of channels that are exempt from the rule.
3601+
reason: Optional[:class:`str`]
3602+
The reason for creating the rule. Shows up in the audit log.
3603+
3604+
Raises
3605+
-------
3606+
HTTPException
3607+
Creating the auto moderation rule failed.
3608+
Forbidden
3609+
You do not have the Manage Guild permission.
3610+
3611+
Returns
3612+
--------
3613+
:class:`AutoModRule`
3614+
The new auto moderation rule.
3615+
"""
3616+
payload = {
3617+
"name": name,
3618+
"event_type": event_type.value,
3619+
"trigger_type": trigger_type.value,
3620+
"trigger_metadata": trigger_metadata.to_dict(),
3621+
"actions": [a.to_dict() for a in actions],
3622+
"enabled": enabled,
3623+
}
3624+
3625+
if exempt_roles:
3626+
payload["exempt_roles"] = [r.id for r in exempt_roles]
3627+
3628+
if exempt_channels:
3629+
payload["exempt_channels"] = [c.id for c in exempt_channels]
3630+
3631+
data = await self._state.http.create_auto_moderation_rule(self.id, payload)
3632+
return AutoModRule(state=self._state, data=data, reason=reason)

discord/http.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from .types import (
7272
appinfo,
7373
audit_log,
74+
automod,
7475
channel,
7576
components,
7677
embed,
@@ -2315,6 +2316,74 @@ def get_guild_command_permissions(
23152316
)
23162317
return self.request(r)
23172318

2319+
# Guild Automod Rules
2320+
2321+
def get_auto_moderation_rules(
2322+
self,
2323+
guild_id: Snowflake,
2324+
) -> Response[List[automod.AutoModRule]]:
2325+
r = Route(
2326+
"GET",
2327+
"/guilds/{guild_id}/auto-moderation/rules",
2328+
guild_id=guild_id,
2329+
)
2330+
return self.request(r)
2331+
2332+
def get_auto_moderation_rule(
2333+
self,
2334+
guild_id: Snowflake,
2335+
rule_id: Snowflake,
2336+
) -> Response[automod.AutoModRule]:
2337+
r = Route(
2338+
"GET",
2339+
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
2340+
guild_id=guild_id,
2341+
rule_id=rule_id,
2342+
)
2343+
return self.request(r)
2344+
2345+
def create_auto_moderation_rule(
2346+
self,
2347+
guild_id: Snowflake,
2348+
payload: automod.CreateAutoModRule,
2349+
reason: Optional[str] = None,
2350+
) -> Response[automod.AutoModRule]:
2351+
r = Route(
2352+
"POST",
2353+
"/guilds/{guild_id}/auto-moderation/rules",
2354+
guild_id=guild_id,
2355+
)
2356+
return self.request(r, json=payload, reason=reason)
2357+
2358+
def edit_auto_moderation_rule(
2359+
self,
2360+
guild_id: Snowflake,
2361+
rule_id: Snowflake,
2362+
payload: automod.EditAutoModRule,
2363+
reason: Optional[str] = None,
2364+
) -> Response[automod.AutoModRule]:
2365+
r = Route(
2366+
"PATCH",
2367+
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
2368+
guild_id=guild_id,
2369+
rule_id=rule_id,
2370+
)
2371+
return self.request(r, json=payload, reason=reason)
2372+
2373+
def delete_auto_moderation_rule(
2374+
self,
2375+
guild_id: Snowflake,
2376+
rule_id: Snowflake,
2377+
reason: Optional[str] = None,
2378+
) -> Response[None]:
2379+
r = Route(
2380+
"DELETE",
2381+
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
2382+
guild_id=guild_id,
2383+
rule_id=rule_id,
2384+
)
2385+
return self.request(r, reason=reason)
2386+
23182387
# Interaction responses
23192388

23202389
def _edit_webhook_helper(

0 commit comments

Comments
 (0)