Skip to content

Commit

Permalink
feat: Add custom_message to AutoModActionMetadata and fix TypeError o…
Browse files Browse the repository at this point in the history
…n AutoModRule (#2029)

* Automod fixes and additions

* style(pre-commit): auto fixes from pre-commit.com hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
NeloBlivion and pre-commit-ci[bot] authored Apr 25, 2023
1 parent 0407f28 commit a8ce125
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
19 changes: 18 additions & 1 deletion discord/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,29 @@ class AutoModActionMetadata:
timeout_duration: :class:`datetime.timedelta`
How long the member that triggered the action should be timed out for.
Only for actions of type :attr:`AutoModActionType.timeout`.
custom_message: :class:`str`
An additional message shown to members when their message is blocked.
Maximum 150 characters.
Only for actions of type :attr:`AutoModActionType.block_message`.
"""

# maybe add a table of action types and attributes?

__slots__ = (
"channel_id",
"timeout_duration",
"custom_message",
)

def __init__(
self, channel_id: int = MISSING, timeout_duration: timedelta = MISSING
self,
channel_id: int = MISSING,
timeout_duration: timedelta = MISSING,
custom_message: str = MISSING,
):
self.channel_id: int = channel_id
self.timeout_duration: timedelta = timeout_duration
self.custom_message: str = custom_message

def to_dict(self) -> dict:
data = {}
Expand All @@ -100,6 +109,9 @@ def to_dict(self) -> dict:
if self.timeout_duration is not MISSING:
data["duration_seconds"] = self.timeout_duration.total_seconds()

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

return data

@classmethod
Expand All @@ -113,12 +125,16 @@ def from_dict(cls, data: AutoModActionMetadataPayload):
# might need an explicit int cast
kwargs["timeout_duration"] = timedelta(seconds=duration_seconds)

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

return cls(**kwargs)

def __repr__(self) -> str:
repr_attrs = (
"channel_id",
"timeout_duration",
"custom_message",
)
inner = []

Expand Down Expand Up @@ -352,6 +368,7 @@ class AutoModRule(Hashable):
"""

__slots__ = (
"__dict__",
"_state",
"id",
"guild_id",
Expand Down
1 change: 1 addition & 0 deletions discord/types/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AutoModTriggerMetadata(TypedDict, total=False):
class AutoModActionMetadata(TypedDict, total=False):
channel_id: Snowflake
duration_seconds: int
custom_message: str


class AutoModAction(TypedDict):
Expand Down

0 comments on commit a8ce125

Please sign in to comment.