Implement AutoMod

This commit is contained in:
Alex Nørgaard
2022-06-27 05:47:52 +01:00
committed by GitHub
parent 7ad00750c6
commit 5426d19dc7
12 changed files with 1279 additions and 2 deletions

View File

@@ -71,6 +71,7 @@ from .enums import (
NSFWLevel,
MFALevel,
Locale,
AutoModRuleEventType,
)
from .mixins import Hashable
from .user import User
@@ -87,6 +88,7 @@ from .file import File
from .audit_logs import AuditLogEntry
from .object import OLDEST_OBJECT, Object
from .welcome_screen import WelcomeScreen, WelcomeChannel
from .automod import AutoModRule, AutoModTrigger, AutoModRuleAction
__all__ = (
@@ -3833,3 +3835,124 @@ class Guild(Hashable):
ws = self._state._get_websocket(self.id)
channel_id = channel.id if channel else None
await ws.voice_state(self.id, channel_id, self_mute, self_deaf)
async def fetch_automod_rule(self, automod_rule_id: int, /) -> AutoModRule:
"""|coro|
Fetches an active automod rule from the guild.
You must have the :attr:`Permissions.manage_guild` to use this.
.. versionadded:: 2.0
Parameters
-----------
automod_rule_id: :class:`int`
The ID of the automod rule to fetch.
Raises
-------
Forbidden
You do not have permission to view the automod rule.
Returns
--------
:class:`AutoModRule`
The automod rule that was fetched.
"""
data = await self._state.http.get_auto_moderation_rule(self.id, automod_rule_id)
return AutoModRule(data=data, guild=self, state=self._state)
async def fetch_automod_rules(self) -> List[AutoModRule]:
"""|coro|
Fetches all automod rules from the guild.
You must have the :attr:`Permissions.manage_guild` to use this.
.. versionadded:: 2.0
Raises
-------
Forbidden
You do not have permission to view the automod rule.
NotFound
There are no automod rules within this guild.
Returns
--------
List[:class:`AutoModRule`]
The automod rules that were fetched.
"""
data = await self._state.http.get_auto_moderation_rules(self.id)
return [AutoModRule(data=d, guild=self, state=self._state) for d in data]
async def create_automod_rule(
self,
*,
name: str,
event_type: AutoModRuleEventType,
trigger: AutoModTrigger,
actions: List[AutoModRuleAction],
enabled: bool = MISSING,
exempt_roles: Sequence[Snowflake] = MISSING,
exempt_channels: Sequence[Snowflake] = MISSING,
reason: str = MISSING,
) -> AutoModRule:
"""|coro|
Create an automod rule.
You must have the :attr:`Permissions.manage_guild` permission to use this.
.. versionadded:: 2.0
Parameters
-----------
name: :class:`str`
The name of the automod rule.
event_type: :class:`AutoModRuleEventType`
The type of event that the automod rule will trigger on.
trigger: :class:`AutoModTrigger`
The trigger that will trigger the automod rule.
actions: List[:class:`AutoModRuleAction`]
The actions that will be taken when the automod rule is triggered.
enabled: :class:`bool`
Whether the automod rule is enabled.
Discord will default to ``False``.
exempt_roles: Sequence[:class:`abc.Snowflake`]
A list of roles that will be exempt from the automod rule.
exempt_channels: Sequence[:class:`abc.Snowflake`]
A list of channels that will be exempt from the automod rule.
reason: :class:`str`
The reason for creating this automod rule. Shows up on the audit log.
Raises
-------
Forbidden
You do not have permissions to create an automod rule.
HTTPException
Creating the automod rule failed.
Returns
--------
:class:`AutoModRule`
The automod rule that was created.
"""
data = await self._state.http.create_auto_moderation_rule(
self.id,
name=name,
event_type=event_type.value,
trigger_type=trigger.type.value,
trigger_metadata=trigger.to_metadata_dict() or None,
actions=[a.to_dict() for a in actions],
enabled=enabled,
exempt_roles=[str(r.id) for r in exempt_roles] if exempt_roles else None,
exempt_channel=[str(c.id) for c in exempt_channels] if exempt_channels else None,
reason=reason,
)
return AutoModRule(data=data, guild=self, state=self._state)