Skip to content

Commit

Permalink
Add AutoModTrigger.regex_patterns support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Nov 6, 2022
1 parent e92a626 commit 84767ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
48 changes: 31 additions & 17 deletions discord/automod.py
Expand Up @@ -118,17 +118,17 @@ class AutoModTrigger:
The following table illustrates relevant attributes for each :class:`AutoModRuleTriggerType`:
+-----------------------------------------------+--------------------------------------+
| Type | Attributes |
+===============================================+======================================+
| :attr:`AutoModRuleTriggerType.keyword` | :attr:`keyword_filter` |
+-----------------------------------------------+--------------------------------------+
| :attr:`AutoModRuleTriggerType.spam` | |
+-----------------------------------------------+--------------------------------------+
| :attr:`AutoModRuleTriggerType.keyword_preset` | :attr:`presets`\, :attr:`allow_list` |
+-----------------------------------------------+--------------------------------------+
| :attr:`AutoModRuleTriggerType.mention_spam` | :attr:`mention_limit` |
+-----------------------------------------------+--------------------------------------+
+-----------------------------------------------+------------------------------------------------+
| Type | Attributes |
+===============================================+================================================+
| :attr:`AutoModRuleTriggerType.keyword` | :attr:`keyword_filter`, :attr:`regex_patterns` |
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.spam` | |
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.keyword_preset` | :attr:`presets`\, :attr:`allow_list` |
+-----------------------------------------------+------------------------------------------------+
| :attr:`AutoModRuleTriggerType.mention_spam` | :attr:`mention_limit` |
+-----------------------------------------------+------------------------------------------------+
.. versionadded:: 2.0
Expand All @@ -137,7 +137,18 @@ class AutoModTrigger:
type: :class:`AutoModRuleTriggerType`
The type of trigger.
keyword_filter: List[:class:`str`]
The list of strings that will trigger the keyword filter.
The list of strings that will trigger the keyword filter. Maximum of 1000.
Keywords can only be up to 30 characters in length.
This could be combined with :attr:`regex_patterns`.
regex_patterns: List[:class:`str`]
The regex pattern that will trigger the filter. The syntax is based off of
`Rust's regex syntax <https://docs.rs/regex/latest/regex/#syntax>`_.
Maximum of 10. Regex strings can only be up to 75 characters in length.
This could be combined with :attr:`keyword_filter`.
.. versionadded:: 2.1
presets: :class:`AutoModPresets`
The presets used with the preset keyword filter.
allow_list: List[:class:`str`]
Expand All @@ -153,6 +164,7 @@ class AutoModTrigger:
'presets',
'allow_list',
'mention_limit',
'regex_patterns',
)

def __init__(
Expand All @@ -163,13 +175,14 @@ def __init__(
presets: Optional[AutoModPresets] = None,
allow_list: Optional[List[str]] = None,
mention_limit: Optional[int] = None,
regex_patterns: Optional[List[str]] = None,
) -> None:
if type is None and sum(arg is not None for arg in (keyword_filter, presets, mention_limit)) > 1:
raise ValueError('Please pass only one of keyword_filter, presets, or mention_limit.')
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.')

if type is not None:
self.type = type
elif keyword_filter is not None:
elif keyword_filter is not None or regex_patterns is not None:
self.type = AutoModRuleTriggerType.keyword
elif presets is not None:
self.type = AutoModRuleTriggerType.keyword_preset
Expand All @@ -184,14 +197,15 @@ 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.regex_patterns: List[str] = regex_patterns if regex_patterns is not None else []

@classmethod
def from_data(cls, type: int, data: Optional[AutoModerationTriggerMetadataPayload]) -> Self:
type_ = try_enum(AutoModRuleTriggerType, type)
if data is None:
return cls(type=type_)
elif type_ is AutoModRuleTriggerType.keyword:
return cls(type=type_, keyword_filter=data.get('keyword_filter'))
return cls(type=type_, keyword_filter=data.get('keyword_filter'), regex_patterns=data.get('regex_patterns'))
elif type_ is AutoModRuleTriggerType.keyword_preset:
return cls(
type=type_, presets=AutoModPresets._from_value(data.get('presets', [])), allow_list=data.get('allow_list')
Expand All @@ -203,7 +217,7 @@ def from_data(cls, type: int, data: Optional[AutoModerationTriggerMetadataPayloa

def to_metadata_dict(self) -> Optional[Dict[str, Any]]:
if self.type is AutoModRuleTriggerType.keyword:
return {'keyword_filter': self.keyword_filter}
return {'keyword_filter': self.keyword_filter, 'regex_patterns': self.regex_patterns}
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:
Expand Down
1 change: 1 addition & 0 deletions discord/types/automod.py
Expand Up @@ -65,6 +65,7 @@ class _AutoModerationActionTimeout(TypedDict):

class _AutoModerationTriggerMetadataKeyword(TypedDict):
keyword_filter: List[str]
regex_patterns: NotRequired[List[str]]


class _AutoModerationTriggerMetadataKeywordPreset(TypedDict):
Expand Down

0 comments on commit 84767ef

Please sign in to comment.