Skip to content

Commit

Permalink
Support for AutoMod Members
Browse files Browse the repository at this point in the history
Co-authored-by: Josh <[email protected]>
Co-authored-by: Kaylynn Morgan <[email protected]>
Co-authored-by: Danny <[email protected]>
  • Loading branch information
4 people authored Oct 19, 2023
1 parent 163a86c commit 5d35328
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
94 changes: 78 additions & 16 deletions discord/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from __future__ import annotations
import datetime

from typing import TYPE_CHECKING, Any, Dict, Optional, List, Set, Union, Sequence, overload
from typing import TYPE_CHECKING, Any, Dict, Optional, List, Set, Union, Sequence, overload, Literal

from .enums import AutoModRuleTriggerType, AutoModRuleActionType, AutoModRuleEventType, try_enum
from .flags import AutoModPresets
Expand Down Expand Up @@ -85,36 +85,76 @@ class AutoModRuleAction:
__slots__ = ('type', 'channel_id', 'duration', 'custom_message')

@overload
def __init__(self, *, channel_id: Optional[int] = ...) -> None:
def __init__(self, *, channel_id: int = ...) -> None:
...

@overload
def __init__(self, *, duration: Optional[datetime.timedelta] = ...) -> None:
def __init__(self, *, type: Literal[AutoModRuleActionType.send_alert_message], channel_id: int = ...) -> None:
...

@overload
def __init__(self, *, custom_message: Optional[str] = ...) -> None:
def __init__(self, *, duration: datetime.timedelta = ...) -> None:
...

@overload
def __init__(self, *, type: Literal[AutoModRuleActionType.timeout], duration: datetime.timedelta = ...) -> None:
...

@overload
def __init__(self, *, custom_message: str = ...) -> None:
...

@overload
def __init__(self, *, type: Literal[AutoModRuleActionType.block_message]) -> None:
...

@overload
def __init__(self, *, type: Literal[AutoModRuleActionType.block_message], custom_message: Optional[str] = ...) -> None:
...

@overload
def __init__(
self,
*,
type: Optional[AutoModRuleActionType] = ...,
channel_id: Optional[int] = ...,
duration: Optional[datetime.timedelta] = ...,
custom_message: Optional[str] = ...,
) -> None:
...

def __init__(
self,
*,
type: Optional[AutoModRuleActionType] = None,
channel_id: Optional[int] = None,
duration: Optional[datetime.timedelta] = None,
custom_message: Optional[str] = None,
) -> None:
self.channel_id: Optional[int] = channel_id
self.duration: Optional[datetime.timedelta] = duration
self.custom_message: Optional[str] = custom_message

if sum(v is None for v in (channel_id, duration, custom_message)) < 2:
raise ValueError('Only one of channel_id, duration, or custom_message can be passed.')

self.type: AutoModRuleActionType = AutoModRuleActionType.block_message
if channel_id:
self.type: AutoModRuleActionType
if type is not None:
self.type = type
elif channel_id is not None:
self.type = AutoModRuleActionType.send_alert_message
elif duration:
elif duration is not None:
self.type = AutoModRuleActionType.timeout
else:
self.type = AutoModRuleActionType.block_message

if self.type is AutoModRuleActionType.send_alert_message:
if channel_id is None:
raise ValueError('channel_id cannot be None if type is send_alert_message')
self.channel_id: Optional[int] = channel_id

if self.type is AutoModRuleActionType.timeout:
if duration is None:
raise ValueError('duration cannot be None set if type is timeout')
self.duration: Optional[datetime.timedelta] = duration

self.custom_message: Optional[str] = custom_message

def __repr__(self) -> str:
return f'<AutoModRuleAction type={self.type.value} channel={self.channel_id} duration={self.duration}>'
Expand All @@ -127,7 +167,11 @@ def from_data(cls, data: AutoModerationActionPayload) -> Self:
elif data['type'] == AutoModRuleActionType.send_alert_message.value:
channel_id = int(data['metadata']['channel_id'])
return cls(channel_id=channel_id)
return cls(custom_message=data.get('metadata', {}).get('custom_message'))
elif data['type'] == AutoModRuleActionType.block_message.value:
custom_message = data.get('metadata', {}).get('custom_message')
return cls(type=AutoModRuleActionType.block_message, custom_message=custom_message)

return cls(type=AutoModRuleActionType.block_member_interactions)

def to_dict(self) -> Dict[str, Any]:
ret = {'type': self.type.value, 'metadata': {}}
Expand Down Expand Up @@ -155,7 +199,11 @@ class AutoModTrigger:
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.keyword_preset` | :attr:`presets`\, :attr:`allow_list` |
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.mention_spam` | :attr:`mention_limit` |
| :attr:`AutoModRuleTriggerType.mention_spam` | :attr:`mention_limit`, |
| | :attr:`mention_raid_protection` |
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.member_profile` | :attr:`keyword_filter`, :attr:`regex_patterns`,|
| | :attr:`allow_list` |
+-----------------------------------------------+------------------------------------------------+
.. versionadded:: 2.0
Expand Down Expand Up @@ -185,6 +233,10 @@ class AutoModTrigger:
mention_limit: :class:`int`
The total number of user and role mentions a message can contain.
Has a maximum of 50.
mention_raid_protection: :class:`bool`
Whether mention raid protection is enabled or not.
.. versionadded:: 2.4
"""

__slots__ = (
Expand All @@ -194,6 +246,7 @@ class AutoModTrigger:
'allow_list',
'mention_limit',
'regex_patterns',
'mention_raid_protection',
)

def __init__(
Expand All @@ -205,6 +258,7 @@ def __init__(
allow_list: Optional[List[str]] = None,
mention_limit: Optional[int] = None,
regex_patterns: Optional[List[str]] = None,
mention_raid_protection: Optional[bool] = None,
) -> None:
if type is None and sum(arg is not None for arg in (keyword_filter or regex_patterns, presets, mention_limit)) > 1:
raise ValueError('Please pass only one of keyword_filter, regex_patterns, presets, or mention_limit.')
Expand All @@ -215,7 +269,7 @@ def __init__(
self.type = AutoModRuleTriggerType.keyword
elif presets is not None:
self.type = AutoModRuleTriggerType.keyword_preset
elif mention_limit is not None:
elif mention_limit is not None or mention_raid_protection is not None:
self.type = AutoModRuleTriggerType.mention_spam
else:
raise ValueError(
Expand All @@ -226,6 +280,7 @@ def __init__(
self.presets: AutoModPresets = presets if presets is not None else AutoModPresets()
self.allow_list: List[str] = allow_list if allow_list is not None else []
self.mention_limit: int = mention_limit if mention_limit is not None else 0
self.mention_raid_protection: bool = mention_raid_protection if mention_raid_protection is not None else False
self.regex_patterns: List[str] = regex_patterns if regex_patterns is not None else []

def __repr__(self) -> str:
Expand Down Expand Up @@ -253,7 +308,11 @@ def from_data(cls, type: int, data: Optional[AutoModerationTriggerMetadataPayloa
type=type_, presets=AutoModPresets._from_value(data.get('presets', [])), allow_list=data.get('allow_list')
)
elif type_ is AutoModRuleTriggerType.mention_spam:
return cls(type=type_, mention_limit=data.get('mention_total_limit'))
return cls(
type=type_,
mention_limit=data.get('mention_total_limit'),
mention_raid_protection=data.get('mention_raid_protection_enabled'),
)
else:
return cls(type=type_)

Expand All @@ -267,7 +326,10 @@ def to_metadata_dict(self) -> Optional[Dict[str, Any]]:
elif self.type is AutoModRuleTriggerType.keyword_preset:
return {'presets': self.presets.to_array(), 'allow_list': self.allow_list}
elif self.type is AutoModRuleTriggerType.mention_spam:
return {'mention_total_limit': self.mention_limit}
return {
'mention_total_limit': self.mention_limit,
'mention_raid_protection_enabled': self.mention_raid_protection,
}


class AutoModRule:
Expand Down
3 changes: 3 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,16 +750,19 @@ class AutoModRuleTriggerType(Enum):
spam = 3
keyword_preset = 4
mention_spam = 5
member_profile = 6


class AutoModRuleEventType(Enum):
message_send = 1
member_update = 2


class AutoModRuleActionType(Enum):
block_message = 1
send_alert_message = 2
timeout = 3
block_member_interactions = 4


class ForumLayoutType(Enum):
Expand Down
1 change: 1 addition & 0 deletions discord/types/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class _AutoModerationTriggerMetadataKeywordPreset(TypedDict):

class _AutoModerationTriggerMetadataMentionLimit(TypedDict):
mention_total_limit: int
mention_raid_protection_enabled: bool


AutoModerationTriggerMetadata = Union[
Expand Down
18 changes: 18 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3330,6 +3330,12 @@ of :class:`enum.Enum`.
The rule will trigger when combined number of role and user mentions
is greater than the set limit.

.. attribute:: member_profile

The rule will trigger when a user's profile contains a keyword.

.. versionadded:: 2.4

.. class:: AutoModRuleEventType

Represents the event type of an automod rule.
Expand All @@ -3340,6 +3346,12 @@ of :class:`enum.Enum`.

The rule will trigger when a message is sent.

.. attribute:: member_update

The rule will trigger when a member's profile is updated.

.. versionadded:: 2.4

.. class:: AutoModRuleActionType

Represents the action type of an automod rule.
Expand All @@ -3358,6 +3370,12 @@ of :class:`enum.Enum`.

The rule will timeout a user.

.. attribute:: block_member_interactions

Similar to :attr:`timeout`, except the user will be timed out indefinitely.
This will request the user to edit it's profile.

.. versionadded:: 2.4

.. class:: ForumLayoutType

Expand Down

0 comments on commit 5d35328

Please sign in to comment.