Skip to content

Commit e45a645

Browse files
committed
feat(automod): Implement AutoModAction.custom_message
1 parent 8915c61 commit e45a645

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

discord/automod.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,38 @@ class AutoModAction:
8383
timeout_duration: Optional[Union[:class:`int`, :class:`datetime.timedelta`]]
8484
Duration in seconds (:class:`int`) or a timerange (:class:`~datetime.timedelta`) for wich the user should be timeouted.
8585
86-
**The maximum value is ``2419200`` seconds (4 weeks)**
86+
**The maximum value is** ``2419200`` **seconds (4 weeks)**
8787
8888
.. note::
8989
This field is only required if :attr:`type` is :attr:`AutoModActionType.timeout_user`
90-
90+
91+
custom_message: Optional[:class:`str`]
92+
Additional explanation that will be shown to members whenever their message is blocked. **Max 150 characters**
93+
94+
.. note::
95+
This field is only allowed if :attr:`type` is :attr:`AutoModActionType.block_message`
9196
"""
9297
def __init__(self, type: AutoModActionType, **metadata):
9398
self.type: AutoModActionType = try_enum(AutoModActionType, type)
9499
self.metadata = metadata # maybe we need this later... idk
100+
95101
action_type = self.type # speedup attribute access
96-
if action_type.send_alert_message:
102+
103+
if action_type.block_message:
104+
try:
105+
self.custom_message: Optional[str] = metadata['custom_message']
106+
except KeyError:
107+
pass
108+
else:
109+
if len(self.custom_message) > 150:
110+
raise ValueError('The maximum length of the custom message is 150 characters.')
111+
112+
elif action_type.send_alert_message:
97113
try:
98114
self.channel_id: Optional[int] = metadata['channel_id']
99115
except KeyError:
100116
raise TypeError('If the type is send_alert_message you must specify a channel_id')
117+
101118
elif action_type.timeout_user:
102119
try:
103120
timeout_duration: Optional[Union[int, datetime.timedelta]] = metadata['timeout_duration']
@@ -115,6 +132,10 @@ def to_dict(self) -> Dict[str, Any]:
115132
'type': int(self.type)
116133
}
117134
metadata = {}
135+
if self.type.block_message:
136+
custom_message = getattr(self, 'custom_message', None)
137+
if custom_message:
138+
metadata['custom_message'] = self.custom_message
118139
if self.type.send_alert_message:
119140
metadata['channel_id'] = self.channel_id
120141
elif self.type.timeout_user:
@@ -126,7 +147,9 @@ def to_dict(self) -> Dict[str, Any]:
126147
def from_dict(cls, data: Dict[str, Any]) -> Self:
127148
action_type = try_enum(AutoModActionType, data['type'])
128149
metadata = data['metadata']
129-
if action_type.timeout_user:
150+
if action_type.block_message:
151+
metadata['custom_message'] = metadata.pop('custom_message', None)
152+
elif action_type.timeout_user:
130153
metadata['timeout_duration'] = metadata.pop('duration_seconds')
131154
elif action_type.send_alert_message:
132155
metadata['channel_id'] = int(metadata['channel_id'])

0 commit comments

Comments
 (0)