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

Implement Automod Features #1316

Merged
merged 68 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
d00871a
Add auto moderation
plun1331 May 3, 2022
0bb1e1b
fix MessageType type
plun1331 May 3, 2022
de958ea
Add embed type
plun1331 May 3, 2022
8b6b4f5
Add AUTO_MODERATION guild feature
plun1331 May 3, 2022
7fc0fda
Document AUTO_MODERATION feature
plun1331 May 3, 2022
14af3cb
Update enums.py
plun1331 May 6, 2022
1b1f42e
Merge branch 'master' into automod
plun1331 May 11, 2022
6992a26
Add more automod enums
plun1331 May 13, 2022
d31ed4b
Add AutoMod types
plun1331 May 13, 2022
7e7b372
Merge branch 'Pycord-Development:master' into automod
plun1331 May 13, 2022
ab9e156
Merge branch 'master' into automod
plun1331 May 16, 2022
2351612
Merge branch 'master' into automod
plun1331 May 17, 2022
fbd80ec
Merge branch 'Pycord-Development:master' into automod
plun1331 May 19, 2022
05a00cb
Update automod typing
plun1331 May 19, 2022
2d08cc1
Merge branch 'Pycord-Development:master' into automod
plun1331 May 20, 2022
4bfc67e
Update audit_log.py
plun1331 May 20, 2022
22fb84a
Update enums.py
plun1331 May 20, 2022
da1dbf2
Update http.py
plun1331 May 20, 2022
cf7e825
Update http.py
plun1331 May 20, 2022
c2c4e07
Update flags.py
plun1331 May 20, 2022
0335be3
Update state.py
plun1331 May 20, 2022
d54f844
Update http.py
plun1331 May 20, 2022
4a82eac
Update http.py
plun1331 May 20, 2022
c346210
Update automod.py
plun1331 May 20, 2022
8bda417
Create automod.py
plun1331 May 20, 2022
996a834
Update automod.py
plun1331 May 20, 2022
594b3d8
Merge branch 'Pycord-Development:master' into automod
plun1331 May 23, 2022
1f676ce
Actions and some renaming
plun1331 May 23, 2022
b38fd76
more things
plun1331 May 23, 2022
69c0143
add gateway events
plun1331 May 24, 2022
458e400
update docs
plun1331 May 24, 2022
007919f
Merge branch 'Pycord-Development:master' into automod
plun1331 May 24, 2022
f7be9e2
Merge branch 'master' into automod
plun1331 May 24, 2022
2efe7fe
Update raw_models.py
plun1331 May 25, 2022
913d391
Update enums.py
plun1331 May 25, 2022
4450d3c
Update automod.py
plun1331 May 25, 2022
da2d30b
more stuff
plun1331 May 25, 2022
857c2f3
Merge branch 'master' into automod
plun1331 May 26, 2022
ec46e6a
Add missing import
plun1331 May 26, 2022
5a1a89e
update docs
plun1331 May 26, 2022
3fa5f39
Document permission requirements
plun1331 May 26, 2022
2df694b
Merge branch 'Pycord-Development:master' into automod
plun1331 Jun 2, 2022
01a13c0
add support for automod timeouts
plun1331 Jun 2, 2022
f1a41d7
Update raw_models.py
plun1331 Jun 2, 2022
5eeab40
keyword_lists to presets
plun1331 Jun 8, 2022
dea4da1
Merge branch 'automod' of https://github.com/plun1331/pycord into aut…
plun1331 Jun 8, 2022
423fac9
rework auto_moderation_action_execution event
plun1331 Jun 8, 2022
6737cfc
Remove comment
plun1331 Jun 16, 2022
cf95b07
Merge branch 'master' into automod
plun1331 Jun 16, 2022
3778054
Formatting fixes
plun1331 Jun 20, 2022
190bc80
Merge branch 'master' into automod
plun1331 Jun 20, 2022
e73ad6a
Fix incorrect HTTP method
plun1331 Jun 20, 2022
5a1cd59
Update discord/guild.py
plun1331 Jun 20, 2022
1983ba7
Remove unnecessary variable
plun1331 Jun 20, 2022
ac61789
Alphabetize
plun1331 Jun 20, 2022
7e01835
Alphabetizing
plun1331 Jun 20, 2022
cea2872
Return Object in exempt_* properties
plun1331 Jun 20, 2022
f6d7db0
Alphabetizing
plun1331 Jun 20, 2022
70764c0
Update guild.py
plun1331 Jun 20, 2022
7125ce5
Alphabetizing
plun1331 Jun 20, 2022
6879c6d
Support for reasons
plun1331 Jun 20, 2022
0d8df7d
Support for reasons
plun1331 Jun 20, 2022
9edab82
Support for reasons and fix docstrings
plun1331 Jun 20, 2022
3798236
Update docstrings
plun1331 Jun 20, 2022
30ea4bb
Merge branch 'master' into automod
Lulalaby Jun 20, 2022
a426c80
Merge branch 'master' into automod
Lulalaby Jul 2, 2022
ed75d01
Update discord/automod.py
Lulalaby Jul 2, 2022
2082db9
Merge branch 'master' into automod
Lulalaby Jul 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
421 changes: 421 additions & 0 deletions discord/automod.py

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class MessageType(Enum):
thread_starter_message = 21
guild_invite_reminder = 22
context_menu_command = 23
auto_moderation_action = 24


class VoiceRegion(Enum):
Expand Down Expand Up @@ -381,6 +382,10 @@ class AuditLogAction(Enum):
thread_update = 111
thread_delete = 112
application_command_permission_update = 121
auto_moderation_rule_create = 140
auto_moderation_rule_update = 141
auto_moderation_rule_delete = 142
auto_moderation_block_message = 143

@property
def category(self) -> Optional[AuditLogActionCategory]:
Expand Down Expand Up @@ -433,6 +438,10 @@ def category(self) -> Optional[AuditLogActionCategory]:
AuditLogAction.thread_update: AuditLogActionCategory.update,
AuditLogAction.thread_delete: AuditLogActionCategory.delete,
AuditLogAction.application_command_permission_update: AuditLogActionCategory.update,
AuditLogAction.auto_moderation_rule_create: AuditLogActionCategory.create,
AuditLogAction.auto_moderation_rule_update: AuditLogActionCategory.update,
AuditLogAction.auto_moderation_rule_delete: AuditLogActionCategory.delete,
AuditLogAction.auto_moderation_block_message: None,
}
return lookup[self]

Expand Down Expand Up @@ -471,6 +480,8 @@ def target_type(self) -> Optional[str]:
return "thread"
elif v < 121:
return "application_command_permission"
elif v < 144:
return "auto_moderation_rule"


class UserFlags(Enum):
Expand Down Expand Up @@ -764,6 +775,29 @@ class ScheduledEventLocationType(Enum):
voice = 2
external = 3


class AutoModTriggerType(Enum):
keyword = 1
harmful_link = 2
spam = 3
keyword_preset = 4


class AutoModEventType(Enum):
message_send = 1


class AutoModActionType(Enum):
block_message = 1
send_alert_message = 2
timeout = 3


class AutoModKeywordPresetType(Enum):
profanity = 1
sexual_content = 2
slurs = 3


T = TypeVar("T")

Expand Down
22 changes: 22 additions & 0 deletions discord/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,28 @@ def scheduled_events(self):
- :meth:`Guild.get_scheduled_event`
"""
return 1 << 16

@flag_value
def auto_moderation_configuration(self):
""":class:`bool`: Whether guild auto moderation configuration events are enabled.

This corresponds to the following events:

- :func:`on_auto_moderation_rule_create`
- :func:`on_auto_moderation_rule_update`
- :func:`on_auto_moderation_rule_delete`
"""
return 1 << 20

@flag_value
def auto_moderation_execution(self):
""":class:`bool`: Whether guild auto moderation execution events are enabled.

This corresponds to the following events:

- :func:`on_auto_moderation_action_execution`
"""
return 1 << 21


@fill_with_flags()
Expand Down
111 changes: 110 additions & 1 deletion discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@
overload,
)


plun1331 marked this conversation as resolved.
Show resolved Hide resolved
from . import abc, utils
from .automod import AutoModAction, AutoModRule, AutoModTriggerMetadata
from .asset import Asset
from .channel import *
from .channel import _guild_channel_factory, _threaded_guild_channel_factory
from .colour import Colour
from .emoji import Emoji
from .enums import (
AuditLogAction,
AutoModEventType,
AutoModTriggerType,
ChannelType,
ContentFilter,
NotificationLevel,
Expand Down Expand Up @@ -241,6 +245,7 @@ class Guild(Hashable):
- ``VERIFIED``: Guild is a verified server.
- ``VIP_REGIONS``: Guild has VIP voice regions.
- ``WELCOME_SCREEN_ENABLED``: Guild has enabled the welcome screen.
- ``AUTO_MODERATION``: Guild has enabled the auto moderation system.
plun1331 marked this conversation as resolved.
Show resolved Hide resolved

premium_tier: :class:`int`
The premium tier for this guild. Corresponds to "Nitro Server" in the official UI.
Expand Down Expand Up @@ -550,7 +555,7 @@ def _from_data(self, guild: GuildPayload) -> None:

for obj in guild.get("voice_states", []):
self._update_voice_state(obj, int(obj["channel_id"]))

plun1331 marked this conversation as resolved.
Show resolved Hide resolved
# TODO: refactor/remove?
def _sync(self, data: GuildPayload) -> None:
try:
Expand Down Expand Up @@ -3531,3 +3536,107 @@ async def create_scheduled_event(
def scheduled_events(self) -> List[ScheduledEvent]:
"""List[:class:`.ScheduledEvent`]: A list of scheduled events in this guild."""
return list(self._scheduled_events.values())

async def fetch_auto_moderation_rules(self) -> List[AutoModRule]:
"""|coro|

Retrieves a list of auto moderation rules for this guild.

Raises
-------
HTTPException
Getting the auto moderation rules failed.
Forbidden
You do not have the Manage Guild permission.

Returns
--------
List[:class:`AutoModRule`]
The auto moderation rules for this guild.
"""
data = await self._state.http.get_auto_moderation_rules(self.id)
result = []
for rule in data:
result.append(AutoModRule(state=self._state, data=rule))
return result
plun1331 marked this conversation as resolved.
Show resolved Hide resolved

async def fetch_auto_moderation_rule(self, id: int) -> AutoModRule:
"""|coro|

Retrieves a :class:`AutoModRule` from rule ID.

Raises
-------
HTTPException
Getting the auto moderation rule failed.
Forbidden
You do not have the Manage Guild permission.

Returns
--------
:class:`AutoModRule`
The requested auto moderation rule.
"""
data = await self._state.http.get_auto_moderation_rule(self.id, id)
return AutoModRule(state=self._state, data=data)

async def create_auto_moderation_rule(
self,
*,
name: str,
event_type: AutoModEventType,
trigger_type: AutoModTriggerType,
trigger_metadata: AutoModTriggerMetadata,
actions: List[AutoModAction],
enabled: bool = False,
exempt_roles: List[Snowflake] = None,
exempt_channels: List[Snowflake] = None,
) -> AutoModRule:
"""
Creates an auto moderation rule.

Parameters
-----------
name: :class:`str`
The name of the auto moderation rule.
event_type: :class:`AutoModEventType`
The type of event that triggers the rule.
trigger_type: :class:`AutoModTriggerType`
The rule's trigger type.
trigger_metadata: :class:`AutoModTriggerMetadata`
The rule's trigger metadata.
actions: :class:`List[AutoModAction]`
The actions to take when the rule is triggered.
enabled: :class:`bool`
Whether the rule is enabled.
exempt_roles: :class:`List[Snowflake]`
A list of roles that are exempt from the rule.
exempt_channels: :class:`List[Snowflake]`
A list of channels that are exempt from the rule.

Raises
-------
HTTPException
Creating the auto moderation rule failed.
Forbidden
You do not have the Manage Guild permission.

Returns
--------
:class:`AutoModRule`
The new auto moderation rule.
"""
payload = {
"name": name,
"event_type": event_type.value,
"trigger_type": trigger_type.value,
"trigger_metadata": trigger_metadata.to_dict(),
"actions": [a.to_dict() for a in actions],
"enabled": enabled,
}
if exempt_roles:
payload["exempt_roles"] = [r.id for r in exempt_roles]
if exempt_channels:
payload["exempt_channels"] = [c.id for c in exempt_channels]
data = await self._state.http.create_auto_moderation_rule(self.id, payload)
plun1331 marked this conversation as resolved.
Show resolved Hide resolved
return AutoModRule(state=self._state, data=data)
66 changes: 66 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from .types import (
appinfo,
audit_log,
automod,
channel,
components,
embed,
Expand Down Expand Up @@ -2317,6 +2318,71 @@ def get_guild_command_permissions(
)
return self.request(r)

# Guild Automod Rules

def get_auto_moderation_rules(
self,
guild_id: Snowflake,
) -> Response[List[automod.AutoModRule]]:
r = Route(
"GET",
"/guilds/{guild_id}/auto-moderation/rules",
guild_id=guild_id,
)
return self.request(r)

def get_auto_moderation_rule(
self,
guild_id: Snowflake,
rule_id: Snowflake,
) -> Response[automod.AutoModRule]:
r = Route(
"GET",
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
guild_id=guild_id,
rule_id=rule_id,
)
return self.request(r)

def create_auto_moderation_rule(
self,
guild_id: Snowflake,
payload: automod.CreateAutoModRule,
) -> Response[automod.AutoModRule]:
r = Route(
"POST",
"/guilds/{guild_id}/auto-moderation/rules",
guild_id=guild_id,
)
return self.request(r, json=payload)

def edit_auto_moderation_rule(
self,
guild_id: Snowflake,
rule_id: Snowflake,
payload: automod.EditAutoModRule,
) -> Response[automod.AutoModRule]:
r = Route(
"POST",
plun1331 marked this conversation as resolved.
Show resolved Hide resolved
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
guild_id=guild_id,
rule_id=rule_id,
)
return self.request(r, json=payload)

def delete_auto_moderation_rule(
self,
guild_id: Snowflake,
rule_id: Snowflake,
) -> Response[None]:
r = Route(
"DELETE",
"/guilds/{guild_id}/auto-moderation/rules/{rule_id}",
guild_id=guild_id,
rule_id=rule_id,
)
return self.request(r)

# Interaction responses

def _edit_webhook_helper(
Expand Down
Loading