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

feat: Update AutoMod implementation #1809

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5925da8
Add missing rule_trigger_type attribute to AutoModActionExecutionEvent
TheEnigmaBlade Aug 21, 2022
a6e47c6
Update documentation for rule_trigger_type in AutoModActionExecutionE…
TheEnigmaBlade Aug 21, 2022
9049a64
Add missing trigger metadata values
TheEnigmaBlade Nov 29, 2022
69a6e47
docs: Add missing AutoMod information
TheEnigmaBlade Nov 29, 2022
651f277
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 29, 2022
4a64e95
docs: Fix typos in AutoModTriggerMetadata and add warning on regex fl…
TheEnigmaBlade Nov 29, 2022
3a15e9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 29, 2022
ac7a37e
Apply suggestions from code review
Lulalaby Nov 29, 2022
ea5bd50
Update discord/types/automod.py
Lulalaby Nov 29, 2022
41b5682
Merge branch 'master' into fix/missing-automod-values
BobDotCom Nov 30, 2022
270a43d
docs: Add versionadded to new AutoMod properties, and correct one fro…
TheEnigmaBlade Nov 30, 2022
524676a
docs(changelog): Update changelog for this pull request
TheEnigmaBlade Nov 30, 2022
c9a057d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 30, 2022
feca849
docs: Mark `harmful_link` trigger type as deprecated, and update `Aut…
TheEnigmaBlade Nov 30, 2022
584b387
Merge branch 'master' into fix/missing-automod-values
BobDotCom Nov 30, 2022
050a40f
Merge branch 'master' into fix/missing-automod-values
plun1331 Dec 5, 2022
e252717
docs: Add additional AutoMod documentation for `AutoModTriggerMetadat…
TheEnigmaBlade Dec 7, 2022
4783f12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 7, 2022
0667595
docs: Make `AutoModActionMetadata` visible for docs compilation `Auto…
TheEnigmaBlade Dec 7, 2022
38f4ffd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 7, 2022
842e649
Merge branch 'master' into fix/missing-automod-values
Lulalaby Dec 7, 2022
2f82cf0
Merge branch 'master' into fix/missing-automod-values
Lulalaby Dec 16, 2022
906cd9f
Merge branch 'master' into fix/missing-automod-values
Lulalaby Dec 21, 2022
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,7 +10,11 @@ possible (see our [Version Guarantees] for more info).

These changes are available on the `master` branch, but have not yet been released.

_No changes yet_
### Added

- Added new AutoMod trigger metadata properties `regex_patterns`, `allow_list`, and
`mention_total_limit`; and added the `mention_spam` trigger type.
([#1809](https://github.com/Pycord-Development/pycord/pull/1809))

## [2.3.2] - 2022-12-03

Expand Down
57 changes: 54 additions & 3 deletions discord/automod.py
Expand Up @@ -39,7 +39,10 @@
from .mixins import Hashable
from .object import Object

__all__ = ("AutoModRule",)
__all__ = (
"AutoModRule",
"AutoModAction",
)

if TYPE_CHECKING:
from .abc import Snowflake
Expand Down Expand Up @@ -176,36 +179,72 @@ class AutoModTriggerMetadata:
Attributes
----------
keyword_filter: List[:class:`str`]
A list of substrings to filter. Only for triggers of type :attr:`AutoModTriggerType.keyword`.
A list of substrings to filter.
Only for triggers of type :attr:`AutoModTriggerType.keyword`.
regex_patterns: List[:class:`str`]
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved
A list of regex patterns to filter using Rust-flavored regex, which is not
fully compatible with regex syntax supported by the builtin `re` module.
Only for triggers of type :attr:`AutoModTriggerType.keyword`.

.. versionadded:: 2.4
presets: List[:class:`AutoModKeywordPresetType`]
A list of keyword presets to filter. Only for triggers of type :attr:`AutoModTriggerType.keyword_preset`.
A list of keyword presets to filter.
Only for triggers of type :attr:`AutoModTriggerType.keyword_preset`.
allow_list: List[:class:`str`]
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved
A list of substrings to allow, overriding keyword and regex matches.
Only for triggers of type :attr:`AutoModTriggerType.keyword` and :attr:`AutoModTriggerType.keyword_preset`.

.. versionadded:: 2.4
mention_total_limit: :class:`int`
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved
The total number of unique role and user mentions allowed.
Only for triggers of type :attr:`AutoModTriggerType.mention_spam`.

.. versionadded:: 2.4
"""

# maybe add a table of action types and attributes?
# wording for presets could change

__slots__ = (
"keyword_filter",
"regex_patterns",
"presets",
"allow_list",
"mention_total_limit",
)

def __init__(
self,
keyword_filter: list[str] = MISSING,
regex_patterns: list[str] = MISSING,
presets: list[AutoModKeywordPresetType] = MISSING,
allow_list: list[str] = MISSING,
mention_total_limit: int = MISSING,
):
self.keyword_filter = keyword_filter
self.regex_patterns = regex_patterns
self.presets = presets
self.allow_list = allow_list
self.mention_total_limit = mention_total_limit

def to_dict(self) -> dict:
data = {}

if self.keyword_filter is not MISSING:
data["keyword_filter"] = self.keyword_filter

if self.regex_patterns is not MISSING:
data["regex_patterns"] = self.regex_patterns

if self.presets is not MISSING:
data["presets"] = [wordset.value for wordset in self.presets]

if self.allow_list is not MISSING:
data["allow_list"] = self.allow_list

if self.mention_total_limit is not MISSING:
data["mention_total_limit"] = self.mention_total_limit

return data

@classmethod
Expand All @@ -215,17 +254,29 @@ def from_dict(cls, data: AutoModTriggerMetadataPayload):
if (keyword_filter := data.get("keyword_filter")) is not None:
kwargs["keyword_filter"] = keyword_filter

if (regex_patterns := data.get("regex_patterns")) is not None:
kwargs["regex_patterns"] = regex_patterns

if (presets := data.get("presets")) is not None:
kwargs["presets"] = [
try_enum(AutoModKeywordPresetType, wordset) for wordset in presets
]

if (allow_list := data.get("allow_list")) is not None:
kwargs["allow_list"] = allow_list

if (mention_total_limit := data.get("mention_total_limit")) is not None:
kwargs["mention_total_limit"] = mention_total_limit

return cls(**kwargs)

def __repr__(self) -> str:
repr_attrs = (
"keyword_filter",
"regex_patterns",
"presets",
"allow_list",
"mention_total_limit",
)
inner = []

Expand Down
1 change: 1 addition & 0 deletions discord/enums.py
Expand Up @@ -880,6 +880,7 @@ class AutoModTriggerType(Enum):
harmful_link = 2
spam = 3
keyword_preset = 4
mention_spam = 5


class AutoModEventType(Enum):
Expand Down
10 changes: 9 additions & 1 deletion discord/raw_models.py
Expand Up @@ -28,7 +28,7 @@
import datetime
from typing import TYPE_CHECKING

from .automod import AutoModAction
from .automod import AutoModAction, AutoModTriggerType
from .enums import ChannelType, try_enum

if TYPE_CHECKING:
Expand Down Expand Up @@ -409,6 +409,10 @@ class AutoModActionExecutionEvent:
The action that was executed.
rule_id: :class:`int`
The ID of the rule that the action belongs to.
rule_trigger_type: :class:`AutoModTriggerType`
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved
The category of trigger the rule belongs to.

.. versionadded:: 2.4
guild_id: :class:`int`
The ID of the guild that the action was executed in.
guild: Optional[:class:`Guild`]
Expand Down Expand Up @@ -443,6 +447,7 @@ class AutoModActionExecutionEvent:
__slots__ = (
"action",
"rule_id",
"rule_trigger_type",
"guild_id",
"guild",
"user_id",
Expand All @@ -461,6 +466,9 @@ class AutoModActionExecutionEvent:
def __init__(self, state: ConnectionState, data: AutoModActionExecution) -> None:
self.action: AutoModAction = AutoModAction.from_dict(data["action"])
self.rule_id: int = int(data["rule_id"])
self.rule_trigger_type: AutoModTriggerType = try_enum(
AutoModTriggerType, int(data["rule_trigger_type"])
)
self.guild_id: int = int(data["guild_id"])
self.guild: Guild | None = state._get_guild(self.guild_id)
self.user_id: int = int(data["user_id"])
Expand Down
5 changes: 4 additions & 1 deletion discord/types/automod.py
Expand Up @@ -27,7 +27,7 @@
from .._typed_dict import NotRequired, TypedDict
from .snowflake import Snowflake

AutoModTriggerType = Literal[1, 2, 3, 4]
AutoModTriggerType = Literal[1, 2, 3, 4, 5]

AutoModEventType = Literal[1]

Expand All @@ -38,7 +38,10 @@

class AutoModTriggerMetadata(TypedDict, total=False):
keyword_filter: list[str]
regex_patterns: list[str]
presets: list[AutoModKeywordPresetType]
allow_list: list[str]
mention_total_limit: int


class AutoModActionMetadata(TypedDict, total=False):
Expand Down
94 changes: 94 additions & 0 deletions docs/api/enums.rst
Expand Up @@ -1867,3 +1867,97 @@ of :class:`enum.Enum`.
.. attribute:: guild_only

Represents a scheduled event that is only available to members inside the guild.

.. class:: AutoModTriggerType
TheEnigmaBlade marked this conversation as resolved.
Show resolved Hide resolved

Represents an AutoMod trigger type.

.. versionadded:: 2.0

.. attribute:: keyword

Represents a keyword rule trigger, which are customizable by a guild.

Possible attributes for :class:`AutoModTriggerMetadata`:

- :attr:`~AutoModTriggerMetadata.keyword_filter`
- :attr:`~AutoModTriggerMetadata.regex_patterns`
- :attr:`~AutoModTriggerMetadata.allow_list`

.. attribute:: keyword_preset

Represents a preset keyword rule trigger.

Possible attributes for :class:`AutoModTriggerMetadata`:

- :attr:`~AutoModTriggerMetadata.presets`
- :attr:`~AutoModTriggerMetadata.allow_list`

.. attribute:: spam

Represents the spam rule trigger.

There are no possible attributes for :class:`AutoModTriggerMetadata`.

.. attribute:: mention_spam

Represents a mention spam keyword rule trigger.

Possible attributes for :class:`AutoModTriggerMetadata`:

- :attr:`~AutoModTriggerMetadata.mention_total_limit`

.. versionadded:: 2.4

.. attribute:: harmful_link

Represents a harmful link rule trigger.

.. deprecated:: 2.4
Lulalaby marked this conversation as resolved.
Show resolved Hide resolved
Removed by Discord and merged into `spam`.

.. class:: AutoModEventType

Represents an AutoMod event type.

.. versionadded:: 2.0

.. attribute:: message_send

Represents a message send AutoMod event.

.. class:: AutoModActionType

Represents the type of action AutoMod is performing.

.. versionadded:: 2.0

.. attribute:: block_message

Represents a block message action.

.. attribute:: send_alert_message

Represents a send alert message action.

.. attribute:: timeout

Represents a timeout action.

.. class:: AutoModKeywordPresetType

Represents an AutoMod keyword preset type.

.. versionadded:: 2.0

.. attribute:: profanity

Represents the profanity keyword preset rule.

.. attribute:: sexual_content

Represents the sexual content keyword preset rule.

.. attribute:: slurs

Represents the slurs keyword preset rule.
8 changes: 8 additions & 0 deletions docs/api/models.rst
Expand Up @@ -146,11 +146,19 @@ Guild
.. autoclass:: Template()
:members:

AutoMod
~~~~~~~

.. attributetable:: AutoModRule

.. autoclass:: AutoModRule()
:members:

.. attributetable:: AutoModAction

.. autoclass:: AutoModAction()
:members:

Lulalaby marked this conversation as resolved.
Show resolved Hide resolved
Invites
~~~~~~~

Expand Down