This repository was archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauto_moderation.py
160 lines (144 loc) · 6.21 KB
/
auto_moderation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# cython: language_level=3
# Copyright (c) 2021-present Pycord Development
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE
from __future__ import annotations
from typing import TYPE_CHECKING
from .enums import (
AutoModActionType,
AutoModEventType,
AutoModKeywordPresetType,
AutoModTriggerType,
)
from .missing import MISSING, Maybe, MissingEnum
from .snowflake import Snowflake
from .types import (
AutoModerationAction as DiscordAutoModerationAction,
AutoModerationActionMetadata as DiscordAutoModerationActionMetadata,
AutoModerationRule as DiscordAutoModerationRule,
AutoModerationTriggerMetadata as DiscordAutoModerationTriggerMetadata,
)
if TYPE_CHECKING:
from .state import State
class AutoModTriggerMetadata:
def __init__(self, data: DiscordAutoModerationTriggerMetadata) -> None:
self.keyword_filter: list[str] | MissingEnum = data.get(
'keyword_filter', MISSING
)
self.regex_patterns: list[str] | MissingEnum = data.get(
'regex_patterns', MISSING
)
self.presets: list[AutoModKeywordPresetType] | MissingEnum = (
[AutoModKeywordPresetType(preset) for preset in data['presets']]
if data.get('presets') is not None
else MISSING
)
self.allow_list: list[str] | MissingEnum = data.get('allow_list', MISSING)
self.mention_total_limit: int | MissingEnum = data.get(
'mention_total_limit', MISSING
)
class AutoModActionMetadata:
def __init__(self, data: DiscordAutoModerationActionMetadata) -> None:
self.channel_id: Snowflake | MissingEnum = (
Snowflake(data['channel_id'])
if data.get('channel_id') is not None
else MISSING
)
self.duration_seconds: int | MissingEnum = data.get('duration_seconds', MISSING)
class AutoModAction:
def __init__(self, data: DiscordAutoModerationAction) -> None:
self.type: AutoModActionType = AutoModActionType(data['type'])
self.metadata: AutoModActionMetadata = AutoModActionMetadata(data['metadata'])
class AutoModRule:
def __init__(self, data: DiscordAutoModerationRule, state: State) -> None:
self._state: State = state
self.id: Snowflake = Snowflake(data['id'])
self.guild_id: Snowflake = Snowflake(data['guild_id'])
self.name: str = data['name']
self.creator_id: Snowflake = Snowflake(data['creator_id'])
self.event_type: AutoModEventType = AutoModEventType(data['event_type'])
self.trigger_type: AutoModTriggerType = AutoModTriggerType(data['trigger_type'])
self.trigger_metadata: AutoModTriggerMetadata = AutoModTriggerMetadata(
data['trigger_metadata']
)
self.actions: list[AutoModAction] = [
AutoModAction(action) for action in data['actions']
]
self.enabled: bool = data['enabled']
self.exempt_roles: list[Snowflake] = [
Snowflake(role) for role in data.get('exempt_roles', [])
]
self.exempt_channels: list[Snowflake] = [
Snowflake(member) for member in data.get('exempt_channels', [])
]
async def edit(
self,
*,
name: str | MissingEnum = MISSING,
event_type: AutoModEventType | MissingEnum = MISSING,
trigger_metadata: AutoModTriggerMetadata | MissingEnum = MISSING,
actions: list[AutoModAction] | MissingEnum = MISSING,
enabled: bool | MissingEnum = MISSING,
exempt_roles: list[Snowflake] | MissingEnum = MISSING,
exempt_channels: list[Snowflake] | MissingEnum = MISSING,
reason: str | None = None,
) -> AutoModRule:
"""Edits the auto moderation rule.
Parameters
----------
name: :class:`str`
The rule name.
event_type: :class:`AutoModEventType`
The event type.
trigger_metadata: :class:`AutoModTriggerMetadata`
The trigger metadata.
actions: list[:class:`AutoModAction`]
The actions to take when the rule is triggered.
enabled: :class:`bool`
Whether the rule is enabled.
exempt_roles: list[:class:`Snowflake`]
The roles exempt from the rule.
exempt_channels: list[:class:`Snowflake`]
The channels exempt from the rule.
reason: :class:`str` | None
The reason for editing this rule. Appears in the guild's audit log.
"""
data = await self._state.http.modify_auto_moderation_rule(
self.guild_id,
self.id,
name=name,
event_type=event_type,
trigger_metadata=trigger_metadata,
actions=actions,
enabled=enabled,
exempt_roles=exempt_roles,
exempt_channels=exempt_channels,
reason=reason,
)
return AutoModRule(data, self._state)
async def delete(self, *, reason: str | None = None) -> None:
"""Deletes the auto moderation rule.
Parameters
----------
reason: :class:`str` | None
The reason for deleting this rule. Appears in the guild's audit log.
"""
await self._state.http.delete_auto_moderation_rule(
self.guild_id, self.id, reason=reason
)